예제 #1
0
 /// <summary>
 /// Make a span the active span and return its new scope.
 /// </summary>
 /// <param name="span">The span to activate.</param>
 /// <param name="finishOnClose">Determines whether closing the returned scope will also finish the span.</param>
 /// <returns>A Scope object wrapping this span.</returns>
 public Scope ActivateSpan(Span span, bool finishOnClose = true)
 {
     return(_scopeManager.Activate(span, finishOnClose));
 }
예제 #2
0
        /// <summary>
        /// Creates a new <see cref="Span"/> with the specified parameters.
        /// </summary>
        /// <param name="operationName">The span's operation name</param>
        /// <param name="parent">The span's parent</param>
        /// <param name="serviceName">The span's service name</param>
        /// <param name="startTime">An explicit start time for that span</param>
        /// <param name="ignoreActiveScope">If set the span will not be a child of the currently active span</param>
        /// <returns>The newly created span</returns>
        public Span StartSpan(string operationName, ISpanContext parent = null, string serviceName = null, DateTimeOffset?startTime = null, bool ignoreActiveScope = false)
        {
            if (parent == null && !ignoreActiveScope)
            {
                parent = _scopeManager.Active?.Span?.Context;
            }

            ITraceContext traceContext;

            // try to get the trace context (from local spans) or
            // sampling priority (from propagated spans),
            // otherwise start a new trace context
            var isRootSpan = false;

            if (parent is SpanContext parentSpanContext)
            {
                traceContext = parentSpanContext.TraceContext ??
                               new TraceContext(this)
                {
                    SamplingPriority = parentSpanContext.SamplingPriority
                };
            }
            else
            {
                isRootSpan   = true;
                traceContext = new TraceContext(this);
            }

            var finalServiceName = serviceName ?? parent?.ServiceName ?? DefaultServiceName;
            var spanContext      = new SpanContext(parent, traceContext, finalServiceName);

            var span = new Span(spanContext, startTime)
            {
                OperationName = operationName,
            };

            var env = Settings.Environment;

            // automatically add the "env" tag if defined
            if (!string.IsNullOrWhiteSpace(env))
            {
                span.SetTag(Tags.Env, env);
            }

            // Apply root span tags
            if (isRootSpan)
            {
                span.SetTag(Tags.Language, TracerConstants.Language);
                span.SetTag(Tags.Version, TracerConstants.AssemblyVersion);
            }

            // Apply any global tags
            if (Settings.GlobalTags?.Count > 0)
            {
                foreach (var entry in Settings.GlobalTags)
                {
                    span.SetTag(entry.Key, entry.Value);
                }
            }

            traceContext.AddSpan(span);
            return(span);
        }
예제 #3
0
 /// <summary>
 /// Make a span the active span and return its new scope.
 /// </summary>
 /// <param name="span">The span to activate.</param>
 /// <returns>A Scope object wrapping this span.</returns>
 Scope IDatadogTracer.ActivateSpan(Span span)
 {
     return(ActivateSpan(span));
 }