/// <summary>
            /// 开始执行Action
            /// </summary>
            /// <param name="context"></param>
            public void OnActionExecuting(ActionExecutingContext context)
            {
                var          path           = context.HttpContext.Request.Path;
                var          callingHeaders = new TextMapExtractAdapter(context.HttpContext.Request.Headers.ToDictionary(m => m.Key, m => m.Value.ToString()));
                var          spanContex     = _tracer.Extract(BuiltinFormats.HttpHeaders, callingHeaders);
                ISpanBuilder builder        = null;

                if (spanContex != null)
                {
                    builder = _tracer.BuildSpan("中间件Span").AsChildOf(spanContex);
                }
                else
                {
                    builder = _tracer.BuildSpan("中间件Span");
                }
                _scope = builder.StartActive(true);
                _scope.Span.SetOperationName(path);
                // 记录请求信息到span
                foreach (var query in context.HttpContext.Request.Query)
                {
                    _scope.Span.SetTag(query.Key, query.Value);
                }
                if (context.HttpContext.Request.HasFormContentType)
                {
                    foreach (var form in context.HttpContext.Request.Form)
                    {
                        _scope.Span.SetTag(form.Key, form.Value);
                    }
                }
            }
        public async Task Send(PublishContext context, IPipe <PublishContext> next)
        {
            DeimosOptions options = GetDeimosOptions();

            if (options is null || !options.Enabled)
            {
                return;
            }

            if (GlobalTracer.Instance is null)
            {
                return;
            }

            var operationName = $"Publishing Message: {context.DestinationAddress.GetExchangeName()}";

            ISpanBuilder spanBuilder = GlobalTracer.Instance.BuildSpan(operationName)
                                       .AsChildOf(GlobalTracer.Instance.ActiveSpan.Context)
                                       .WithTag("destination.address", context.DestinationAddress?.ToString())
                                       .WithTag("source.address", context.SourceAddress?.ToString())
                                       .WithTag("initiator.id", context.InitiatorId?.ToString())
                                       .WithTag("message.id", context.MessageId?.ToString());

            using (IScope scope = spanBuilder.StartActive())
            {
                GlobalTracer.Instance.Inject(GlobalTracer.Instance.ActiveSpan.Context,
                                             BuiltinFormats.TextMap, new MassTransitTextMapInjectAdapter(context));

                await next.Send(context);
            }
        }
Esempio n. 3
0
        public async Task InvokeAsync(HttpContext context, ITracer tracer)
        {
            // Open Tracing
            ISpanBuilder builder = CreateTracingSpanBuilder("中间件Span", tracer, context.Request);

            using (IScope scope = builder.StartActive(true))
            {
                // Span Name
                scope.Span.SetOperationName(context.Request.Path);
                // 记录请求信息到span
                if (_jaegerOptions != null && _jaegerOptions.QuerySpan)
                {
                    foreach (var query in context.Request.Query)
                    {
                        var value = _jaegerOptions.QueryValueMaxLength <= 0 ? query.Value.ToString() : query.Value.ToString().Substring(0, _jaegerOptions.FormValueMaxLength);
                        scope.Span.SetTag(query.Key, query.Value);
                    }
                }
                if (_jaegerOptions != null && _jaegerOptions.FormSpan && context.Request.HasFormContentType)
                {
                    foreach (var form in context.Request.Form)
                    {
                        var value = _jaegerOptions.FormValueMaxLength <= 0 ? form.Value.ToString() : form.Value.ToString().Substring(0, _jaegerOptions.FormValueMaxLength);
                        scope.Span.SetTag(form.Key, value);
                    }
                }
                //foreach (var form in context.Request.Headers)
                //{
                //    scope.Span.SetTag(form.Key, form.Value);
                //}
            }
            await _next(context);
        }
        public async Task InvokeAsync(HttpContext context, ITracer tracer)
        {
            // Open Tracing
            ISpanBuilder builder = CreateTracingSpanBuilder("中间件Span", tracer, context.Request);

            using (IScope scope = builder.StartActive(true))
            {
                // Span Name
                scope.Span.SetOperationName(context.Request.Path);
                // 记录请求信息到span
                foreach (var query in context.Request.Query)
                {
                    scope.Span.SetTag(query.Key, query.Value);
                }
                if (context.Request.HasFormContentType)
                {
                    foreach (var form in context.Request.Form)
                    {
                        scope.Span.SetTag(form.Key, form.Value);
                    }
                }
                //foreach (var form in context.Request.Headers)
                //{
                //    scope.Span.SetTag(form.Key, form.Value);
                //}
            }
            await _next(context);
        }
        /// <summary>
        /// 调用管道
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="tracer">跟踪器</param>
        /// <returns></returns>
        public async Task InvokeAsync(HttpContext context, ITracer tracer)
        {
            _logger.LogInformation("jaeger调用");
            var path = context.Request.Path;

            if (Path.HasExtension(path))
            {
                await _next(context);
            }
            else
            {
                //接收传入的Headers
                var          callingHeaders = new TextMapExtractAdapter(context.Request.Headers.ToDictionary(m => m.Key, m => m.Value.ToString()));
                var          spanContex     = tracer.Extract(BuiltinFormats.HttpHeaders, callingHeaders);
                ISpanBuilder builder        = null;
                if (spanContex != null)
                {
                    builder = tracer.BuildSpan("中间件Span").AsChildOf(spanContex);
                }
                else
                {
                    builder = tracer.BuildSpan("中间件Span");
                }
                //开始设置Span
                using (IScope scope = builder.StartActive(true))
                {
                    scope.Span.SetOperationName(path);
                    // 记录请求信息到span
                    if (_jaegerOptions.IsQuerySpan)
                    {
                        foreach (var query in context.Request.Query)
                        {
                            //包含敏感词跳出
                            if (_jaegerOptions.NoSpanKeys.Contains(query.Key))
                            {
                                continue;
                            }
                            var value = query.Value.ToString().Length > _jaegerOptions.QueryValueMaxLength ? query.Value.ToString()?.Substring(0, _jaegerOptions.QueryValueMaxLength) : query.Value.ToString();
                            scope.Span.SetTag(query.Key, value);
                        }
                    }
                    if (_jaegerOptions.IsFormSpan && context.Request.HasFormContentType)
                    {
                        foreach (var form in context.Request.Form)
                        {
                            //包含敏感词跳出
                            if (_jaegerOptions.NoSpanKeys.Contains(form.Key))
                            {
                                continue;
                            }
                            var value = form.Value.ToString().Length > _jaegerOptions.FormValueMaxLength ? form.Value.ToString()?.Substring(0, _jaegerOptions.FormValueMaxLength) : form.Value.ToString();
                            scope.Span.SetTag(form.Key, value);
                        }
                    }
                    await _next(context);
                }
            }
        }
Esempio n. 6
0
 public static void ExecuteInScopeAndLogExceptions(
     this ISpanBuilder spanBuilder,
     Action action)
 {
     using (IScope scope = spanBuilder.StartActive())
     {
         scope.Span.LogExceptions(action);
     }
 }
        public OpenTracingExtensionsTests()
        {
            _spanContext = Substitute.For <ISpanContext>();

            _span = Substitute.For <ISpan>();
            _span.Context.Returns(_spanContext);

            _scope = Substitute.For <IScope>();
            _scope.Span.Returns(_span);

            _spanBuilder = Substitute.For <ISpanBuilder>();
            _spanBuilder.AsChildOf(Arg.Any <ISpan>()).Returns(_spanBuilder);
            _spanBuilder.StartActive().Returns(_scope);
            _spanBuilder.StartActive(Arg.Any <bool>()).ReturnsForAnyArgs(_scope);

            _tracer = Substitute.For <ITracer>();
            _tracer.BuildSpan("").ReturnsForAnyArgs(_spanBuilder);
        }
Esempio n. 8
0
        public static IYieldReadyScope StartActiveYieldReadyScope(
            this ISpanBuilder spanBuilder,
            bool finishOnDispose,
            IScopeManager scopeManager)
        {
            IScope startActive = spanBuilder.StartActive(false);

            return(new YieldReadyScope(startActive, finishOnDispose, scopeManager));
        }
Esempio n. 9
0
 public static async Task ExecuteInScopeAndLogExceptionsAsync(
     this ISpanBuilder spanBuilder,
     Func <Task> action)
 {
     using (IScope scope = spanBuilder.StartActive())
     {
         await scope.Span.LogExceptions(action);
     }
 }
Esempio n. 10
0
 public static T ExecuteInScopeAndLogExceptions <T>(
     this ISpanBuilder spanBuilder,
     Func <T> action)
 {
     using (IScope scope = spanBuilder.StartActive())
     {
         return(scope.Span.LogExceptions(action));
     }
 }
Esempio n. 11
0
 public static async Task <T> ExecuteInScopeAsync <T>(
     [NotNull] this ISpanBuilder spanBuilder,
     [NotNull] Func <ISpan, Task <T> > action)
 {
     using (IScope scope = spanBuilder.StartActive(true))
     {
         return(await action(scope.Span).ConfigureAwait(false));
     }
 }
Esempio n. 12
0
 public static T ExecuteInScope <T>(
     [NotNull] this ISpanBuilder spanBuilder,
     [NotNull] Func <T> action)
 {
     using (IScope scope = spanBuilder.StartActive(true))
     {
         return(action());
     }
 }
Esempio n. 13
0
 public static async Task ExecuteInScopeAsync(
     [NotNull] this ISpanBuilder spanBuilder,
     [NotNull] Func <Task> action)
 {
     using (IScope scope = spanBuilder.StartActive(true))
     {
         await action().ConfigureAwait(false);
     }
 }
Esempio n. 14
0
 public static void ExecuteInScope(
     [NotNull] this ISpanBuilder spanBuilder,
     [NotNull] Action action)
 {
     using (IScope scope = spanBuilder.StartActive(true))
     {
         action();
     }
 }
Esempio n. 15
0
        public ISpan TraceOperation(string operationName)
        {
            IScope       activeScope = Tracer.ScopeManager.Active;
            ISpanBuilder spanBuilder = Tracer.BuildSpan(operationName);

            return(activeScope?.Span is null
                       ? spanBuilder.StartActive(true).Span
                       : spanBuilder
                   .AsChildOf(Tracer.ScopeManager.Active.Span)
                   .Start());
        }
Esempio n. 16
0
        private void HandleActivityStart(string eventName, Activity activity, object untypedArg)
        {
            ISpanBuilder spanBuilder = _tracer.BuildSpan(activity.OperationName)
                                       .WithTag(Tags.Component, _listenerName);

            foreach (var tag in activity.Tags)
            {
                spanBuilder.WithTag(tag.Key, tag.Value);
            }

            spanBuilder.StartActive();
        }
Esempio n. 17
0
        public void StartActive()
        {
            string       expectedOperationName = "Testoperation";
            Tracer       tracer  = new Tracer();
            ISpanBuilder builder = tracer.BuildSpan(expectedOperationName);
            IScope       scope   = builder.StartActive();

            string[] actualSpanFields = scope.Span.ToString().Split(";");

            Assert.AreEqual(scope.Span, tracer.ActiveSpan);
            Assert.AreEqual(scope.Span, tracer.ScopeManager.Active.Span);
            Assert.AreEqual(tracer.ActiveSpan, tracer.ScopeManager.Active.Span);
        }
Esempio n. 18
0
 public static void ExecuteTryMethodInScopeAndLogFailures(
     this ISpanBuilder spanBuilder,
     TryMethodDelegate action)
 {
     using (IScope scope = spanBuilder.StartActive())
     {
         action(out bool failed);
         if (failed)
         {
             scope.Span.SetTag(KnownTagNames.Error, true);
         }
     }
 }
Esempio n. 19
0
        public static T ExecuteTryMethodInScopeAndLogFailures <T>(
            this ISpanBuilder spanBuilder,
            TryMethodDelegate <T> action)
        {
            using (IScope scope = spanBuilder.StartActive())
            {
                var result = action(out bool failed);
                if (failed)
                {
                    scope.Span.SetTag(KnownTagNames.Error, true);
                }

                return(result);
            }
        }
        public void BeforeRequest(object request, Context context)
        {
            ISpanBuilder spanBuilder = _tracer.BuildSpan(OperationName)
                                       .WithTag(Tags.SpanKind.Key, Tags.SpanKindClient);

            if (_ignoreActiveSpan)
            {
                spanBuilder.IgnoreActiveSpan();
            }

            // No need to put 'span' in Context, as our ScopeManager
            // will automatically propagate it, even when switching between threads,
            // and will be available when AfterResponse() is called.
            spanBuilder.StartActive(true);
        }
        private void HandleActivityStart(string eventName, Activity activity, object untypedArg, IEnumerable <KeyValuePair <string, string> > tags)
        {
            ISpanBuilder spanBuilder = _tracer.BuildSpan(activity.OperationName)
                                       .WithTag(Tags.Component, _listenerName);

            foreach (var tag in activity.Tags)
            {
                spanBuilder.WithTag(tag.Key, tag.Value);
            }

            if (tags != null)
            {
                foreach (var tag in tags)
                {
                    spanBuilder.WithTag(tag.Key, tag.Value);
                }
            }

            spanBuilder.StartActive();
        }
        public void TraceEnter(
            string methodInfo,
            Tuple <string, string>[] configParameters,
            string[] paramNames,
            object[] paramValues)
        {
            if (!GlobalTracer.IsRegistered())
            {
                // Not locking on this check, it's okay to log a few times
                if (!loggedTraceEnterError)
                {
                    loggedTraceEnterError = true;
                    Trace.TraceError(Error.TracerNotRegisteredOnEnter);
                }

                return;
            }

            ISpanBuilder spanBuilder = GlobalTracer.Instance
                                       .BuildSpan($"{this.name}{methodInfo}");

            // Add arguments (if configured to)
            {
                if (ShouldIncludeArguments(configParameters))
                {
                    for (int paramIndex = 0; paramIndex < paramNames.Length; paramIndex++)
                    {
                        string paramName  = paramNames[paramIndex];
                        object paramValue = paramValues[paramIndex];
                        // TODO: Support other forms of serialization
                        string serializedParamValue = paramValue?.ToString();

                        spanBuilder = spanBuilder
                                      .WithTag(paramName, serializedParamValue);
                    }
                }
            }

            spanBuilder
            .StartActive();
        }
Esempio n. 23
0
        public void AddReference()
        {
            string expectedOperationName_1 = "Testoperation_1";
            string expectedOperationName_2 = "Testoperation_2";
            string expectedOperationName_3 = "Testoperation_3";
            string expectedOperationName_4 = "Testoperation_4";
            //Configure needed to handle dispose and finish of spans
            //Cautions: Test run parallel ->  Portusage needs to vary between tests
            string address      = "localhost";
            int    agentport    = 13339;
            int    reporterport = 13340;

            Tracer tracer = new Tracer();

            tracer.Configure(address, agentport, reporterport);
            IScope first_scope;

            using (IScope scope = tracer.BuildSpan(expectedOperationName_1).StartActive()) {
                first_scope = scope;
                Assert.AreEqual(scope.Span, tracer.ActiveSpan);
            };
            using (IScope scope = tracer.BuildSpan(expectedOperationName_2).StartActive())
            {
                Assert.AreEqual(scope.Span, tracer.ActiveSpan);
                Assert.AreNotEqual(scope.Span, first_scope);
            };
            using (IScope scope = tracer.BuildSpan(expectedOperationName_3).StartActive())
            {
                ISpanBuilder builder = tracer.BuildSpan(expectedOperationName_4);
                using (IScope childScope = builder.StartActive())
                {
                    Assert.AreEqual(childScope.Span, tracer.ActiveSpan);
                    Assert.AreNotEqual(scope.Span, tracer.ActiveSpan);
                    Assert.AreNotEqual(scope.Span, childScope.Span);
                    Assert.IsTrue(tracer.ActiveSpan.Context.SpanId == childScope.Span.Context.SpanId);
                    Assert.IsTrue(tracer.ActiveSpan.Context.TraceId == childScope.Span.Context.TraceId);
                    Assert.IsTrue(tracer.ActiveSpan.Context.TraceId == scope.Span.Context.TraceId);
                    Assert.IsFalse(tracer.ActiveSpan.Context.SpanId == scope.Span.Context.SpanId);
                };
            };
        }
        public async Task InvokeAsync(HttpContext context, ITracer tracer)
        {
            var path = context.Request.Path;

            if (Path.HasExtension(path))
            {
                await _next(context);
            }
            else
            {
                var          callingHeaders = new TextMapExtractAdapter(context.Request.Headers.ToDictionary(m => m.Key, m => m.Value.ToString()));
                var          spanContex     = tracer.Extract(BuiltinFormats.HttpHeaders, callingHeaders);
                ISpanBuilder builder        = null;
                if (spanContex != null)
                {
                    builder = tracer.BuildSpan("中间件Span").AsChildOf(spanContex);
                }
                else
                {
                    builder = tracer.BuildSpan("中间件Span");
                }
                using (IScope scope = builder.StartActive(true))
                {
                    scope.Span.SetOperationName(path);
                    // 记录请求信息到span
                    foreach (var query in context.Request.Query)
                    {
                        scope.Span.SetTag(query.Key, query.Value);
                    }
                    if (context.Request.HasFormContentType)
                    {
                        foreach (var form in context.Request.Form)
                        {
                            scope.Span.SetTag(form.Key, form.Value);
                        }
                    }
                    await _next(context);
                }
            }
        }