borkke / opentracing-csharp-mongo

:no_entry: [DEPRECATED] See details here - https://github.com/opentracing/specification/issues/163
Apache License 2.0
5 stars 3 forks source link

TracingMongoClient多次实例化问题 #11

Closed guochen2 closed 2 years ago

guochen2 commented 3 years ago

TracingMongoClient在多次使用的情况下 (对TracingMongoClient进行Scope注入) 会有多次订阅mongodb事件的问题,建议对MongoClientSettings做单例处理,或者从外部传入一个MongoClientSettings,由外部调用方控制

扩展示例

public static class ClusterBuilderExt
    {
        public static ClusterBuilder AddOpenTracing(this ClusterBuilder builder, ITracer tracer, TracingOptions tracingOptions = null)
        {
            if (tracingOptions == null)
            {
                tracingOptions = new TracingOptions();
            }
            var mongoEventListener = new MongoEventListener(tracer, tracingOptions);
            builder
                  .Subscribe<CommandStartedEvent>(mongoEventListener.StartEventHandler)
                  .Subscribe<CommandSucceededEvent>(mongoEventListener.SuccessEventHandler)
                  .Subscribe<CommandFailedEvent>(mongoEventListener.ErrorEventHandler);
            return builder;
        }
    }

使用方式

clientSettings.ClusterConfigurator = cb => cb.AddOpenTracing(OpenTracing.Util.GlobalTracer.Instance);
borkke commented 2 years ago

Closing because of OpenTracing deprecation