-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EventCounters] Add example project (#860)
- Loading branch information
Showing
5 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
examples/event-counters/Examples.EventCounters/Examples.EventCounters.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="$(OpenTelemetryCoreLatestVersion)" /> | ||
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="$(OpenTelemetryCoreLatestVersion)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.EventCounters\OpenTelemetry.Instrumentation.EventCounters.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// <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 | ||
ThreadLocal<Random> random = new(() => new Random()); | ||
using EventSource eventSource = new("MyEventSource"); | ||
using EventCounter eventCounter = new("MyEventCounter", eventSource); | ||
using PollingCounter pollingCounter = new("MyPollingCounter", eventSource, () => random.Value!.NextDouble()); | ||
|
||
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters