public void ProcessEvent_Start() { var opts = GetOptions(); OpenCensusTracing tracing = new OpenCensusTracing(opts, null); var obs = new HttpClientCoreObserver(opts, tracing); var request = GetHttpRequestMessage(); obs.ProcessEvent(HttpClientCoreObserver.START_EVENT, new { Request = request }); Span span = GetCurrentSpan(tracing.Tracer); Assert.NotNull(span); Assert.True(request.Properties.TryGetValue(HttpClientCoreObserver.SPANCONTEXT_KEY, out object context)); HttpClientTracingObserver.SpanContext spanContext = context as HttpClientTracingObserver.SpanContext; Assert.NotNull(spanContext); Assert.Equal(span, spanContext.Active); Assert.NotNull(spanContext.ActiveScope); Assert.Equal("httpclient:/", span.Name); Assert.True(request.Headers.Contains(B3Constants.XB3TraceId)); Assert.True(request.Headers.Contains(B3Constants.XB3SpanId)); Assert.False(request.Headers.Contains(B3Constants.XB3ParentSpanId)); var spanId = request.Headers.GetValues(B3Constants.XB3SpanId).Single(); Assert.Equal(span.Context.SpanId.ToLowerBase16(), spanId); var traceId = request.Headers.GetValues(B3Constants.XB3TraceId).Single(); var expected = GetTraceId(opts, span.Context); Assert.Equal(expected, traceId); if (span.Context.TraceOptions.IsSampled) { Assert.True(request.Headers.Contains(B3Constants.XB3Sampled)); } Assert.False(span.HasEnded); var spanData = span.ToSpanData(); var attributes = spanData.Attributes.AttributeMap; Assert.Equal(SpanKind.Client, span.Kind); Assert.Equal(AttributeValue.StringAttributeValue("http://localhost:5555/"), attributes[SpanAttributeConstants.HttpUrlKey]); Assert.Equal(AttributeValue.StringAttributeValue(HttpMethod.Get.ToString()), attributes[SpanAttributeConstants.HttpMethodKey]); Assert.Equal(AttributeValue.StringAttributeValue("localhost:5555"), attributes[SpanAttributeConstants.HttpHostKey]); Assert.Equal(AttributeValue.StringAttributeValue("/"), attributes[SpanAttributeConstants.HttpPathKey]); Assert.Equal(AttributeValue.StringAttributeValue("Header"), attributes["http.request.TEST"]); }
public void ProcessEvent_Exception_PreviousStarted() { var opts = GetOptions(); OpenCensusTracing tracing = new OpenCensusTracing(opts, null); var obs = new HttpClientCoreObserver(opts, tracing); var request = GetHttpRequestMessage(); obs.ProcessEvent(HttpClientCoreObserver.START_EVENT, new { Request = request }); Span span = AsyncLocalContext.CurrentSpan as Span; Assert.NotNull(span); Assert.True(request.Properties.TryGetValue(HttpClientCoreObserver.SPANCONTEXT_KEY, out object context)); HttpClientTracingObserver.SpanContext spanContext = context as HttpClientTracingObserver.SpanContext; Assert.NotNull(spanContext); Assert.Equal(span, spanContext.Active); Assert.Null(spanContext.Previous); Assert.Equal("httpclient:/", span.Name); var exception = new Exception("Help"); obs.ProcessEvent(HttpClientCoreObserver.EXCEPTION_EVENT, new { Request = request, Exception = exception }); var response = GetHttpResponseMessage(HttpStatusCode.InternalServerError); obs.ProcessEvent(HttpClientCoreObserver.STOP_EVENT, new { Request = request, Response = response, RequestTaskStatus = TaskStatus.RanToCompletion }); Assert.True(span.HasEnded); Assert.Null(AsyncLocalContext.CurrentSpan); Assert.False(request.Properties.TryGetValue(HttpClientCoreObserver.SPANCONTEXT_KEY, out object ctx)); var spanData = span.ToSpanData(); var attributes = spanData.Attributes.AttributeMap; Assert.Equal(AttributeValue.StringAttributeValue(SpanAttributeConstants.ClientSpanKind), attributes[SpanAttributeConstants.SpanKindKey]); Assert.Equal(AttributeValue.StringAttributeValue("http://localhost:5555/"), attributes[SpanAttributeConstants.HttpUrlKey]); Assert.Equal(AttributeValue.StringAttributeValue(HttpMethod.Get.ToString()), attributes[SpanAttributeConstants.HttpMethodKey]); Assert.Equal(AttributeValue.StringAttributeValue("localhost"), attributes[SpanAttributeConstants.HttpHostKey]); Assert.Equal(AttributeValue.StringAttributeValue("/"), attributes[SpanAttributeConstants.HttpPathKey]); Assert.Equal(AttributeValue.StringAttributeValue("Header"), attributes["http.request.TEST"]); Assert.Equal(AttributeValue.StringAttributeValue("Header"), attributes["http.response.TEST"]); Assert.Equal(AttributeValue.LongAttributeValue((long)HttpStatusCode.InternalServerError), attributes[SpanAttributeConstants.HttpStatusCodeKey]); Assert.Equal(AttributeValue.StringAttributeValue(obs.GetExceptionMessage(exception)), attributes[SpanAttributeConstants.ErrorKey]); Assert.Equal(AttributeValue.StringAttributeValue(obs.GetExceptionStackTrace(exception)), attributes[SpanAttributeConstants.ErrorStackTrace]); }
public void ProcessEvent_Stop_PreviousStarted() { var opts = GetOptions(); OpenCensusTracing tracing = new OpenCensusTracing(opts, null); var obs = new HttpClientCoreObserver(opts, tracing); var request = GetHttpRequestMessage(); obs.ProcessEvent(HttpClientCoreObserver.START_EVENT, new { Request = request }); Span span = GetCurrentSpan(tracing.Tracer); Assert.NotNull(span); Assert.True(request.Properties.TryGetValue(HttpClientCoreObserver.SPANCONTEXT_KEY, out object context)); HttpClientTracingObserver.SpanContext spanContext = context as HttpClientTracingObserver.SpanContext; Assert.NotNull(spanContext); Assert.Equal(span, spanContext.Active); Assert.NotNull(spanContext.ActiveScope); Assert.Equal("httpclient:/", span.Name); var response = GetHttpResponseMessage(HttpStatusCode.OK); obs.ProcessEvent(HttpClientCoreObserver.STOP_EVENT, new { Request = request, Response = response, RequestTaskStatus = TaskStatus.RanToCompletion }); Assert.True(span.HasEnded); Assert.False(request.Properties.TryGetValue(HttpClientCoreObserver.SPANCONTEXT_KEY, out object ctx)); var spanData = span.ToSpanData(); var attributes = spanData.Attributes.AttributeMap; Assert.Equal(SpanKind.Client, span.Kind); Assert.Equal(AttributeValue.StringAttributeValue("http://localhost:5555/"), attributes[SpanAttributeConstants.HttpUrlKey]); Assert.Equal(AttributeValue.StringAttributeValue(HttpMethod.Get.ToString()), attributes[SpanAttributeConstants.HttpMethodKey]); Assert.Equal(AttributeValue.StringAttributeValue("localhost:5555"), attributes[SpanAttributeConstants.HttpHostKey]); Assert.Equal(AttributeValue.StringAttributeValue("/"), attributes[SpanAttributeConstants.HttpPathKey]); Assert.Equal(AttributeValue.StringAttributeValue("Header"), attributes["http.request.TEST"]); Assert.Equal(AttributeValue.StringAttributeValue("Header"), attributes["http.response.TEST"]); Assert.Equal(AttributeValue.LongAttributeValue((long)HttpStatusCode.OK), attributes[SpanAttributeConstants.HttpStatusCodeKey]); }