Пример #1
0
        private bool IsConfigValuesNull(IZipkinConfig zipkinConfig)
        {
            if (String.IsNullOrEmpty(zipkinConfig.ZipkinServerName))
            {
                logger.Error("zipkinConfig.ZipkinServerName is null");
                return(true);
            }

            if (String.IsNullOrEmpty(zipkinConfig.ZipkinServerPort))
            {
                logger.Error("zipkinConfig.ZipkinServerPort is null");
                return(true);
            }

            if (String.IsNullOrEmpty(zipkinConfig.ServiceName))
            {
                logger.Error("zipkinConfig.ServiceName value is null");
                return(true);
            }

            if (String.IsNullOrEmpty(zipkinConfig.SpanProcessorBatchSize))
            {
                logger.Error("zipkinConfig.SpanProcessorBatchSize value is null");
                return(true);
            }

            if (String.IsNullOrEmpty(zipkinConfig.ZipkinSampleRate))
            {
                logger.Error("zipkinConfig.ZipkinSampleRate value is null");
                return(true);
            }
            return(false);
        }
        private ITracerClient SetupZipkinClient(IZipkinConfig zipkinConfig = null)
        {
            spanCollectorStub = MockRepository.GenerateStub <SpanCollector>(new Uri("http://localhost"), (uint)0, logger);

            traceProvider.Stub(x => x.TraceId).Return(fixture.Create <string>());
            traceProvider.Stub(x => x.SpanId).Return(fixture.Create <string>());
            traceProvider.Stub(x => x.ParentSpanId).Return(fixture.Create <string>());
            traceProvider.Stub(x => x.IsSampled).Return(true);

            var context = MockRepository.GenerateStub <IOwinContext>();
            var request = MockRepository.GenerateStub <IOwinRequest>();

            context.Stub(x => x.Request).Return(request);
            context.Stub(x => x.Environment).Return(new Dictionary <string, object> {
                { TraceProvider.Key, traceProvider }
            });

            IZipkinConfig zipkinConfigSetup = zipkinConfig;

            if (zipkinConfig == null)
            {
                zipkinConfigSetup = CreateZipkinConfigWithDefaultValues();
            }

            return(new ZipkinClient(logger, zipkinConfigSetup, context, spanCollectorStub));
        }
        public ZipkinClient(ITraceProvider traceProvider, string requestName, IMDLogger logger, IZipkinConfig zipkinConfig, ISpanCollectorBuilder spanCollectorBuilder)
        {
            this.logger = logger;
            isTraceOn = true;

            if ( logger == null || IsConfigValuesNull(zipkinConfig) || !IsConfigValuesValid(zipkinConfig) || !IsTraceProviderValidAndSamplingOn(traceProvider))
            {
                isTraceOn = false;
            }

            if (isTraceOn)
            {
                try
                {
                    spanCollector = spanCollectorBuilder.Build(zipkinConfig.ZipkinServerName, int.Parse(zipkinConfig.ZipkinServerPort), int.Parse(zipkinConfig.SpanProcessorBatchSize));
                    spanCollector.Start();

                    spanTracer = new SpanTracer(spanCollector, zipkinConfig.ServiceName, new ServiceEndpoint());

                    this.requestName = requestName;
                    this.traceProvider = traceProvider;
                }
                catch (Exception ex)
                {
                    logger.Error("Error Building Zipkin Client Provider", ex);
                    isTraceOn = false;
                }
            }
        }
