internal Span(SpanContext context, DateTimeOffset?start) : this(context, start, null) { }
/// <summary> /// This is a shortcut for <see cref="StartSpan"/> and <see cref="ActivateSpan"/>, it creates a new span with the given parameters and makes it active. /// </summary> /// <param name="operationName">The span's operation name</param> /// <param name="childOf">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> /// <param name="finishOnClose">If set to false, closing the returned scope will not close the enclosed span </param> /// <returns>A scope wrapping the newly created span</returns> public Scope StartActive(string operationName, SpanContext childOf = null, string serviceName = null, DateTimeOffset?startTime = null, bool ignoreActiveScope = false, bool finishOnClose = true) { var span = StartSpan(operationName, childOf, serviceName, startTime, ignoreActiveScope); return(_scopeManager.Activate(span, finishOnClose)); }
/// <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 if (parent is SpanContext parentSpanContext) { traceContext = parentSpanContext.TraceContext ?? new TraceContext(this) { SamplingPriority = parentSpanContext.SamplingPriority }; } else { 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, }; // Apply any global tags if (Settings.GlobalTags.Count > 0) { foreach (var entry in Settings.GlobalTags) { span.SetTag(entry.Key, entry.Value); } } // automatically add the "env" tag if defined, taking precedence over an "env" tag set from a global tag var env = Settings.Environment; if (!string.IsNullOrWhiteSpace(env)) { span.SetTag(Tags.Env, env); } // automatically add the "version" tag if defined, taking precedence over an "version" tag set from a global tag var version = Settings.ServiceVersion; if (!string.IsNullOrWhiteSpace(version) && string.Equals(finalServiceName, DefaultServiceName)) { span.SetTag(Tags.Version, version); } traceContext.AddSpan(span); return(span); }
private static string ToHeaderValue(SpanContext context) { return(string.Format(ServerTimingFormat, W3CContextPropagator.GetFormattedTraceParent(context))); }
internal Span(SpanContext context, DateTimeOffset?start) { Context = context; ServiceName = context.ServiceName; StartTime = start ?? Context.TraceContext.UtcNow; }
/// <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); }