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

Create an EventCounter Listener that subscribes to all event counters #365

Merged
Merged
Show file tree
Hide file tree
Changes from 14 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
41 changes: 41 additions & 0 deletions .github/ISSUE_TEMPLATE/comp_instrumentation_eventcounters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
name: OpenTelemetry.Instrumentation.EventCounters
about: Issue with OpenTelemetry.Instrumentation.EventCounters
labels: comp:instrumentation.eventcounters
---

# Issue with OpenTelemetry.Instrumentation.EventCounters

List of [all OpenTelemetry NuGet
packages](https://www.nuget.org/profiles/OpenTelemetry) and version that you are
using (e.g. `OpenTelemetry 1.0.2`):

* TBD

Runtime version (e.g. `net462`, `net48`, `netcoreapp3.1`, `net6.0` etc. You can
find this information from the `*.csproj` file):

* TBD

**Is this a feature request or a bug?**

* [ ] Feature Request
* [ ] Bug

**What is the expected behavior?**

What do you expect to see?

**What is the actual behavior?**

What did you see instead? If you are reporting a bug, create a self-contained
project using the template of your choice and apply the minimum required code to
result in the issue you're observing. We will close this issue if:

* The repro project you share with us is complex. We can't investigate custom
projects, so don't point us to such, please.
* If we can not reproduce the behavior you're reporting.

## Additional Context

Add any other context about the feature request here.
5 changes: 4 additions & 1 deletion .github/component_owners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ components:
- xiang17
src/OpenTelemetry.Instrumentation.Wcf/:
- codeblanch

src/OpenTelemetry.Instrumentation.EventCounters/:
- hananiel
test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/:
- srprash
- lupengamzn
Expand Down Expand Up @@ -97,3 +98,5 @@ components:
- xiang17
test/OpenTelemetry.Instrumentation.Wcf.Tests/:
- codeblanch
test/OpenTelemetry.Instrumentation.EventCounters.Tests/:
- hananiel
49 changes: 49 additions & 0 deletions .github/workflows/package-Instrumentation.EventCounters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Pack OpenTelemetry.Instrumentation.EventCounters

on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'warning'
push:
tags:
- 'Instrumentation.EventCounters-*' # trigger when we create a tag with prefix "Instrumentation.EventCounters-"

jobs:
build-test-pack:
runs-on: ${{ matrix.os }}
env:
PROJECT: OpenTelemetry.Instrumentation.EventCounters

strategy:
matrix:
os: [windows-latest]

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # fetching all

- name: Install dependencies
run: dotnet restore

- name: dotnet build ${{env.PROJECT}}
run: dotnet build src/${{env.PROJECT}} --configuration Release --no-restore -p:Deterministic=true

- name: dotnet test ${{env.PROJECT}}
run: dotnet test test/${{env.PROJECT}}.Tests

- name: dotnet pack ${{env.PROJECT}}
run: dotnet pack src/${{env.PROJECT}} --configuration Release --no-build

- name: Publish Artifacts
uses: actions/upload-artifact@v3
with:
name: ${{env.PROJECT}}-packages
path: '**/${{env.PROJECT}}/bin/**/*.*nupkg'

- name: Publish Nuget
run: |
nuget push **/${{env.PROJECT}}/bin/**/*.nupkg -Source https://api.nuget.org/v3/index.json -ApiKey ${{ secrets.NUGET_TOKEN }} -SymbolApiKey ${{ secrets.NUGET_TOKEN }}
14 changes: 14 additions & 0 deletions opentelemetry-dotnet-contrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples.AspNet", "examples
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "runtime-instrumentation", "examples\runtime-instrumentation\runtime-instrumentation.csproj", "{9FF9B46A-AD93-4B3F-92DA-6FDCC98FEA91}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Instrumentation.EventCounters", "src\OpenTelemetry.Instrumentation.EventCounters\OpenTelemetry.Instrumentation.EventCounters.csproj", "{AE12EFB7-4B1A-46B8-B89A-0375252B10B1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Instrumentation.EventCounters.Tests", "test\OpenTelemetry.Instrumentation.EventCounters.Tests\OpenTelemetry.Instrumentation.EventCounters.Tests.csproj", "{DDA355A3-4D75-4F45-9A5E-E93C3EFB9896}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -470,6 +474,14 @@ Global
{9FF9B46A-AD93-4B3F-92DA-6FDCC98FEA91}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9FF9B46A-AD93-4B3F-92DA-6FDCC98FEA91}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9FF9B46A-AD93-4B3F-92DA-6FDCC98FEA91}.Release|Any CPU.Build.0 = Release|Any CPU
{AE12EFB7-4B1A-46B8-B89A-0375252B10B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AE12EFB7-4B1A-46B8-B89A-0375252B10B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AE12EFB7-4B1A-46B8-B89A-0375252B10B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AE12EFB7-4B1A-46B8-B89A-0375252B10B1}.Release|Any CPU.Build.0 = Release|Any CPU
{DDA355A3-4D75-4F45-9A5E-E93C3EFB9896}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DDA355A3-4D75-4F45-9A5E-E93C3EFB9896}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DDA355A3-4D75-4F45-9A5E-E93C3EFB9896}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DDA355A3-4D75-4F45-9A5E-E93C3EFB9896}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -543,6 +555,8 @@ Global
{2B6D0764-5E66-423A-9943-B3A72FF181EA} = {B75EE478-97F7-4E9F-9A5A-DB3D0988EDEA}
{9A4E3A68-904B-4835-A3C8-F664B73098DB} = {2B6D0764-5E66-423A-9943-B3A72FF181EA}
{9FF9B46A-AD93-4B3F-92DA-6FDCC98FEA91} = {B75EE478-97F7-4E9F-9A5A-DB3D0988EDEA}
{AE12EFB7-4B1A-46B8-B89A-0375252B10B1} = {22DF5DC0-1290-4E83-A9D8-6BB7DE3B3E63}
{DDA355A3-4D75-4F45-9A5E-E93C3EFB9896} = {2097345F-4DD3-477D-BC54-A922F9B2B402}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B0816796-CDB3-47D7-8C3C-946434DE3B66}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

