-
Notifications
You must be signed in to change notification settings - Fork 302
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
Event counter listener #177
Closed
Closed
Changes from 2 commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
cf49964
Initial commit
hananiel 3ca132b
formatting
hananiel 7381f48
Add missing headers
twenzel 79ce3c2
Remove not required packages
twenzel 769ff52
Add example
twenzel a92627b
Rename project
twenzel 3fb77d3
First implementation for options
twenzel 86367ca
Working metrics
twenzel 18c3507
Update readme
twenzel bffd200
Distinguish between different counters
twenzel 69bd102
Better event source configuration
twenzel 4f7ff3e
Support more metric types
twenzel a562375
Add tests
twenzel 0619d33
Merge pull request #3 from twenzel/EventSourceConfiguration
6dbbf45
Test configuration via IConfiguration
twenzel 1a9c341
Add NET 6 to PR build
twenzel d5abbfe
Create package-Instrumentation.EventCounters.yml
twenzel 464dfe4
Merge pull request #4 from twenzel/EventSourceConfiguration
2e9350c
Revert example to Net5.0
hananiel a4e5b62
Markdown lint
hananiel 97d9521
More MD lint
hananiel 15f3a97
more markdown
hananiel f37cef3
Code review feedback: deleting example, removing well known counters,…
hananiel 73a5ced
Remove old file
twenzel 9b52ac4
Remove change of adding .NET 6
twenzel 95542fb
Adds more tests
twenzel faeb150
Remove example project as wished by reviewer
twenzel 85eb1c6
fix markdown lint
twenzel d72a15e
Fix typo
twenzel 8540b42
Merge pull request #8 from twenzel/hananiel-EventCounterListener
b11b0b6
Merge branch 'main' into EventCounterListener
cfb4d0f
Lint issues and re-add the properties; not sure if we need them or not
hananiel 07ac588
Add more tests
twenzel 7f5bf0d
Use new Instrumentation type enum
twenzel 35d6a6a
Better way to infer instrumentation type
twenzel d88c972
Fix markdown lint
twenzel 6074cf1
Fix merge issue
twenzel c98619b
Merge pull request #10 from twenzel/EventCounterListener
85530dd
Fix sanity check
twenzel 1f66660
Add more tests
twenzel 188b919
Rename variables
twenzel b3a7d27
Merge pull request #12 from twenzel/EventCounterListener
16ceae7
Rename instrumentation types to reflect the created instrumentation
twenzel bb3a3d4
Merge pull request #14 from twenzel/EventCounterListener
02f3e19
Fix test
twenzel a32c005
Merge pull request #15 from twenzel/EventCounterListener
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
23 changes: 23 additions & 0 deletions
23
src/OpenTelemetry.Contrib.EventCounterListener/AssemblyInfo.cs
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,23 @@ | ||
// <copyright file="AssemblyInfo.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.Runtime.CompilerServices; | ||
|
||
#if SIGNED | ||
[assembly: InternalsVisibleTo("OpenTelemetry.Contrib.EventCounterListener.Tests, PublicKey=")] | ||
#else | ||
[assembly: InternalsVisibleTo("OpenTelemetry.Contrib.EventCounterListener.Tests")] | ||
#endif |
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,5 @@ | ||
# Changelog | ||
|
||
## Unreleased | ||
|
||
* Initial release |
37 changes: 37 additions & 0 deletions
37
src/OpenTelemetry.Contrib.EventCounterListener/EventCounter.cs
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,37 @@ | ||
// <copyright file="MySqlDataInstrumentationOptions.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.Data; | ||
using System.Diagnostics; | ||
using System.Text.RegularExpressions; | ||
|
||
using OpenTelemetry.Trace; | ||
|
||
namespace OpenTelemetry.Contrib.Instrumentation.EventCounterListener | ||
{ | ||
|
||
/// <summary> | ||
/// The Event Counter to listen to | ||
/// </summary> | ||
public class EventCounter | ||
{ | ||
public string Name { get; set; } | ||
|
||
public string Type { get; set; } | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
src/OpenTelemetry.Contrib.EventCounterListener/EventCounterListener.cs
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,135 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Metrics; | ||
using System.Diagnostics.Tracing; | ||
using System.Reflection; | ||
using Microsoft.Diagnostics.Monitoring.EventPipe; | ||
using OpenTelemetry.Contrib.EventCounterListener.EventPipe; | ||
|
||
namespace OpenTelemetry.Contrib.Instrumentation.EventCounterListener | ||
{ | ||
/// <summary> | ||
/// EventCounterListener that subscribes to EventSource Events. | ||
/// </summary> | ||
public class EventCounterListener : EventListener | ||
hananiel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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 string eventSourceName = "System.Runtime"; // TODO : Get from options | ||
private readonly string eventName = "EventCounters"; | ||
private readonly Meter meter; | ||
private readonly EventCounterListenerOptions options; | ||
|
||
private ConcurrentDictionary<MetricKey, double> metericStore = new(); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="EventCounterListener"/> class. | ||
/// </summary> | ||
/// <param name="options">Options to configure the EventCounterListener</param> | ||
public EventCounterListener(EventCounterListenerOptions options) | ||
{ | ||
this.options = options; | ||
this.meter = new Meter(InstrumentationName, InstrumentationVersion); | ||
} | ||
|
||
/// <summary> | ||
/// Processes a new EventSource event. | ||
/// </summary> | ||
/// <param name="eventData">Event to process.</param> | ||
protected override void OnEventWritten(EventWrittenEventArgs eventData) | ||
{ | ||
if (eventData == null) | ||
{ | ||
throw new ArgumentNullException(nameof(eventData)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does anyone catch this? |
||
} | ||
|
||
try | ||
{ | ||
if (eventData.EventName.Equals(this.eventName, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
this.ExtractAndRecordMetric(eventData); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
EventCounterListenerEventSource.Log.ErrorEventCounter(this.eventName, ex.ToString()); | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void OnEventSourceCreated(EventSource eventSource) | ||
{ | ||
if (eventSource == null) | ||
{ | ||
throw new ArgumentNullException(nameof(eventSource)); | ||
} | ||
|
||
if (this.eventSourceName.Equals(eventSource.Name, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
var refreshInterval = new Dictionary<string, string>() { { "EventCounterIntervalSec", "1" } }; // TODO: Get from configuration | ||
try | ||
{ | ||
this.EnableEvents(eventSource, EventLevel.Verbose, EventKeywords.All, refreshInterval); | ||
} | ||
catch (Exception ex) | ||
{ | ||
// TODO: Log to eventSource | ||
} | ||
} | ||
} | ||
|
||
private static bool CompareMetrics(ICounterPayload first, ICounterPayload second) | ||
{ | ||
return string.Equals(first.Name, second.Name); | ||
} | ||
|
||
private double ObserveValue(MetricKey key) | ||
{ | ||
// return last value | ||
return this.metericStore[key]; | ||
} | ||
|
||
private void ExtractAndRecordMetric(EventWrittenEventArgs eventWrittenEventArgs) | ||
{ | ||
eventWrittenEventArgs.TryGetCounterPayload(out var eventPayload); | ||
var metricKey = new MetricKey(eventPayload); | ||
if (!this.metericStore.ContainsKey(metricKey)) | ||
{ | ||
this.meter.CreateObservableGauge<double>(eventPayload.Name, () => this.ObserveValue(metricKey), eventPayload.DisplayName); | ||
} | ||
|
||
this.metericStore[metricKey] = eventPayload.Value; | ||
} | ||
|
||
private sealed class MetricKey | ||
{ | ||
private ICounterPayload metric; | ||
|
||
public MetricKey(ICounterPayload metric) | ||
{ | ||
this.metric = metric; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
HashCode code = default(HashCode); | ||
code.Add(this.metric.Provider); | ||
code.Add(this.metric.Name); | ||
return code.ToHashCode(); | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (obj is MetricKey metricKey) | ||
{ | ||
return CompareMetrics(this.metric, metricKey.metric); | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/OpenTelemetry.Contrib.EventCounterListener/EventCounterListenerEventSource.cs
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,35 @@ | ||
// <copyright file="MySqlDataInstrumentationEventSource.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; | ||
|
||
namespace OpenTelemetry.Contrib.Instrumentation.EventCounterListener | ||
{ | ||
/// <summary> | ||
/// EventSource events emitted from the project. | ||
/// </summary> | ||
[EventSource(Name = "OpenTelemetry-Instrumentation-MySqlData")] | ||
internal class EventCounterListenerEventSource : EventSource | ||
{ | ||
public static readonly EventCounterListenerEventSource Log = new EventCounterListenerEventSource(); | ||
|
||
[Event(2, Message = "Error accured while processing eventCounter, EventCounter: {0}, Exception: {2}", Level = EventLevel.Error)] | ||
public void ErrorEventCounter(string counterName, string exception) | ||
{ | ||
this.WriteEvent(1, counterName, exception); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/OpenTelemetry.Contrib.EventCounterListener/EventCounterListenerInstrumentation.cs
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,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace OpenTelemetry.Contrib.Instrumentation.EventCounterListener | ||
{ | ||
internal class EventCounterListenerInstrumentation : IDisposable | ||
{ | ||
|
||
private readonly EventCounterListener eventCounterListener; | ||
|
||
public EventCounterListenerInstrumentation(EventCounterListenerOptions options) | ||
{ | ||
this.eventCounterListener = new EventCounterListener(options); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
this.eventCounterListener.Dispose(); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/OpenTelemetry.Contrib.EventCounterListener/EventCounterListenerOptions.cs
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,37 @@ | ||
// <copyright file="MySqlDataInstrumentationOptions.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.Data; | ||
using System.Diagnostics; | ||
using System.Text.RegularExpressions; | ||
|
||
using OpenTelemetry.Trace; | ||
|
||
namespace OpenTelemetry.Contrib.Instrumentation.EventCounterListener | ||
{ | ||
/// <summary> | ||
/// Options for <see cref="EventCounterListener"/>. | ||
/// </summary> | ||
public class EventCounterListenerOptions | ||
{ | ||
/// <summary> | ||
/// Gets or sets event Counters to listen to. | ||
/// </summary> | ||
public EventCounter[] Sources { get; set; } | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/OpenTelemetry.Contrib.EventCounterListener/EventPipe/CounterPayload.cs
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,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace OpenTelemetry.Contrib.EventCounterListener.EventPipe | ||
{ | ||
internal class CounterPayload : ICounterPayload | ||
{ | ||
public CounterPayload(DateTime timestamp, | ||
string provider, | ||
string name, | ||
string displayName, | ||
string unit, | ||
double value, | ||
CounterType counterType, | ||
float interval) | ||
{ | ||
Timestamp = timestamp; | ||
Name = name; | ||
DisplayName = displayName; | ||
Unit = unit; | ||
Value = value; | ||
CounterType = counterType; | ||
Provider = provider; | ||
Interval = interval; | ||
} | ||
|
||
public string Namespace { get; } | ||
|
||
public string Name { get; } | ||
|
||
public string DisplayName { get; } | ||
|
||
public string Unit { get; } | ||
|
||
public double Value { get; } | ||
|
||
public DateTime Timestamp { get; } | ||
|
||
public float Interval { get; } | ||
|
||
public CounterType CounterType { get; } | ||
|
||
public string Provider { get; } | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can remove this, as we won't need any packages from non nuget.org/myget.org
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed ..