-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
740 additions
and
0 deletions.
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 | ||
{ | ||
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)); | ||
} | ||
|
||
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.