Esempio n. 1
0
 /// <inheritdoc />
 public override TelemetrySpan StartSpan(string operationName, TelemetrySpan parent, SpanKind kind,
                                         SpanCreationOptions options)
 {
     Logger.Log(
         $"{prefix}.StartSpan({operationName}, {parent.GetType().Name}, {kind}, {options.StartTimestamp:o}, {options.LinksFactory}, {options.Links})");
     return(new LoggingSpan(operationName, kind));
 }
        public static SpanData CreateSpanData(string name,
                                              SpanContext parentContext,
                                              SpanKind kind,
                                              DateTimeOffset startTimestamp,
                                              IEnumerable <Link> links,
                                              IDictionary <string, object> attributes,
                                              IEnumerable <Event> events,
                                              Status status,
                                              DateTimeOffset endTimestamp)
        {
            var processor = new Mock <SpanProcessor>();

            processor.Setup(p => p.OnEnd(It.IsAny <SpanData>()));

            var tracer = TracerFactory.Create(b =>
                                              b.AddProcessorPipeline(p =>
                                                                     p.AddProcessor(_ => processor.Object)))
                         .GetTracer(null);

            SpanCreationOptions spanOptions = null;

            if (links != null || attributes != null || startTimestamp != default)
            {
                spanOptions = new SpanCreationOptions
                {
                    Links          = links,
                    Attributes     = attributes,
                    StartTimestamp = startTimestamp,
                };
            }
            var span = tracer.StartSpan(name, parentContext, kind, spanOptions);

            if (events != null)
            {
                foreach (var evnt in events)
                {
                    span.AddEvent(evnt);
                }
            }

            span.Status = status.IsValid ? status : Status.Ok;

            if (endTimestamp == default)
            {
                span.End();
            }
            else
            {
                span.End(endTimestamp);
            }

            return((SpanData)processor.Invocations[0].Arguments[0]);
        }
Esempio n. 3
0
        async Task ProcessItem(HelloRequest request)
        {
            async Task <(Activity Activity, string Message)> OpenTelemetrySayHello(DateTime start, string city)
            {
                var res = await RawSayHello(start, city);

                var span = tracer.StartSpanFromActivity(res.Activity.OperationName, res.Activity, SpanKind.Consumer);

                span.End();

                return(res);
            }

            async Task <(Activity Activity, string Message)> ApplicationInsightsSayHello(DateTime start, string city)
            {
                var res = await RawSayHello(start, city);

                var operation = telemetryClient.StartOperation <RequestTelemetry>(res.Activity.OperationName, res.Activity.TraceId.ToString(), res.Activity.SpanId.ToString());

                telemetryClient.StopOperation(operation);

                return(res);
            }

            async Task <(Activity Activity, string Message)> RawSayHello(DateTime start, string city)
            {
                var activity = new Activity("Single Say Hello").Start();

                activity.AddBaggage("city", city);
                await Task.Delay(10);

                return(activity, $"{start}: Hello {city}");
            }

            Func <DateTime, string, Task <(Activity Activity, string Message)> > runner = null;

            if (tracer != null)
            {
                runner = OpenTelemetrySayHello;
            }
            else if (telemetryClient != null)
            {
                runner = ApplicationInsightsSayHello;
            }

            if (runner == null)
            {
                return;
            }

            var batchStart = DateTimeOffset.UtcNow;
            var tasks      = new List <Task <(Activity Activity, string Message)> >();

            foreach (var v in request.Cities)
            {
                tasks.Add(Task.Run(() => runner(request.RequestTime, v)));
            }


            await Task.WhenAll(tasks);

            if (this.tracer != null)
            {
                var opts = new SpanCreationOptions
                {
                    StartTimestamp = batchStart,
                    Links          = tasks.Select(x => new Link(ExtractContext(x.Result.Activity))),
                };

                tracer.StartActiveSpan("Say Hello batch processing", SpanKind.Consumer, opts, out var batchSpan);
                batchSpan.End();
            }
            else if (telemetryClient != null)
            {
                var links = tasks.Select(x => new ApplicationInsightsLink(x.Result.Activity));
                using var batchOperation = telemetryClient.StartOperation <RequestTelemetry>("Say Hello batch processing");

                batchOperation.Telemetry.Timestamp = batchStart;

                batchOperation.Telemetry.Properties[ApplicationInsightsLink.TelemetryPropertyName] = System.Text.Json.JsonSerializer.Serialize(links);
            }
        }
Esempio n. 4
0
 /// <inheritdoc/>
 public ISpan StartRootSpan(string operationName, SpanKind kind, SpanCreationOptions options)
 {
     Logger.Log($"{this.prefix}.StartRootSpan({operationName}, {kind}, {options.StartTimestamp:o}, {options.LinksFactory}, {options.Links})");
     return(new LoggingSpan(operationName, kind));
 }