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] Adding example project #744

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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" />
</ItemGroup>

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

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

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

</Project>
39 changes: 39 additions & 0 deletions examples/event-counters/Examples.EventCounters/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// <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());

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

eventCounter.WriteMetric(0);
eventCounter.WriteMetric(1000);

Thread.Sleep(1500);
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
```