You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by li...@apache.org on 2022/06/28 09:19:27 UTC

[rocketmq-client-csharp] branch observability updated: add MeterProvider

This is an automated email from the ASF dual-hosted git repository.

lizhanhui pushed a commit to branch observability
in repository https://gitbox.apache.org/repos/asf/rocketmq-client-csharp.git


The following commit(s) were added to refs/heads/observability by this push:
     new 794a64d  add MeterProvider
794a64d is described below

commit 794a64de287bc466854ead1b6713b24fdf28ce75
Author: Li Zhanhui <li...@gmail.com>
AuthorDate: Tue Jun 28 17:19:18 2022 +0800

    add MeterProvider
---
 rocketmq-client-csharp/Client.cs                   |  4 ++-
 rocketmq-client-csharp/Producer.cs                 | 35 ++++++++++++++++++----
 .../rocketmq-client-csharp.csproj                  |  2 +-
 3 files changed, 33 insertions(+), 8 deletions(-)

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 @@ namespace Org.Apache.Rocketmq
         
         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;
 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 @@ namespace Org.Apache.Rocketmq
         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 @@ namespace Org.Apache.Rocketmq
         {
             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 @@ namespace Org.Apache.Rocketmq
 
         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>