Esempio n. 1
0
        public void SetParentNormalCase()
        {
            var ExpectedTraceState = "congo=t61rcWkgMzE";
            var parentContext      = new W3CTraceContext()
            {
                OperationName = "Foo",
                TraceState    = ExpectedTraceState
            };

            parentContext.StartAsNew();
            Assert.AreEqual(ExpectedTraceState, parentContext.TraceState);
            Assert.AreEqual(parentContext.CurrentActivity.Id, parentContext.TraceParent);
            Assert.AreEqual(parentContext.CurrentActivity.SpanId.ToHexString(), parentContext.TelemetryId);
            Assert.AreEqual(parentContext.CurrentActivity.RootId, parentContext.TelemetryContextOperationId);
            Assert.AreEqual(parentContext.CurrentActivity.ParentSpanId.ToHexString(), parentContext.TelemetryContextOperationParentId);

            var childContext = new W3CTraceContext()
            {
                OperationName = "Bar"
            };

            childContext.SetParentAndStart(parentContext);
            Assert.AreEqual(ExpectedTraceState, childContext.TraceState);
            Assert.AreEqual(childContext.CurrentActivity.Id, childContext.TraceParent);
            Assert.AreEqual(childContext.CurrentActivity.SpanId.ToHexString(), childContext.TelemetryId);
            Assert.AreEqual(childContext.CurrentActivity.RootId, childContext.TelemetryContextOperationId);
            Assert.AreEqual(parentContext.CurrentActivity.SpanId.ToHexString(), childContext.ParentSpanId);
            Assert.AreEqual(parentContext.CurrentActivity.SpanId.ToHexString(), childContext.TelemetryContextOperationParentId);
        }
Esempio n. 2
0
        public void SetParentWithNullObject()
        {
            var traceContext       = new W3CTraceContext();
            var parentTraceContext = new NullObjectTraceContext();

            traceContext.SetParentAndStart(parentTraceContext);
            Assert.AreEqual(traceContext.StartTime, traceContext.CurrentActivity.StartTimeUtc);
        }
Esempio n. 3
0
        internal static void UpdateTelemetryW3C(ITelemetry telemetry, W3CTraceContext context)
        {
            OperationTelemetry opTelemetry = telemetry as OperationTelemetry;

            bool initializeFromCurrent = opTelemetry != null;

            if (initializeFromCurrent)
            {
                initializeFromCurrent &= !(opTelemetry is DependencyTelemetry dependency &&
                                           dependency.Type == SqlRemoteDependencyType &&
                                           dependency.Context.GetInternalContext().SdkVersion
                                           .StartsWith(RddDiagnosticSourcePrefix, StringComparison.Ordinal));
            }

            if (!string.IsNullOrEmpty(context.TraceState))
            {
                opTelemetry.Properties["w3c_tracestate"] = context.TraceState;
            }

            TraceParent traceParent = context.TraceParent.ToTraceParent();

            if (initializeFromCurrent)
            {
                if (string.IsNullOrEmpty(opTelemetry.Id))
                {
                    opTelemetry.Id = StringUtilities.FormatRequestId(telemetry.Context.Operation.Id, traceParent.SpanId);
                }

                if (string.IsNullOrEmpty(context.ParentSpanId))
                {
                    telemetry.Context.Operation.ParentId = StringUtilities.FormatRequestId(telemetry.Context.Operation.Id, context.ParentSpanId);
                }
            }
            else
            {
                if (telemetry.Context.Operation.Id == null)
                {
                    telemetry.Context.Operation.Id = traceParent.TraceId;
                }

                if (telemetry.Context.Operation.ParentId == null) // TODO check if it works.
                {
                    telemetry.Context.Operation.ParentId = StringUtilities.FormatRequestId(telemetry.Context.Operation.Id, traceParent.SpanId);
                }
            }
        }
Esempio n. 4
0
        public void RestoredSoThatNoCurrentActivity()
        {
            var ExpectedTraceState   = "congo=t61rcWkgMzE";
            var ExpectedSpanId       = "b7ad6b7169203331";
            var ExpectedRootId       = "0af7651916cd43dd8448eb211c80319c";
            var ExpectedTraceParent  = $"00-{ExpectedRootId}-{ExpectedSpanId}-01";
            var ExpectedParentSpanId = "00f067aa0ba902b7";
            var context = new W3CTraceContext()
            {
                OperationName = "Foo",
                TraceState    = ExpectedTraceState,
                TraceParent   = ExpectedTraceParent,
                ParentSpanId  = ExpectedParentSpanId,
                TelemetryType = TelemetryType.Request
            };

            Assert.AreEqual(ExpectedTraceState, context.TraceState);
            Assert.AreEqual(ExpectedTraceParent, context.TraceParent);
            Assert.AreEqual(ExpectedSpanId, context.TelemetryId);
            Assert.AreEqual(ExpectedRootId, context.TelemetryContextOperationId);
            Assert.AreEqual(ExpectedParentSpanId, context.TelemetryContextOperationParentId);
        }