diff --git a/examples/event-counters/Examples.EventCounters/Examples.EventCounters.csproj b/examples/event-counters/Examples.EventCounters/Examples.EventCounters.csproj new file mode 100644 index 0000000000..29ed2fe979 --- /dev/null +++ b/examples/event-counters/Examples.EventCounters/Examples.EventCounters.csproj @@ -0,0 +1,19 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + + diff --git a/examples/event-counters/Examples.EventCounters/Program.cs b/examples/event-counters/Examples.EventCounters/Program.cs new file mode 100644 index 0000000000..fc98de6fbb --- /dev/null +++ b/examples/event-counters/Examples.EventCounters/Program.cs @@ -0,0 +1,42 @@ +// +// 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. +// + +using System.Diagnostics.Tracing; +using OpenTelemetry; +using OpenTelemetry.Metrics; + +// Create EventSources and EventCounters +ThreadLocal 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); diff --git a/examples/event-counters/Examples.EventCounters/README.md b/examples/event-counters/Examples.EventCounters/README.md new file mode 100644 index 0000000000..d79aefe1a6 --- /dev/null +++ b/examples/event-counters/Examples.EventCounters/README.md @@ -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 +``` diff --git a/opentelemetry-dotnet-contrib.sln b/opentelemetry-dotnet-contrib.sln index 5c61596560..ff592c7b7b 100644 --- a/opentelemetry-dotnet-contrib.sln +++ b/opentelemetry-dotnet-contrib.sln @@ -242,6 +242,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 @@ -508,6 +510,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 @@ -586,6 +592,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} diff --git a/src/OpenTelemetry.Instrumentation.EventCounters/README.md b/src/OpenTelemetry.Instrumentation.EventCounters/README.md index bbfc44a413..1be4d33fab 100644 --- a/src/OpenTelemetry.Instrumentation.EventCounters/README.md +++ b/src/OpenTelemetry.Instrumentation.EventCounters/README.md @@ -11,6 +11,9 @@ using OpenTelemetry Metrics API. ## Steps to enable OpenTelemetry.Instrumentation.EventCounters +You can view an example project using EventCounters at +`/examples/event-counters/Examples.EventCounters`. + ### Step 1: Install Package Add a reference to the