|
| 1 | +// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package software.aws.toolkits.jetbrains.services.telemetry.otel |
| 5 | + |
| 6 | +import com.intellij.openapi.Disposable |
| 7 | +import com.intellij.openapi.components.Service |
| 8 | +import com.intellij.openapi.diagnostic.thisLogger |
| 9 | +import com.intellij.platform.diagnostic.telemetry.impl.OpenTelemetryConfigurator |
| 10 | +import com.intellij.platform.util.http.ContentType |
| 11 | +import com.intellij.platform.util.http.httpPost |
| 12 | +import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator |
| 13 | +import io.opentelemetry.context.Context |
| 14 | +import io.opentelemetry.context.propagation.ContextPropagators |
| 15 | +import io.opentelemetry.exporter.internal.otlp.traces.TraceRequestMarshaler |
| 16 | +import io.opentelemetry.sdk.OpenTelemetrySdk |
| 17 | +import io.opentelemetry.sdk.trace.ReadWriteSpan |
| 18 | +import io.opentelemetry.sdk.trace.ReadableSpan |
| 19 | +import io.opentelemetry.sdk.trace.SdkTracerProvider |
| 20 | +import io.opentelemetry.sdk.trace.SpanProcessor |
| 21 | +import kotlinx.coroutines.CancellationException |
| 22 | +import kotlinx.coroutines.CoroutineScope |
| 23 | +import kotlinx.coroutines.launch |
| 24 | +import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider |
| 25 | +import software.amazon.awssdk.http.ContentStreamProvider |
| 26 | +import software.amazon.awssdk.http.HttpExecuteRequest |
| 27 | +import software.amazon.awssdk.http.SdkHttpMethod |
| 28 | +import software.amazon.awssdk.http.SdkHttpRequest |
| 29 | +import software.amazon.awssdk.http.apache.ApacheHttpClient |
| 30 | +import software.amazon.awssdk.http.auth.aws.signer.AwsV4HttpSigner |
| 31 | +import java.io.ByteArrayOutputStream |
| 32 | +import java.net.ConnectException |
| 33 | + |
| 34 | +private class BasicOtlpSpanProcessor(private val coroutineScope: CoroutineScope, private val traceUrl: String = "http://127.0.0.1:4318/v1/traces") : SpanProcessor { |
| 35 | + override fun onStart(parentContext: Context, span: ReadWriteSpan) {} |
| 36 | + override fun isStartRequired() = false |
| 37 | + override fun isEndRequired() = true |
| 38 | + |
| 39 | + override fun onEnd(span: ReadableSpan) { |
| 40 | + val data = span.toSpanData() |
| 41 | + coroutineScope.launch { |
| 42 | + try { |
| 43 | + val item = TraceRequestMarshaler.create(listOf(data)) |
| 44 | + |
| 45 | + httpPost(traceUrl, contentLength = item.binarySerializedSize.toLong(), contentType = ContentType.XProtobuf) { |
| 46 | + item.writeBinaryTo(this) |
| 47 | + } |
| 48 | + } catch (e: CancellationException) { |
| 49 | + throw e |
| 50 | + } catch (e: ConnectException) { |
| 51 | + thisLogger().warn("Cannot export (url=$traceUrl): ${e.message}") |
| 52 | + } catch (e: Throwable) { |
| 53 | + thisLogger().error("Cannot export (url=$traceUrl)", e) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +private class SigV4OtlpSpanProcessor(private val coroutineScope: CoroutineScope, private val traceUrl: String, private val creds: AwsCredentialsProvider) : SpanProcessor { |
| 60 | + override fun onStart(parentContext: Context, span: ReadWriteSpan) {} |
| 61 | + override fun isStartRequired() = false |
| 62 | + override fun isEndRequired() = true |
| 63 | + |
| 64 | + private val client = ApacheHttpClient.create() |
| 65 | + |
| 66 | + override fun onEnd(span: ReadableSpan) { |
| 67 | + coroutineScope.launch { |
| 68 | + val data = span.toSpanData() |
| 69 | + try { |
| 70 | + val item = TraceRequestMarshaler.create(listOf(data)) |
| 71 | + // calculate the sigv4 header |
| 72 | + val signer = AwsV4HttpSigner.create() |
| 73 | + val httpRequest = |
| 74 | + SdkHttpRequest.builder() |
| 75 | + .uri(traceUrl) |
| 76 | + .method(SdkHttpMethod.POST) |
| 77 | + .putHeader("Content-Type", "application/x-protobuf") |
| 78 | + .build() |
| 79 | + |
| 80 | + val baos = ByteArrayOutputStream() |
| 81 | + item.writeBinaryTo(baos) |
| 82 | + val payload = ContentStreamProvider.fromByteArray(baos.toByteArray()) |
| 83 | + val signedRequest = signer.sign { |
| 84 | + it.identity(creds.resolveIdentity().get()) |
| 85 | + it.request(httpRequest) |
| 86 | + it.payload(payload) |
| 87 | + it.putProperty(AwsV4HttpSigner.SERVICE_SIGNING_NAME, "osis") |
| 88 | + it.putProperty(AwsV4HttpSigner.REGION_NAME, "us-west-2") |
| 89 | + } |
| 90 | + |
| 91 | + // Create and HTTP client and send the request. ApacheHttpClient requires the 'apache-client' module. |
| 92 | + client.prepareRequest( |
| 93 | + HttpExecuteRequest.builder() |
| 94 | + .request(signedRequest.request()) |
| 95 | + .contentStreamProvider(signedRequest.payload().orElse(null)) |
| 96 | + .build() |
| 97 | + ).call() |
| 98 | + } catch (e: CancellationException) { |
| 99 | + throw e |
| 100 | + } catch (e: ConnectException) { |
| 101 | + thisLogger().warn("Cannot export (url=$traceUrl): ${e.message}") |
| 102 | + } catch (e: Throwable) { |
| 103 | + thisLogger().error("Cannot export (url=$traceUrl)", e) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +private object StdoutSpanProcessor : SpanProcessor { |
| 110 | + override fun onStart(parentContext: Context, span: ReadWriteSpan) {} |
| 111 | + override fun isStartRequired() = false |
| 112 | + override fun isEndRequired() = true |
| 113 | + |
| 114 | + override fun onEnd(span: ReadableSpan) { |
| 115 | + println(span.toSpanData()) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +@Service |
| 120 | +class OTelService(private val cs: CoroutineScope) : Disposable { |
| 121 | + private val sdkDelegate = lazy { |
| 122 | + val configurator = OpenTelemetryConfigurator( |
| 123 | + sdkBuilder = OpenTelemetrySdk.builder(), |
| 124 | + ) |
| 125 | + |
| 126 | + configurator.getConfiguredSdkBuilder() |
| 127 | + .setTracerProvider( |
| 128 | + SdkTracerProvider.builder() |
| 129 | + .addSpanProcessor(StdoutSpanProcessor) |
| 130 | + .setResource(configurator.resource) |
| 131 | + .build() |
| 132 | + ) |
| 133 | + .setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance())) |
| 134 | + .build() |
| 135 | + } |
| 136 | + val sdk: OpenTelemetrySdk by sdkDelegate |
| 137 | + |
| 138 | + override fun dispose() { |
| 139 | + if (sdkDelegate.isInitialized()) { |
| 140 | + sdk.close() |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments