-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathGarnetOpenTelemetryServerMonitor.cs
More file actions
117 lines (108 loc) · 5.86 KB
/
GarnetOpenTelemetryServerMonitor.cs
File metadata and controls
117 lines (108 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Reflection;
using Garnet.server.Metrics.Latency;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
namespace Garnet.server.Metrics
{
/// <summary>
/// Registers OpenTelemetry metrics for Garnet server and manages their lifecycle. This includes server-level metrics, session-level metrics, and latency metrics.
/// </summary>
/// <remarks>
/// <para>
/// This class acts as the central coordinator for all OpenTelemetry metrics in the Garnet server.
/// It wraps the raw metrics sources (<see cref="GarnetServerMetrics"/> and <see cref="GarnetSessionMetrics"/>)
/// into OpenTelemetry-compatible instruments exposed via <see cref="System.Diagnostics.Metrics.Meter"/> instances:
/// </para>
/// <list type="bullet">
/// <item><description><see cref="GarnetOpenTelemetryServerMetrics"/> — connection-level metrics (active, received, disposed).</description></item>
/// <item><description><see cref="GarnetOpenTelemetrySessionMetrics"/> — session-level metrics (commands processed, network I/O, cache lookups).</description></item>
/// <item><description><see cref="GarnetOpenTelemetryLatencyMetrics"/> — latency histograms and counters (command latency, bytes/ops per receive call).</description></item>
/// </list>
/// <para>
/// Call <see cref="Start"/> after construction to configure the OTLP exporter and begin metric collection.
/// The exporter endpoint, protocol, timeout, and interval are controlled by the corresponding
/// properties on <see cref="GarnetServerOptions"/>.
/// </para>
/// <para>
/// This class implements <see cref="IDisposable"/>; disposing it tears down all underlying meters
/// and the latency metrics singleton.
/// </para>
/// </remarks>
internal sealed class GarnetOpenTelemetryServerMonitor : IDisposable
{
private readonly GarnetServerOptions options;
private readonly GarnetOpenTelemetryServerMetrics serverMetrics;
private readonly GarnetOpenTelemetrySessionMetrics sessionMetrics;
private MeterProvider meterProvider;
/// <summary>
/// Initializes a new instance of the <see cref="GarnetOpenTelemetryServerMonitor"/> class,
/// creating the OpenTelemetry metric wrappers for server and session metrics and initializing
/// the latency metrics singleton.
/// </summary>
/// <param name="options">
/// The <see cref="GarnetServerOptions"/> that control OpenTelemetry export behavior, including
/// <see cref="GarnetServerOptions.OpenTelemetryEndpoint"/>,
/// <see cref="GarnetServerOptions.OpenTelemetryExportProtocol"/>,
/// <see cref="GarnetServerOptions.OpenTelemetryExportTimeout"/> and
/// <see cref="GarnetServerOptions.OpenTelemetryExportInterval"/>
/// </param>
/// <param name="serverMetrics">
/// The <see cref="GarnetServerMetrics"/> instance that provides raw server and session counters.
/// If <see cref="GarnetServerMetrics.globalSessionMetrics"/> is <c>null</c>, session-level
/// metrics will not be registered.
/// </param>
public GarnetOpenTelemetryServerMonitor(GarnetServerOptions options, GarnetServerMetrics serverMetrics)
{
this.options = options;
this.serverMetrics = new GarnetOpenTelemetryServerMetrics(serverMetrics);
this.sessionMetrics = serverMetrics.globalSessionMetrics != null
? new GarnetOpenTelemetrySessionMetrics(serverMetrics.globalSessionMetrics)
: null;
GarnetOpenTelemetryLatencyMetrics.Initialize(options.LatencyMonitor);
}
/// <summary>
/// Initializes and configures OpenTelemetry metrics exporting if an endpoint is specified in the options.
/// </summary>
/// <remarks>Call this method to enable OpenTelemetry metrics collection and exporting for the
/// service. Metrics will be exported using the configured endpoint and protocol. If no endpoint is specified,
/// metrics exporting will not be enabled.</remarks>
public void Start()
{
if (this.options.OpenTelemetryEndpoint != null)
{
this.meterProvider = Sdk.CreateMeterProviderBuilder()
.ConfigureResource(rb => rb.AddService("Microsoft.Garnet", serviceVersion: Assembly.GetEntryAssembly()?.GetName()?.Version?.ToString() ?? "unknown"))
.AddMeter(GarnetOpenTelemetryServerMetrics.MeterName, GarnetOpenTelemetrySessionMetrics.MeterName, GarnetOpenTelemetryLatencyMetrics.MeterName)
.AddOtlpExporter(opts =>
{
opts.Endpoint = this.options.OpenTelemetryEndpoint;
if (this.options.OpenTelemetryExportProtocol.HasValue)
{
opts.Protocol = this.options.OpenTelemetryExportProtocol.Value;
}
if (this.options.OpenTelemetryExportTimeout != 0)
{
opts.TimeoutMilliseconds = this.options.OpenTelemetryExportTimeout;
}
if (this.options.OpenTelemetryExportInterval != 0)
{
opts.BatchExportProcessorOptions.ScheduledDelayMilliseconds = this.options.OpenTelemetryExportInterval;
}
})
.Build();
}
}
/// <inheritdoc />
public void Dispose()
{
this.serverMetrics.Dispose();
this.sessionMetrics?.Dispose();
GarnetOpenTelemetryLatencyMetrics.DisposeInstance();
this.meterProvider?.Dispose();
}
}
}