add MeterProvider
diff --git a/rocketmq-client-csharp/Client.cs b/rocketmq-client-csharp/Client.cs
index 8347650..32dffae 100644
--- a/rocketmq-client-csharp/Client.cs
+++ b/rocketmq-client-csharp/Client.cs
@@ -488,6 +488,8 @@
protected readonly ConcurrentDictionary<string, Session> _sessions = new ConcurrentDictionary<string, Session>();
- protected static readonly Meter MetricMeter = new("Apache.RocketMQ.Client", "1.0");
+ public static readonly string MeterName = "Apache.RocketMQ.Client";
+
+ protected static readonly Meter MetricMeter = new(MeterName, "1.0");
}
}
\ No newline at end of file
diff --git a/rocketmq-client-csharp/Producer.cs b/rocketmq-client-csharp/Producer.cs
index bd24251..5c51cdc 100644
--- a/rocketmq-client-csharp/Producer.cs
+++ b/rocketmq-client-csharp/Producer.cs
@@ -26,7 +26,9 @@
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using NLog;
+using OpenTelemetry;
using OpenTelemetry.Exporter;
+using OpenTelemetry.Metrics;
namespace Org.Apache.Rocketmq
{
@@ -35,10 +37,8 @@
public Producer(AccessPoint accessPoint, string resourceNamespace) : base(accessPoint, resourceNamespace)
{
_loadBalancer = new ConcurrentDictionary<string, PublishLoadBalancer>();
- _otlpExporterOptions = new OtlpExporterOptions();
- _otlpExporterOptions.Protocol = OtlpExportProtocol.Grpc;
_sendFailureTotal = MetricMeter.CreateCounter<long>("rocketmq_send_failure_total");
- _sendLatency = MetricMeter.CreateHistogram<double>("rocketmq_send_success_cost_time",
+ _sendLatency = MetricMeter.CreateHistogram<double>(SendLatencyName,
description: "Measure the duration of publishing messages to brokers",
unit: "milliseconds");
}
@@ -47,9 +47,30 @@
{
await base.Start();
// More initialization
- _otlpExporterOptions.TimeoutMilliseconds = (int)_clientSettings.RequestTimeout.ToTimeSpan().TotalMilliseconds;
- _otlpExporterOptions.Endpoint = new(_accessPoint.TargetUrl());
// TODO: Add authentication header
+
+ _meterProvider = Sdk.CreateMeterProviderBuilder()
+ .AddMeter("Apache.RocketMQ.Client")
+ .AddOtlpExporter(delegate(OtlpExporterOptions options, MetricReaderOptions readerOptions)
+ {
+ options.Protocol = OtlpExportProtocol.Grpc;
+ options.Endpoint = new Uri(_accessPoint.TargetUrl());
+ options.TimeoutMilliseconds = (int) _clientSettings.RequestTimeout.ToTimeSpan().TotalMilliseconds;
+
+ readerOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 60 * 1000;
+ })
+ .AddView((instrument) =>
+ {
+ if (instrument.Meter.Name == MeterName && instrument.Name == SendLatencyName)
+ {
+ return new ExplicitBucketHistogramConfiguration()
+ {
+ Boundaries = new double[] {1, 5, 10, 20, 50, 200, 500},
+ };
+ }
+ return null;
+ })
+ .Build();
}
public override async Task Shutdown()
@@ -182,6 +203,8 @@
private readonly Counter<long> _sendFailureTotal;
private readonly Histogram<double> _sendLatency;
- private readonly OtlpExporterOptions _otlpExporterOptions;
+
+ private static readonly string SendLatencyName = "rocketmq_send_success_cost_time";
+ private MeterProvider _meterProvider;
}
}
\ No newline at end of file
diff --git a/rocketmq-client-csharp/rocketmq-client-csharp.csproj b/rocketmq-client-csharp/rocketmq-client-csharp.csproj
index 86fa88b..baf103f 100644
--- a/rocketmq-client-csharp/rocketmq-client-csharp.csproj
+++ b/rocketmq-client-csharp/rocketmq-client-csharp.csproj
@@ -13,7 +13,7 @@
<ItemGroup>
<PackageReference Include="Crc32.NET" Version="1.2.0" />
<PackageReference Include="Google.Protobuf" Version="3.19.4" />
- <PackageReference Include="Grpc.Net.Client" Version="2.42.0" />
+ <PackageReference Include="Grpc.Net.Client" Version="2.43.0" />
<PackageReference Include="Grpc.Tools" Version="2.43.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>