Skip to content

Instantly share code, notes, and snippets.

@arosien
Last active July 16, 2024 07:34
Show Gist options
  • Save arosien/80e94367fb74c4f40a92c50b07d57370 to your computer and use it in GitHub Desktop.
Save arosien/80e94367fb74c4f40a92c50b07d57370 to your computer and use it in GitHub Desktop.
scala-cli script to show inserting an otel4s span into a log4cats logger
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="CONSOLE"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%X{traceId} %X{spanId} - %m%n</Pattern>
</layout>
</appender>
<root level="debug">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
//> using scala 2
//> using toolkit typelevel:latest
//> using lib org.typelevel::otel4s-java:0.4.0
//> using lib "io.opentelemetry:opentelemetry-exporter-logging:1.34.0"
//> using lib "io.opentelemetry:opentelemetry-sdk-extension-autoconfigure:1.34.0"
//> using lib "io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi:1.34.0"
//> using lib "ch.qos.logback:logback-classic:1.4.14"
//> using resourceDir .
//> using javaProp otel.java.global-autoconfigure.enabled=true otel.metrics.exporter=logging otel.traces.exporter=logging otel.logs.exporter=logging otel.metric.export.interval=500
import cats._
import cats.effect._
import cats.syntax.all._
import org.typelevel.otel4s._
import org.typelevel.otel4s.java.OtelJava
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk
import org.typelevel.log4cats.StructuredLogger
import org.typelevel.log4cats.slf4j.Slf4jLogger
import org.typelevel.otel4s.trace.Span
object Otel4sLog4CatsMDC extends IOApp.Simple {
def run: IO[Unit] =
otel *> OtelJava.global.flatMap { otel4s =>
otel4s.tracerProvider.get("test").flatMap { tracer =>
tracer.span("root").use { implicit span =>
logger[IO].withSpan
.info("yay")
}
}
}
implicit def logger[F[_]: Sync]: StructuredLogger[F] =
Slf4jLogger.getLogger[F]
val otel: IO[Unit] = IO {
AutoConfiguredOpenTelemetrySdk.initialize()
}
implicit class LoggerOps[F[_]](logger: StructuredLogger[F]) {
def withSpan(implicit span: Span[F]): StructuredLogger[F] =
logger.addContext(
"traceId" -> span.context.traceIdHex,
"spanId" -> span.context.spanIdHex
)
}
}
➜ otel-log4cats-mdc scala-cli ./otel-log4cats-mdc.scala
Compiling project (Scala 2.13.11, JVM)
Compiled project (Scala 2.13.11, JVM)
11:35:32,542 |-INFO in ch.qos.logback.classic.LoggerContext[default] - This is logback-classic version 1.4.14
11:35:32,543 |-INFO in ch.qos.logback.classic.util.ContextInitializer@37bf1034 - No custom configurators were discovered as a service.
11:35:32,543 |-INFO in ch.qos.logback.classic.util.ContextInitializer@37bf1034 - Trying to configure with ch.qos.logback.classic.joran.SerializedModelConfigurator
11:35:32,544 |-INFO in ch.qos.logback.classic.util.ContextInitializer@37bf1034 - Constructed configurator of type class ch.qos.logback.classic.joran.SerializedModelConfigurator
11:35:32,544 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.scmo]
11:35:32,544 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.scmo]
11:35:32,549 |-INFO in ch.qos.logback.classic.util.ContextInitializer@37bf1034 - ch.qos.logback.classic.joran.SerializedModelConfigurator.configure() call lasted 0 milliseconds. ExecutionStatus=INVOKE_NEXT_IF_ANY
11:35:32,549 |-INFO in ch.qos.logback.classic.util.ContextInitializer@37bf1034 - Trying to configure with ch.qos.logback.classic.util.DefaultJoranConfigurator
11:35:32,550 |-INFO in ch.qos.logback.classic.util.ContextInitializer@37bf1034 - Constructed configurator of type class ch.qos.logback.classic.util.DefaultJoranConfigurator
11:35:32,550 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
11:35:32,550 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/Users/arosien/arosien/otel-log4cats-mdc/.scala-build/otel-log4cats-mdc_921184d5a8_45dd530d03-a1817f8780/classes/main/logback.xml]
11:35:32,551 |-WARN in ch.qos.logback.classic.util.DefaultJoranConfigurator@2506ea0f - Resource [logback.xml] occurs multiple times on the classpath.
11:35:32,551 |-WARN in ch.qos.logback.classic.util.DefaultJoranConfigurator@2506ea0f - Resource [logback.xml] occurs at [file:/Users/arosien/arosien/otel-log4cats-mdc/logback.xml]
11:35:32,551 |-WARN in ch.qos.logback.classic.util.DefaultJoranConfigurator@2506ea0f - Resource [logback.xml] occurs at [file:/Users/arosien/arosien/otel-log4cats-mdc/.scala-build/otel-log4cats-mdc_921184d5a8_45dd530d03-a1817f8780/classes/main/logback.xml]
11:35:32,629 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - Processing appender named [CONSOLE]
11:35:32,629 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
11:35:32,644 |-WARN in ch.qos.logback.core.ConsoleAppender[CONSOLE] - This appender no longer admits a layout as a sub-component, set an encoder instead.
11:35:32,644 |-WARN in ch.qos.logback.core.ConsoleAppender[CONSOLE] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
11:35:32,644 |-WARN in ch.qos.logback.core.ConsoleAppender[CONSOLE] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
11:35:32,645 |-INFO in ch.qos.logback.classic.model.processor.RootLoggerModelHandler - Setting level of ROOT logger to DEBUG
11:35:32,645 |-INFO in ch.qos.logback.core.model.processor.AppenderRefModelHandler - Attaching appender named [CONSOLE] to Logger[ROOT]
11:35:32,645 |-INFO in ch.qos.logback.core.model.processor.DefaultProcessor@535e0f04 - End of configuration.
11:35:32,646 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@4acae6a7 - Registering current configuration as safe fallback point
11:35:32,646 |-INFO in ch.qos.logback.classic.util.ContextInitializer@37bf1034 - ch.qos.logback.classic.util.DefaultJoranConfigurator.configure() call lasted 96 milliseconds. ExecutionStatus=DO_NOT_INVOKE_NEXT_IF_ANY
9b347be3b6716c7705c65b6af3988522 2279564950df9269 - yay
Jan 09, 2024 11:35:32 AM io.opentelemetry.exporter.logging.LoggingSpanExporter export
INFO: 'root' : 9b347be3b6716c7705c65b6af3988522 2279564950df9269 INTERNAL [tracer: test:] {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment