From dad322d3e159004b8ddecc338e01c55365d8f59f Mon Sep 17 00:00:00 2001 From: "Alexander.Glukhov" Date: Mon, 7 Oct 2024 21:31:07 +0200 Subject: [PATCH] [opentelemetry][IJPL-163526] common telemetry context propagation covered with a test GitOrigin-RevId: d8b43f2d834da5d8277964db6cdf206afc5dabc5 --- .../TelemetryContextPropagationTest.kt | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 platform/diagnostic/telemetry/rt/testSrc/com/intellij/platform/diagnostic/telemetry/rt/context/TelemetryContextPropagationTest.kt diff --git a/platform/diagnostic/telemetry/rt/testSrc/com/intellij/platform/diagnostic/telemetry/rt/context/TelemetryContextPropagationTest.kt b/platform/diagnostic/telemetry/rt/testSrc/com/intellij/platform/diagnostic/telemetry/rt/context/TelemetryContextPropagationTest.kt new file mode 100644 index 000000000000..e3d0093828bd --- /dev/null +++ b/platform/diagnostic/telemetry/rt/testSrc/com/intellij/platform/diagnostic/telemetry/rt/context/TelemetryContextPropagationTest.kt @@ -0,0 +1,59 @@ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.platform.diagnostic.telemetry.rt.context + +import io.opentelemetry.api.trace.Span +import io.opentelemetry.api.trace.SpanContext +import io.opentelemetry.api.trace.TraceFlags +import io.opentelemetry.api.trace.TraceState +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.Test + +class TelemetryContextPropagationTest : TelemetryContextConfiguringTestCase() { + + @Test + fun `test context propagation from IDEA side`() { + val traceId = "79a7da3d96c8ff6350d304ca56f56711" + val spanId = "b8da87d1d227aedc" + withTestContext(traceId, spanId) { + val currentContext = TelemetryContext.current() + val capturedContext = currentContext.asString() + assertEquals("traceparent=00-$traceId-$spanId-00", capturedContext) + val recreatedContext = TelemetryContext.fromString(capturedContext) + assertEquals(currentContext, recreatedContext) + } + } + + @Test + fun `test context propagation onto IDEA side`() { + val traceId = "42a7da3d96c8ff6350d304ca56f56711" + val spanId = "b8da87d1d242aedc" + val context = TelemetryContext.fromString("traceparent=00-$traceId-$spanId-00") + context.extract() + .makeCurrent() + .use { _ -> + val currentContext = Span.current().spanContext + assertEquals(traceId, currentContext.traceId) + assertEquals(spanId, currentContext.spanId) + } + } + + @Test + fun `context deserialization should be fail-safe`() { + assertNotNull(TelemetryContext.fromString("abc")) + assertNotNull(TelemetryContext.fromString("abc,")) + assertNotNull(TelemetryContext.fromString("abc, , , , , ")) + } + + private fun withTestContext(traceId: String, spanId: String, fn: () -> Unit) { + val ctx = SpanContext.createFromRemoteParent( + traceId, + spanId, + TraceFlags.getDefault(), + TraceState.getDefault() + ) + Span.wrap(ctx) + .makeCurrent() + .use { _ -> fn() } + } +}