## 0.1.0-alpha.1

* This is the first release of `OpenTelemetry.Instrumentation.EventCounters` package.

For more details, please refer to the [README](README.md).
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
// <copyright file="EventCounterListener.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;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Diagnostics.Tracing;
using System.Reflection;

namespace OpenTelemetry.Instrumentation.EventCounters
{
internal class EventCounterListener : EventListener
{
internal static readonly AssemblyName AssemblyName = typeof(EventCounterListener).Assembly.GetName();
internal static readonly string InstrumentationName = AssemblyName.Name;
internal static readonly string InstrumentationVersion = AssemblyName.Version.ToString();

private readonly bool isInitialized = false;
private readonly Meter meter;
private readonly EventCounterMetricsOptions options;
private readonly ConcurrentDictionary<MetricKey, Instrument> metricInstruments = new();
private readonly ConcurrentDictionary<MetricKey, double> lastValue = new();
private readonly ConcurrentBag<EventSource> eventSources = new();

public EventCounterListener(EventCounterMetricsOptions options)
{
this.meter = new Meter(InstrumentationName, InstrumentationVersion);
this.options = options ?? throw new ArgumentNullException(nameof(options));

this.isInitialized = true;
this.EnablePendingEventSources(); // Some OnEventSourceCreated may have fired before constructor, enable them
}

private enum InstrumentType
{
ObservableGauge,
ObservableCounter,
}

private Dictionary<string, string> EnableEventArgs => new() { ["EventCounterIntervalSec"] = this.options.RefreshIntervalSecs.ToString(), };

protected override void OnEventSourceCreated(EventSource source)
{
// TODO: Add Configuration options to selectively subscribe to EventCounters
try
{
if (!this.isInitialized)
{
this.eventSources.Add(source);
}
else
{
this.EnableEvents(source, EventLevel.Verbose, EventKeywords.All, this.EnableEventArgs);
}
}
catch (Exception ex)
{
EventCountersInstrumentationEventSource.Log.ErrorEventCounter(source.Name, ex.Message);
}
}

protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
if (!eventData.EventName.Equals("EventCounters") || !this.isInitialized)
{
return;
}

try
{
if (eventData.Payload.Count > 0 && eventData.Payload[0] is IDictionary<string, object> eventPayload)
{
this.ExtractAndPostMetric(eventData.EventSource.Name, eventPayload);
}
else
{
EventCountersInstrumentationEventSource.Log.IgnoreEventWrittenAsEventPayloadNotParseable(eventData.EventSource.Name);
}
}
catch (Exception ex)
{
EventCountersInstrumentationEventSource.Log.ErrorEventCounter(eventData.EventName, ex.ToString());
}
}

private void ExtractAndPostMetric(string eventSourceName, IDictionary<string, object> eventPayload)
{
try
{
bool calculateRate = false;
double actualValue = 0;

string counterName = string.Empty;
string counterDisplayName = string.Empty;
InstrumentType instrumentType = InstrumentType.ObservableGauge;

foreach (KeyValuePair<string, object> payload in eventPayload)
{
var key = payload.Key;
if (key.Equals("Name", StringComparison.OrdinalIgnoreCase))
{
counterName = payload.Value.ToString();
}
else
if (key.Equals("DisplayName", StringComparison.OrdinalIgnoreCase))
{
counterDisplayName = payload.Value.ToString();
}
else if (key.Equals("Mean", StringComparison.OrdinalIgnoreCase))
{
instrumentType = InstrumentType.ObservableGauge;
actualValue = Convert.ToDouble(payload.Value);
}
else if (key.Equals("Increment", StringComparison.OrdinalIgnoreCase))
hananiel marked this conversation as resolved.
Show resolved Hide resolved
{
// Increment indicates we have to calculate rate.
instrumentType = InstrumentType.ObservableCounter;
calculateRate = true;
hananiel marked this conversation as resolved.
Show resolved Hide resolved
actualValue = Convert.ToDouble(payload.Value);
}
}

this.RecordMetric(eventSourceName, counterName, counterDisplayName, instrumentType, actualValue, calculateRate);
}
catch (Exception ex)
{
EventCountersInstrumentationEventSource.Log.EventCountersInstrumentationWarning("ExtractMetric", ex.Message);
}
}

private void RecordMetric(string eventSourceName, string counterName, string displayName, InstrumentType instrumentType, double value, bool calculateRate)
{
var metricKey = new MetricKey(eventSourceName, counterName);
var description = string.IsNullOrEmpty(displayName) ? counterName : displayName;
this.lastValue[metricKey] = calculateRate ? value / this.options.RefreshIntervalSecs : value;
switch (instrumentType)
{
case InstrumentType.ObservableCounter:
if (!this.metricInstruments.ContainsKey(metricKey))
utpilla marked this conversation as resolved.
Show resolved Hide resolved
{
this.metricInstruments.TryAdd(metricKey, this.meter.CreateObservableCounter(counterName, () => this.ObserveDouble(metricKey), description: description));
}

break;

case InstrumentType.ObservableGauge:

if (!this.metricInstruments.TryGetValue(metricKey, out Instrument instrument))
{
this.metricInstruments[metricKey] = this.meter.CreateObservableGauge(counterName, () => this.ObserveDouble(metricKey), description: description);
utpilla marked this conversation as resolved.
Show resolved Hide resolved
}

break;
}
}

private double ObserveDouble(MetricKey key) => this.lastValue[key];

private void EnablePendingEventSources()
{
foreach (var source in this.eventSources)
{
this.EnableEvents(source, EventLevel.Verbose, EventKeywords.All, this.EnableEventArgs);
}
}

private class MetricKey
{
public MetricKey(string eventSourceName, string counterName)
{
this.EventSourceName = eventSourceName;
this.CounterName = counterName;
}

public string EventSourceName { get; private set; }

public string CounterName { get; private set; }

public override int GetHashCode() => (this.EventSourceName, this.CounterName).GetHashCode();

public override bool Equals(object obj) =>
obj is MetricKey nextKey && this.EventSourceName == nextKey.EventSourceName && this.CounterName == nextKey.CounterName;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// <copyright file="EventCounterMetricsOptions.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>

namespace OpenTelemetry.Instrumentation.EventCounters
{
/// <summary>
/// EventCounterMetrics Options.
/// </summary>
public class EventCounterMetricsOptions
{
/// <summary>
/// Gets or sets the subscription interval in seconds.
/// </summary>
public int RefreshIntervalSecs { get; set; } = 60;
}
}
Loading