public void StartRemoteChildSpan_WithProbabilitySamplerDefaultSampler()
        {
            var configMock = Mock.Get <ITraceConfig>(traceConfig);

            configMock.Setup((c) => c.ActiveTraceParams).Returns(TraceParams.Default);
            // This traceId will not be sampled by the ProbabilitySampler because the first 8 bytes as long
            // is not less than probability * Long.MAX_VALUE;
            var traceId =
                ActivityTraceId.CreateFromBytes(
                    new byte[] { 0x8F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, });

            // If parent is sampled then the remote child must be sampled.
            var childSpan =
                new SpanBuilder(SpanName, spanBuilderOptions)
                .SetSpanKind(SpanKind.Internal)
                .SetParent(SpanContext.Create(
                               traceId,
                               ActivitySpanId.CreateRandom(),
                               ActivityTraceFlags.Recorded,
                               Tracestate.Empty))
                .StartSpan();

            Assert.True(childSpan.Context.IsValid);
            Assert.Equal(traceId, childSpan.Context.TraceId);
            Assert.True((childSpan.Context.TraceOptions & ActivityTraceFlags.Recorded) != 0);
            childSpan.End();

            Assert.Equal(TraceParams.Default, traceConfig.ActiveTraceParams);

            // If parent is not sampled then the remote child must be not sampled.
            childSpan =
                new SpanBuilder(SpanName, spanBuilderOptions)
                .SetSpanKind(SpanKind.Internal)
                .SetParent(SpanContext.Create(
                               traceId,
                               ActivitySpanId.CreateRandom(),
                               ActivityTraceFlags.None,
                               Tracestate.Empty))
                .StartSpan();

            Assert.True(childSpan.Context.IsValid);
            Assert.Equal(traceId, childSpan.Context.TraceId);
            Assert.True((childSpan.Context.TraceOptions & ActivityTraceFlags.Recorded) == 0);
            childSpan.End();
        }
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            var spanContext = _spanContextExtractor
                              .Extract(context.Request)
                              .Sample(_sampler);

            var spanBuilder = new SpanBuilder(spanContext);

            spanBuilder.Start()
            .Name(context.Request.Method)
            .Kind(SpanKind.Server)
            .Tag("host", context.Request.Host.Value)
            .Tag("resource", context.Request.Path.Value)
            .Tag("method", context.Request.Method)
            .WithLocalEndpoint(new Endpoint
            {
                ServiceName = _localEndpointName
            });

            _spanContextAccessor.SaveContext(spanContext);

            try
            {
                await next(context);
            }
            catch (Exception ex)
            {
                spanBuilder.Error(ex.Message);
            }
            finally
            {
                var span = spanBuilder
                           .End()
                           .Build();

                try
                {
                    _dispatcher.Dispatch(span);
                }
                catch (DispatchException)
                {
                    // ignore
                }
            }
        }