Пример #4
0
        public ZipkinClient(ILog logger, IZipkinConfig zipkinConfig, IOwinContext context, SpanCollector collector = null)
        {
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            if (zipkinConfig == null)
            {
                throw new ArgumentNullException(nameof(zipkinConfig));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var traceProvider = new TraceProvider(zipkinConfig, context);

            IsTraceOn = !zipkinConfig.Bypass(context.Request) && IsTraceProviderSamplingOn(traceProvider);

            if (!IsTraceOn)
            {
                return;
            }

            zipkinConfig.Validate();
            ZipkinConfig = zipkinConfig;
            this.logger  = logger;

            try
            {
                spanCollector = collector ?? SpanCollector.GetInstance(
                    zipkinConfig.ZipkinBaseUri,
                    zipkinConfig.SpanProcessorBatchSize,
                    logger);

                spanTracer = new SpanTracer(
                    spanCollector,
                    new ServiceEndpoint(),
                    zipkinConfig.NotToBeDisplayedDomainList,
                    zipkinConfig.Domain);

                TraceProvider = traceProvider;
            }
            catch (Exception ex)
            {
                logger.Error("Error Building Zipkin Client Provider", ex);
                IsTraceOn = false;
            }
        }
Пример #5
0
        private bool IsConfigValuesValid(IZipkinConfig zipkinConfig)
        {
            int port;
            int spanProcessorBatchSize;

            if (!int.TryParse(zipkinConfig.ZipkinServerPort, out port))
            {
                logger.Error("zipkinConfig port is not an int");
                return(false);
            }

            if (!int.TryParse(zipkinConfig.SpanProcessorBatchSize, out spanProcessorBatchSize))
            {
                logger.Error("zipkinConfig spanProcessorBatchSize is not an int");
                return(false);
            }
            return(true);
        }
Пример #6
0
        // TODO: Make another constructor to accept System.Web.HttpContext for non Owin applications
        /// <summary>
        /// Initializes a new instance of the TraceProvider class.
        /// </summary>
        /// <param name="config">ZipkinConfig instance</param>
        /// <param name="context">the IOwinContext</param>
        internal TraceProvider(IZipkinConfig config, HttpContext context = null)
        {
            string headerTraceId      = null;
            string headerSpanId       = null;
            string headerParentSpanId = null;
            string headerSampled      = null;
            string requestPath        = null;

            if (context != null)
            {
                object value = context.Items[Key];
                if (value != null)
                {
                    // set properties from context's item.
                    var provider = (ITraceProvider)value;
                    TraceId      = provider.TraceId;
                    SpanId       = provider.SpanId;
                    ParentSpanId = provider.ParentSpanId;
                    IsSampled    = provider.IsSampled;
                    return;
                }

                // zipkin use the following X-Headers to propagate the trace information
                headerTraceId      = context.Request.Headers[TraceIdHeaderName];
                headerSpanId       = context.Request.Headers[SpanIdHeaderName];
                headerParentSpanId = context.Request.Headers[ParentSpanIdHeaderName];
                headerSampled      = context.Request.Headers[SampledHeaderName];

                requestPath = context.Request.Path.ToString();
            }

            TraceId      = headerTraceId.IsParsableTo128Or64Bit() ? headerTraceId : GenerateNewTraceId(config.Create128BitTraceId);
            SpanId       = headerSpanId.IsParsableToLong() ? headerSpanId : GenerateHexEncodedInt64Id();
            ParentSpanId = headerParentSpanId.IsParsableToLong() ? headerParentSpanId : string.Empty;
            IsSampled    = config.ShouldBeSampled(headerSampled, requestPath);

            if (SpanId == ParentSpanId)
            {
                throw new ArgumentException("x-b3-SpanId and x-b3-ParentSpanId must not be the same value.");
            }

            context?.Items.Add(Key, this);
        }
Пример #7
0
        /// <summary>
        /// Initializes a new instance of the TraceProvider class.
        /// </summary>
        /// <param name="config">ZipkinConfig instance</param>
        /// <param name="context">the IOwinContext</param>
        internal TraceProvider(IZipkinConfig config, IOwinContext context = null)
        {
            string headerTraceId      = null;
            string headerSpanId       = null;
            string headerParentSpanId = null;
            string headerSampled      = null;

            if (context != null)
            {
                object value;
                if (context.Environment.TryGetValue(Key, out value))
                {
                    // set properties from context's item.
                    var provider = (ITraceProvider)value;
                    TraceId      = provider.TraceId;
                    SpanId       = provider.SpanId;
                    ParentSpanId = provider.ParentSpanId;
                    IsSampled    = provider.IsSampled;
                    return;
                }

                // zipkin use the following X-Headers to propagate the trace information
                headerTraceId      = context.Request.Headers[TraceIdHeaderName];
                headerSpanId       = context.Request.Headers[SpanIdHeaderName];
                headerParentSpanId = context.Request.Headers[ParentSpanIdHeaderName];
                headerSampled      = context.Request.Headers[SampledHeaderName];
            }

            TraceId      = Parse(headerTraceId) ? headerTraceId : GenerateHexEncodedInt64FromNewGuid();
            SpanId       = Parse(headerSpanId) ? headerSpanId : TraceId;
            ParentSpanId = Parse(headerParentSpanId) ? headerParentSpanId : string.Empty;
            IsSampled    = config.ShouldBeSampled(context, headerSampled);

            if (SpanId == ParentSpanId)
            {
                throw new ArgumentException("x-b3-SpanId and x-b3-ParentSpanId must not be the same value.");
            }

            context?.Environment.Add(Key, this);
        }
Пример #8
0
        public ZipkinClient(ITraceProvider traceProvider, string requestName, IMDLogger logger, IZipkinConfig zipkinConfig, ISpanCollectorBuilder spanCollectorBuilder)
        {
            this.logger = logger;
            isTraceOn   = true;

            if (logger == null || IsConfigValuesNull(zipkinConfig) || !IsConfigValuesValid(zipkinConfig) || !IsTraceProviderValidAndSamplingOn(traceProvider))
            {
                isTraceOn = false;
            }

            if (isTraceOn)
            {
                try
                {
                    spanCollector = spanCollectorBuilder.Build(zipkinConfig.ZipkinServerName, int.Parse(zipkinConfig.ZipkinServerPort), int.Parse(zipkinConfig.SpanProcessorBatchSize));
                    spanCollector.Start();

                    spanTracer = new SpanTracer(spanCollector, zipkinConfig.ServiceName, new ServiceEndpoint());

                    this.requestName   = requestName;
                    this.traceProvider = traceProvider;
                }
                catch (Exception ex)
                {
                    logger.Error("Error Building Zipkin Client Provider", ex);
                    isTraceOn = false;
                }
            }
        }
 public static void UseZipkin(this IAppBuilder app, IZipkinConfig config)
 {
     config.Validate();
     app.Use <ZipkinMiddleware>(config);
 }
 public ZipkinMiddleware(OwinMiddleware next, IZipkinConfig options) : base(next)
 {
     _config = options;
 }
        private bool IsConfigValuesValid(IZipkinConfig zipkinConfig)
        {
            int port;
            int spanProcessorBatchSize;
            if (!int.TryParse(zipkinConfig.ZipkinServerPort, out port))
            {
                logger.Error("zipkinConfig port is not an int");
                return false;
            }

            if (!int.TryParse(zipkinConfig.SpanProcessorBatchSize, out spanProcessorBatchSize))
            {
                logger.Error("zipkinConfig spanProcessorBatchSize is not an int");
                return false;
            }
            return true;
        }
        private bool IsConfigValuesNull(IZipkinConfig zipkinConfig)
        {
            if (String.IsNullOrEmpty(zipkinConfig.ZipkinServerName))
            {
                logger.Error("zipkinConfig.ZipkinServerName is null");
                return true;
            }

            if (String.IsNullOrEmpty(zipkinConfig.ZipkinServerPort))
            {
                logger.Error("zipkinConfig.ZipkinServerPort is null");
                return true;
            }

            if (String.IsNullOrEmpty(zipkinConfig.ServiceName))
            {
                logger.Error("zipkinConfig.ServiceName value is null");
                return true;
            }

            if (String.IsNullOrEmpty(zipkinConfig.SpanProcessorBatchSize))
            {
                logger.Error("zipkinConfig.SpanProcessorBatchSize value is null");
                return true;
            }

            if (String.IsNullOrEmpty(zipkinConfig.ZipkinSampleRate))
            {
                logger.Error("zipkinConfig.ZipkinSampleRate value is null");
                return true;
            }
            return false;
        }