Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EventCounters] Add example project #860

Merged
merged 17 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.3.1" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.3.1" />
mic-max marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.EventCounters\OpenTelemetry.Instrumentation.EventCounters.csproj" />
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions examples/event-counters/Examples.EventCounters/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// <copyright file="Program.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System.Diagnostics.Tracing;
using OpenTelemetry;
using OpenTelemetry.Metrics;

// Create EventSources and EventCounters
EventSource eventSource = new("MyEventSource");
EventCounter eventCounter = new("MyEventCounter", eventSource);
PollingCounter pollingCounter = new("MyPollingCounter", eventSource, () => new Random().NextDouble());
Kielek marked this conversation as resolved.
Show resolved Hide resolved

// Create and Configure Meter Provider
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddEventCountersInstrumentation(options =>
{
options.AddEventSources(eventSource.Name);
options.RefreshIntervalSecs = 1;
})
.AddConsoleExporter()
.Build();

// Write to EventCounters
eventCounter.WriteMetric(0);
eventCounter.WriteMetric(1000);

// Wait for EventCounter data to be polled (RefreshIntervalSecs is 1 second by default)
Thread.Sleep(1200);
21 changes: 21 additions & 0 deletions examples/event-counters/Examples.EventCounters/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# EventCounters Instrumentation for OpenTelemetry .NET - Examples

This is an all-in-one sample that shows how to publish EventCounters using
the OpenTelemetry Metrics Api.

## Expected Output

After running `dotnet run` from this directory

```text
Resource associated with Metric:
service.name: unknown_service:Examples.EventCounters

Export EventCounters.MyEventSource.MyEventCounter, Meter: OpenTelemetry.Instrumentation.EventCounters/1.0.0.0
(2022-11-01T17:37:37.9046769Z, 2022-11-01T17:37:38.4014060Z] DoubleGauge
Value: 500

Export EventCounters.MyEventSource.MyPollingCounter, Meter: OpenTelemetry.Instrumentation.EventCounters/1.0.0.0
(2022-11-01T17:37:37.9076414Z, 2022-11-01T17:37:38.4014299Z] DoubleGauge
Value: 0.5233669819037192
```
7 changes: 7 additions & 0 deletions opentelemetry-dotnet-contrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Instrumentati
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "process-instrumentation", "examples\process-instrumentation\process-instrumentation.csproj", "{C3B3BBAF-CC38-4D5C-AFA2-33184D07BF75}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Examples.EventCounters", "examples\event-counters\Examples.EventCounters\Examples.EventCounters.csproj", "{BA58CC8B-F5CA-4DC7-A3A8-D01B2E10731E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -509,6 +511,10 @@ Global
{C3B3BBAF-CC38-4D5C-AFA2-33184D07BF75}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3B3BBAF-CC38-4D5C-AFA2-33184D07BF75}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3B3BBAF-CC38-4D5C-AFA2-33184D07BF75}.Release|Any CPU.Build.0 = Release|Any CPU
{BA58CC8B-F5CA-4DC7-A3A8-D01B2E10731E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BA58CC8B-F5CA-4DC7-A3A8-D01B2E10731E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA58CC8B-F5CA-4DC7-A3A8-D01B2E10731E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BA58CC8B-F5CA-4DC7-A3A8-D01B2E10731E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -587,6 +593,7 @@ Global
{0F18B7C8-B192-4236-9578-7AD02BFC7128} = {2097345F-4DD3-477D-BC54-A922F9B2B402}
{B40B975E-78B2-4712-8B4D-BADA67DF0C0A} = {22DF5DC0-1290-4E83-A9D8-6BB7DE3B3E63}
{C3B3BBAF-CC38-4D5C-AFA2-33184D07BF75} = {B75EE478-97F7-4E9F-9A5A-DB3D0988EDEA}
{BA58CC8B-F5CA-4DC7-A3A8-D01B2E10731E} = {B75EE478-97F7-4E9F-9A5A-DB3D0988EDEA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B0816796-CDB3-47D7-8C3C-946434DE3B66}
Expand Down
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Instrumentation.EventCounters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ using OpenTelemetry Metrics API.

## Steps to enable OpenTelemetry.Instrumentation.EventCounters

You can view an example project using event counters at
mic-max marked this conversation as resolved.
Show resolved Hide resolved
`/examples/event-counters/Examples.EventCounters`.

### Step 1: Install Package

Add a reference to the
Expand Down