|
| 1 | +/* |
| 2 | + * Copyright 2013-2021 The Kamon Project <https://kamon.io> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package kamon.otel |
| 17 | + |
| 18 | +import com.typesafe.config.Config |
| 19 | +import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest |
| 20 | +import io.opentelemetry.proto.common.v1.InstrumentationLibrary |
| 21 | +import io.opentelemetry.proto.resource.v1.Resource |
| 22 | +import io.opentelemetry.proto.trace.v1.ResourceSpans |
| 23 | +import kamon.Kamon |
| 24 | +import kamon.module.{Module, ModuleFactory, SpanReporter} |
| 25 | +import kamon.trace.Span |
| 26 | +import kamon.otel.SpanConverter._ |
| 27 | +import org.slf4j.LoggerFactory |
| 28 | + |
| 29 | +import java.util.Collections |
| 30 | +import scala.concurrent.ExecutionContext |
| 31 | +import scala.util.{Failure, Success} |
| 32 | + |
| 33 | +object OpenTelemetryTraceReporter { |
| 34 | + private val logger = LoggerFactory.getLogger(classOf[OpenTelemetryTraceReporter]) |
| 35 | + private val kamonVersion = Kamon.status().settings().version |
| 36 | + |
| 37 | + class Factory extends ModuleFactory { |
| 38 | + override def create(settings: ModuleFactory.Settings): Module = { |
| 39 | + logger.info("Creating OpenTelemetry Trace Reporter") |
| 40 | + |
| 41 | + val module = new OpenTelemetryTraceReporter(GrpcTraceService(_))(settings.executionContext) |
| 42 | + module.reconfigure(settings.config) |
| 43 | + module |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +import kamon.otel.OpenTelemetryTraceReporter._ |
| 49 | +/** |
| 50 | + * Converts internal finished Kamon spans to OpenTelemetry format and sends to a configured OpenTelemetry endpoint using gRPC. |
| 51 | + */ |
| 52 | +class OpenTelemetryTraceReporter(traceServiceFactory:Config=>TraceService)(implicit ec:ExecutionContext) extends SpanReporter { |
| 53 | + private var traceService:Option[TraceService] = None |
| 54 | + private var spanConverterFunc:Seq[Span.Finished]=>ResourceSpans = (_ => ResourceSpans.newBuilder().build()) |
| 55 | + |
| 56 | + override def reportSpans(spans: Seq[Span.Finished]): Unit = { |
| 57 | + if(!spans.isEmpty) { |
| 58 | + val resources = Collections.singletonList(spanConverterFunc(spans)) //all spans should belong to the same single resource |
| 59 | + val exportTraceServiceRequest = ExportTraceServiceRequest.newBuilder() |
| 60 | + .addAllResourceSpans(resources) |
| 61 | + .build() |
| 62 | + |
| 63 | + traceService.foreach ( |
| 64 | + _.export(exportTraceServiceRequest).onComplete { |
| 65 | + case Success(_) => logger.debug("Successfully exported traces") |
| 66 | + |
| 67 | + //TODO is there result for which a retry is relevant? Perhaps a glitch in the receiving service |
| 68 | + //Keeping logs to debug as other exporters (e.g.Zipkin) won't log anything if it fails to export traces |
| 69 | + case Failure(t) => logger.debug("Failed to export traces", t) |
| 70 | + } |
| 71 | + ) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + override def reconfigure(newConfig: Config): Unit = { |
| 76 | + logger.info("Reconfigure OpenTelemetry Trace Reporter") |
| 77 | + |
| 78 | + //pre-generate the function for converting Kamon span to proto span |
| 79 | + val instrumentationLibrary:InstrumentationLibrary = InstrumentationLibrary.newBuilder().setName("kamon").setVersion(kamonVersion).build() |
| 80 | + val resource:Resource = buildResource(newConfig.getBoolean("kamon.otel.trace.include-environment-tags")) |
| 81 | + this.spanConverterFunc = SpanConverter.toProtoResourceSpan(resource, instrumentationLibrary) |
| 82 | + |
| 83 | + this.traceService = Option(traceServiceFactory.apply(newConfig)) |
| 84 | + } |
| 85 | + |
| 86 | + override def stop(): Unit = { |
| 87 | + logger.info("Stopping OpenTelemetry Trace Reporter") |
| 88 | + this.traceService.foreach(_.close()) |
| 89 | + this.traceService = None |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Builds the resource information added as resource labels to the exported traces |
| 94 | + * @param includeEnvTags |
| 95 | + * @return |
| 96 | + */ |
| 97 | + private def buildResource(includeEnvTags:Boolean):Resource = { |
| 98 | + val env = Kamon.environment |
| 99 | + val builder = Resource.newBuilder() |
| 100 | + .addAttributes(stringKeyValue("service.name", env.service)) |
| 101 | + .addAttributes(stringKeyValue("telemetry.sdk.name", "kamon")) |
| 102 | + .addAttributes(stringKeyValue("telemetry.sdk.language", "scala")) |
| 103 | + .addAttributes(stringKeyValue("telemetry.sdk.version", kamonVersion)) |
| 104 | + |
| 105 | + //add all kamon.environment.tags as KeyValues to the Resource object |
| 106 | + if(includeEnvTags) { |
| 107 | + env.tags.iterator().map(toProtoKeyValue).foreach(builder.addAttributes) |
| 108 | + } |
| 109 | + |
| 110 | + builder.build() |
| 111 | + } |
| 112 | +} |
0 commit comments