Esempio n. 1
0
        private Span(
            string name,
            SpanContext parentSpanContext,
            ActivityAndTracestate activityAndTracestate,
            bool ownsActivity,
            SpanKind spanKind,
            SpanCreationOptions spanCreationOptions,
            TracerConfiguration tracerConfiguration,
            SpanProcessor spanProcessor,
            Resource libraryResource)
        {
            this.Name            = name;
            this.LibraryResource = libraryResource;

            IEnumerable <Link> links = null;

            if (spanCreationOptions != null)
            {
                links = spanCreationOptions.Links ?? spanCreationOptions.LinksFactory?.Invoke();
                this.startTimestamp = spanCreationOptions.StartTimestamp;
            }

            if (this.startTimestamp == default)
            {
                this.startTimestamp = PreciseTimestamp.GetUtcNow();
            }

            this.tracerConfiguration = tracerConfiguration;
            this.spanProcessor       = spanProcessor;
            this.Kind         = spanKind;
            this.OwnsActivity = ownsActivity;
            this.Activity     = activityAndTracestate.Activity;

            var tracestate = activityAndTracestate.Tracestate;

            this.IsRecording = MakeSamplingDecision(
                parentSpanContext,
                name,
                null,
                links, // we'll enumerate again, but double enumeration over small collection is cheaper than allocation
                this.Activity.TraceId,
                this.Activity.SpanId,
                this.tracerConfiguration);

            if (this.IsRecording)
            {
                this.Activity.ActivityTraceFlags |= ActivityTraceFlags.Recorded;

                this.SetLinks(links);
                this.spanProcessor.OnStart(this);
            }
            else
            {
                this.Activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded;
            }

            // this context is definitely not remote, setting isRemote to false
            this.Context = new SpanContext(this.Activity.TraceId, this.Activity.SpanId, this.Activity.ActivityTraceFlags, false, tracestate);
        }
        /// <inheritdoc/>
        public ISpan Start()
        {
            TelemetrySpan span = null;

            SpanCreationOptions options = null;

            if (this.explicitStartTime != null || this.links != null)
            {
                options = new SpanCreationOptions
                {
                    StartTimestamp = this.explicitStartTime ?? default,
                    Links          = this.links,
                };
            }

            // If specified, this takes precedence.
            if (this.ignoreActiveSpan)
            {
                span = this.tracer.StartRootSpan(this.spanName, this.spanKind, options);
            }
            else if (this.parentSpan != null)
            {
                span = this.tracer.StartSpan(this.spanName, this.parentSpan, this.spanKind, options);
            }
            else if (this.parentSpanContext.IsValid)
            {
                span = this.tracer.StartSpan(this.spanName, this.parentSpanContext, this.spanKind, options);
            }
            else if (this.parentSpan == null && !this.parentSpanContext.IsValid && (this.tracer.CurrentSpan == null || !this.tracer.CurrentSpan.Context.IsValid))
            {
                // We need to know if we should inherit an existing Activity-based context or start a new one.
                if (System.Diagnostics.Activity.Current != null && System.Diagnostics.Activity.Current.IdFormat == System.Diagnostics.ActivityIdFormat.W3C)
                {
                    var currentActivity = System.Diagnostics.Activity.Current;
                    if (this.rootOperationNamesForActivityBasedAutoAdapters.Contains(currentActivity.OperationName))
                    {
                        this.tracer.StartSpanFromActivity(this.spanName, currentActivity, this.spanKind, this.links);
                        span = this.tracer.CurrentSpan;
                    }
                }
            }

            if (span == null)
            {
                span = this.tracer.StartSpan(this.spanName, null, this.spanKind, options);
            }

            foreach (var kvp in this.attributes)
            {
                span.SetAttribute(kvp.Key, kvp.Value);
            }

            if (this.error)
            {
                span.Status = Trace.Status.Unknown;
            }

            return(new SpanShim(span));
        }
Esempio n. 3
0
        public ISpan StartSpan(string operationName, ISpan parent, SpanKind kind, SpanCreationOptions options)
        {
            if (operationName == null)
            {
                throw new ArgumentNullException(nameof(operationName));
            }

            return(this.realTracer != null?this.realTracer.StartSpan(operationName, parent, kind, options) : BlankSpan.Instance);
        }
Esempio n. 4
0
        /// <inheritdoc/>
        public ISpan StartRootSpan(string operationName, SpanKind kind, SpanCreationOptions options)
        {
            if (operationName == null)
            {
                throw new ArgumentNullException(nameof(operationName));
            }

            return(Span.CreateRoot(operationName, kind, options, this.sampler, this.tracerConfiguration, this.spanProcessor, this.LibraryResource));
        }
Esempio n. 5
0
        /// <inheritdoc/>
        public ISpan StartSpan(string operationName, ISpan parent, SpanKind kind, SpanCreationOptions options)
        {
            if (parent == null)
            {
                parent = this.CurrentSpan;
            }

            return(Span.CreateFromParentSpan(operationName, parent, kind, options, this.sampler, this.tracerConfiguration,
                                             this.spanProcessor, this.LibraryResource));
        }
 /// <summary>
 /// Starts active span.
 /// </summary>
 /// <param name="tracer">Tracer instance.</param>
 /// <param name="operationName">Span name.</param>
 /// <param name="kind">Kind.</param>
 /// <param name="options">Advanced span creation options.</param>
 /// <param name="span">Created span.</param>
 /// <returns>Scope.</returns>
 public static IDisposable StartActiveSpan(this Tracer tracer, string operationName, SpanKind kind, SpanCreationOptions options, out ISpan span)
 {
     span = tracer.StartSpan(operationName, kind, options);
     return(tracer.WithSpan(span, true));
 }
Esempio n. 7
0
        private Span(
            string name,
            SpanContext parentSpanContext,
            ActivityAndTracestate activityAndTracestate,
            bool createdFromActivity,
            SpanKind spanKind,
            SpanCreationOptions spanCreationOptions,
            Sampler sampler,
            TracerConfiguration tracerConfiguration,
            SpanProcessor spanProcessor,
            Resource libraryResource)
        {
            if (name != null)
            {
                this.Name = name;
            }
            else
            {
                OpenTelemetrySdkEventSource.Log.InvalidArgument("StartSpan", "span name is null");
                this.Name = string.Empty;
            }

            this.LibraryResource = libraryResource;

            IEnumerable <Link> links = null;

            if (spanCreationOptions != null)
            {
                links = spanCreationOptions.Links ?? spanCreationOptions.LinksFactory?.Invoke();
                this.startTimestamp = spanCreationOptions.StartTimestamp;
            }

            if (this.startTimestamp == default)
            {
                this.startTimestamp = PreciseTimestamp.GetUtcNow();
            }

            this.sampler             = sampler;
            this.tracerConfiguration = tracerConfiguration;
            this.spanProcessor       = spanProcessor;
            this.Kind = spanKind;
            this.createdFromActivity = createdFromActivity;
            this.Activity            = activityAndTracestate.Activity;
            var tracestate = activityAndTracestate.Tracestate;

            this.IsRecording = MakeSamplingDecision(
                parentSpanContext,
                name,
                spanCreationOptions?.Attributes,
                links, // we'll enumerate again, but double enumeration over small collection is cheaper than allocation
                this.Activity.TraceId,
                this.Activity.SpanId,
                this.sampler);

            this.Activity.ActivityTraceFlags =
                this.IsRecording
                ? this.Activity.ActivityTraceFlags |= ActivityTraceFlags.Recorded
                : this.Activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded;

            // this context is definitely not remote, setting isRemote to false
            this.Context = new SpanContext(this.Activity.TraceId, this.Activity.SpanId, this.Activity.ActivityTraceFlags, false, tracestate);

            if (this.IsRecording)
            {
                this.SetLinks(links);

                if (spanCreationOptions?.Attributes != null)
                {
                    foreach (var attribute in spanCreationOptions.Attributes)
                    {
                        this.SetAttribute(attribute);
                    }
                }

                this.spanProcessor.OnStart(this);
            }
        }
Esempio n. 8
0
 /// <inheritdoc/>
 public override ISpan StartRootSpan(string operationName, SpanKind kind, SpanCreationOptions options)
 {
     return(Span.CreateRoot(operationName, kind, options, this.sampler, this.tracerConfiguration, this.spanProcessor, this.LibraryResource));
 }
Esempio n. 9
0
 /// <summary>
 /// Starts span.
 /// </summary>
 /// <param name="operationName">Span name.</param>
 /// <param name="parent">Parent for new span.</param>
 /// <param name="kind">Kind.</param>
 /// <param name="options">Advanced span creation options.</param>
 /// <returns>Span instance.</returns>
 public abstract TelemetrySpan StartSpan(string operationName, TelemetrySpan parent, SpanKind kind, SpanCreationOptions options);
Esempio n. 10
0
 public override ISpan StartSpan(string operationName, ISpan parent, SpanKind kind, SpanCreationOptions options)
 {
     return(this.realTracer != null?this.realTracer.StartSpan(operationName, parent, kind, options) : BlankSpan.Instance);
 }
Esempio n. 11
0
 /// <summary>
 /// Starts span. If there is active current span, it becomes a parent for returned span.
 /// </summary>
 /// <param name="tracer">Tracer instance.</param>
 /// <param name="operationName">Span name.</param>
 /// <param name="kind">Kind.</param>
 /// <param name="options">Advanced span creation options.</param>
 /// <returns>Span instance.</returns>
 public static ISpan StartSpan(this ITracer tracer, string operationName, SpanKind kind, SpanCreationOptions options)
 {
     return(tracer.StartSpan(operationName, null, kind, options));
 }
Esempio n. 12
0
 public override TelemetrySpan StartRootSpan(string operationName, SpanKind kind, SpanCreationOptions options)
 {
     return(this.realTracer != null?this.realTracer.StartRootSpan(operationName, kind, options) : BlankSpan.Instance);
 }
Esempio n. 13
0
        /// <inheritdoc/>
        public override TelemetrySpan StartSpan(string operationName, TelemetrySpan parentSpan, SpanKind kind, SpanCreationOptions options)
        {
            if (parentSpan == null)
            {
                parentSpan = this.CurrentSpan;
            }

            return(SpanSdk.CreateFromParentSpan(
                       operationName,
                       parentSpan,
                       kind,
                       options,
                       this.sampler,
                       this.tracerConfiguration,
                       this.spanProcessor,
                       this.LibraryResource));
        }
        // TODO: add sampling hints

        /// <summary>
        /// Starts root span.
        /// </summary>
        /// <param name="operationName">Span name.</param>
        /// <param name="kind">Kind.</param>
        /// <param name="options">Advanced span creation options.</param>
        /// <returns>Span instance.</returns>
        public abstract ISpan StartRootSpan(string operationName, SpanKind kind, SpanCreationOptions options);
Esempio n. 15
0
 /// <inheritdoc/>
 public ISpan StartSpan(string operationName, SpanKind kind, SpanCreationOptions options)
 {
     return(this.StartSpan(operationName, null, kind, options));
 }