/// <summary> /// Initializes a new instance of the <see cref="SpanContext"/> class /// that is the child of the specified parent context. /// </summary> /// <param name="parent">The parent context.</param> /// <param name="traceContext">The trace context.</param> /// <param name="serviceName">The service name to propagate to child spans.</param> /// <param name="traceId">Override the trace id if there's no parent.</param> /// <param name="spanId">The propagated span id.</param> internal SpanContext(ISpanContext parent, TraceContext traceContext, string serviceName, ulong?traceId = null, ulong?spanId = null) : this(parent?.TraceId ?? traceId, serviceName) { SpanId = spanId ?? SpanIdGenerator.ThreadInstance.CreateNew(); Parent = parent; TraceContext = traceContext; if (parent is SpanContext spanContext) { Origin = spanContext.Origin; } }
/// <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, }; var env = Settings.Environment; // automatically add the "env" tag if defined if (!string.IsNullOrWhiteSpace(env)) { span.SetTag(Tags.Env, env); } // 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); }
internal SpanContext(IDatadogTracer tracer, SpanContext parent, string serviceName) { if (parent != null) { Parent = parent; TraceId = parent.TraceId; // TraceContext may be null if SpanContext was extracted from another process context TraceContext = parent.TraceContext ?? new TraceContext(tracer); } else { TraceId = _random.Value.NextUInt63(); TraceContext = new TraceContext(tracer); } SpanId = _random.Value.NextUInt63(); ServiceName = serviceName ?? parent?.ServiceName ?? tracer.DefaultServiceName; }
/// <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, }; // stspatch Process currentProcessInfo = System.Diagnostics.Process.GetCurrentProcess(); var processStartTime = currentProcessInfo.StartTime; if (!Settings.GlobalTags.ContainsKey(Tags.StsStartTime)) { TimeSpan processStartTimeSpan = (processStartTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)); var startTimeMilliseconds = Convert.ToUInt64(Math.Truncate(processStartTimeSpan.TotalMilliseconds)); Settings.GlobalTags.Add(Tags.StsStartTime, startTimeMilliseconds.ToString()); } if (!Settings.GlobalTags.ContainsKey(Tags.StsPid)) { Settings.GlobalTags.Add(Tags.StsPid, currentProcessInfo.Id.ToString()); } if (!Settings.GlobalTags.ContainsKey(Tags.StsOrigin)) { Settings.GlobalTags.Add(Tags.StsOrigin, "dotnet-traceclient"); } if (!Settings.GlobalTags.ContainsKey(Tags.StsHostname) && !string.IsNullOrEmpty(Environment.MachineName)) { Settings.GlobalTags.Add(Tags.StsHostname, Environment.MachineName); } // /stspatch // 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); }