-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow direct access to trace and span id for zio-logging's LogFormat (#…
…842) * Allow direct access to trace and span id for zio-logging * Allow direct access to trace and span id for zio-logging * Implement LogFormats for spanId and traceId * Implement LogFormats for spanId and traceId * Implement LogFormats for spanId and traceId * Implement LogFormats for spanId and traceId * Implement LogFormats for spanId and traceId * Implement LogFormats for spanId and traceId PR fixes * Implement LogFormats for spanId and traceId PR fixes * Implement LogFormats for spanId and traceId * Implement LogFormats for spanId and traceId * Implement LogFormats for spanId and traceId * Implement LogFormats for spanId and traceId
- Loading branch information
1 parent
d4d2fd3
commit a18c1cd
Showing
10 changed files
with
441 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
--- | ||
id: opentelemetry-zio-logging | ||
title: "OpenTelemetry ZIO Logging" | ||
--- | ||
|
||
`zio-opentelemetry` logging facilities are implemented around OpenTelemetry Logging. | ||
|
||
In order to use `zio-opentelemetry` feature with `zio-logging` you should use `zio-opentelemetry-zio-logging` module. | ||
|
||
`OpenTelemetry ZIO Logging` contains utilities for combining ZIO Opentelemetry with ZIO Logging | ||
|
||
## Installation | ||
|
||
```scala | ||
"dev.zio" %% "zio-opentelemetry-zio-logging" % "<version>" | ||
``` | ||
|
||
## Features | ||
|
||
### Log formats | ||
|
||
This library implements [Log Format](https://zio.dev/zio-logging/formatting-log-records) for span information (`spanId` and `traceId`). | ||
To use them you need a `LogFormats` service in the environment. For this, use the `ZioLogging.logFormats` layer which in turn required a suitable `ContextStorage` implementation. | ||
|
||
```scala | ||
//> using scala "2.13.14" | ||
//> using dep dev.zio::zio:2.1.1 | ||
//> using dep dev.zio::zio-opentelemetry:3.0.0-RC24 | ||
//> using dep dev.zio::zio-opentelemetry-zio-logging:3.0.0-RC24 | ||
//> using dep io.opentelemetry:opentelemetry-sdk:1.38.0 | ||
//> using dep io.opentelemetry:opentelemetry-sdk-trace:1.38.0 | ||
//> using dep io.opentelemetry:opentelemetry-exporter-logging-otlp:1.38.0 | ||
//> using dep io.opentelemetry.semconv:opentelemetry-semconv:1.22.0-alpha | ||
|
||
import io.opentelemetry.exporter.logging.otlp.OtlpJsonLoggingSpanExporter | ||
import io.opentelemetry.exporter.logging.otlp.OtlpJsonLoggingLogRecordExporter | ||
import io.opentelemetry.api.common.Attributes | ||
import io.opentelemetry.sdk.trace.SdkTracerProvider | ||
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor | ||
import io.opentelemetry.sdk.logs.SdkLoggerProvider | ||
import io.opentelemetry.sdk.logs.`export`.SimpleLogRecordProcessor | ||
import io.opentelemetry.sdk.resources.Resource | ||
import io.opentelemetry.semconv.ResourceAttributes | ||
import io.opentelemetry.sdk.OpenTelemetrySdk | ||
import io.opentelemetry.api | ||
import zio._ | ||
import zio.logging.console | ||
import zio.logging.LogFormat._ | ||
import zio.telemetry.opentelemetry.tracing.Tracing | ||
import zio.telemetry.opentelemetry.OpenTelemetry | ||
import zio.telemetry.opentelemetry.context.ContextStorage | ||
import zio.telemetry.opentelemetry.zio.logging.LogFormats | ||
import zio.telemetry.opentelemetry.zio.logging.ZioLogging | ||
|
||
object ZioLoggingApp extends ZIOAppDefault { | ||
|
||
val instrumentationScopeName = "dev.zio.LoggingApp" | ||
val resourceName = "logging-app" | ||
|
||
// Prints to stdout in OTLP Json format | ||
val stdoutLoggerProvider: RIO[Scope, SdkLoggerProvider] = | ||
for { | ||
logRecordExporter <- ZIO.fromAutoCloseable(ZIO.succeed(OtlpJsonLoggingLogRecordExporter.create())) | ||
logRecordProcessor <- ZIO.fromAutoCloseable(ZIO.succeed(SimpleLogRecordProcessor.create(logRecordExporter))) | ||
loggerProvider <- | ||
ZIO.fromAutoCloseable( | ||
ZIO.succeed( | ||
SdkLoggerProvider | ||
.builder() | ||
.setResource(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, resourceName))) | ||
.addLogRecordProcessor(logRecordProcessor) | ||
.build() | ||
) | ||
) | ||
} yield loggerProvider | ||
|
||
// Prints to stdout in OTLP Json format | ||
val stdoutTracerProvider: RIO[Scope, SdkTracerProvider] = | ||
for { | ||
spanExporter <- ZIO.fromAutoCloseable(ZIO.succeed(OtlpJsonLoggingSpanExporter.create())) | ||
spanProcessor <- ZIO.fromAutoCloseable(ZIO.succeed(SimpleSpanProcessor.create(spanExporter))) | ||
tracerProvider <- | ||
ZIO.fromAutoCloseable( | ||
ZIO.succeed( | ||
SdkTracerProvider | ||
.builder() | ||
.setResource(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, resourceName))) | ||
.addSpanProcessor(spanProcessor) | ||
.build() | ||
) | ||
) | ||
} yield tracerProvider | ||
|
||
val otelSdkLayer: TaskLayer[api.OpenTelemetry] = | ||
OpenTelemetry.custom( | ||
for { | ||
tracerProvider <- stdoutTracerProvider | ||
loggerProvider <- stdoutLoggerProvider | ||
sdk <- ZIO.fromAutoCloseable( | ||
ZIO.succeed( | ||
OpenTelemetrySdk | ||
.builder() | ||
.setTracerProvider(tracerProvider) | ||
.setLoggerProvider(loggerProvider) | ||
.build() | ||
) | ||
) | ||
} yield sdk | ||
) | ||
|
||
// Setup zio-logging with spanId and traceId labels | ||
val loggingLayer: URLayer[LogFormats, Unit] = ZLayer { | ||
for { | ||
logFormats <- ZIO.service[LogFormats] | ||
format = timestamp.fixed(32) |-| level |-| label("message", quoted(line)) |-| logFormats.spanIdLabel |-| logFormats.traceIdLabel | ||
myConsoleLogger = console(format.highlight) | ||
} yield Runtime.removeDefaultLoggers >>> myConsoleLogger | ||
}.flatten | ||
|
||
|
||
override def run = | ||
ZIO | ||
.serviceWithZIO[Tracing] { tracing => | ||
val logic = for { | ||
// Read user input | ||
message <- Console.readLine | ||
// Print span and trace ids along with message | ||
_ <- ZIO.logInfo(s"User message: $message") | ||
} yield () | ||
|
||
// All log messages produced by `logic` will be correlated with a "root_span" automatically | ||
logic @@ tracing.aspects.root("root_span") | ||
} | ||
.provide( | ||
otelSdkLayer, | ||
OpenTelemetry.logging(instrumentationScopeName), | ||
OpenTelemetry.tracing(instrumentationScopeName), | ||
OpenTelemetry.contextZIO, | ||
ZioLogging.logFormats, | ||
loggingLayer | ||
) | ||
|
||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...metry-zio-logging/src/main/scala/zio/telemetry/opentelemetry/zio/logging/LogFormats.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package zio.telemetry.opentelemetry.zio.logging | ||
|
||
import zio.logging.LogFormat | ||
import zio.logging.LogFormat.label | ||
|
||
trait LogFormats { | ||
|
||
/** | ||
* Will print traceId from current span or nothing when not in span | ||
*/ | ||
def traceId: LogFormat | ||
|
||
/** | ||
* Will print spanId from current span or nothing when not in span | ||
*/ | ||
def spanId: LogFormat | ||
|
||
/** | ||
* Label with `traceId` key and [[traceId]] value | ||
*/ | ||
def traceIdLabel: LogFormat = label("traceId", traceId) | ||
|
||
/** | ||
* Label with `spanId` key and [[spanId]] value | ||
*/ | ||
def spanIdLabel: LogFormat = label("spanId", spanId) | ||
} |
36 changes: 36 additions & 0 deletions
36
...metry-zio-logging/src/main/scala/zio/telemetry/opentelemetry/zio/logging/ZioLogging.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package zio.telemetry.opentelemetry.zio.logging | ||
|
||
import io.opentelemetry.api.trace.{Span, SpanContext} | ||
import io.opentelemetry.context.Context | ||
import zio._ | ||
import zio.logging.LogFormat | ||
import zio.telemetry.opentelemetry.context.ContextStorage | ||
|
||
object ZioLogging { | ||
|
||
def logFormats: ZLayer[ContextStorage, Nothing, LogFormats] = ZLayer { | ||
for { | ||
ctxStorage <- ZIO.service[ContextStorage] | ||
} yield new LogFormats { | ||
override def traceId: LogFormat = LogFormat.make { (builder, _, _, _, _, _, fiberRefs, _, _) => | ||
getSpanContext(ctxStorage, fiberRefs).map(_.getTraceId).fold(())(builder.appendText(_)) | ||
} | ||
|
||
override def spanId: LogFormat = LogFormat.make { (builder, _, _, _, _, _, fiberRefs, _, _) => | ||
getSpanContext(ctxStorage, fiberRefs).map(_.getSpanId).fold(())(builder.appendText(_)) | ||
} | ||
|
||
private def getSpanContext(ctxStorage: ContextStorage, fiberRefs: FiberRefs): Option[SpanContext] = { | ||
val maybeOtelContext = ctxStorage match { | ||
case ref: ContextStorage.ZIOFiberRef => fiberRefs.get(ref.ref) | ||
case ContextStorage.Native => Some(Context.current()) | ||
} | ||
|
||
maybeOtelContext | ||
.map(Span.fromContext) | ||
.map(_.getSpanContext) | ||
} | ||
} | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
...ging/src/test/scala/zio/telemetry/opentelemetry/zio/logging/TelemetryLogFormatsSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package zio.telemetry.opentelemetry.zio.logging | ||
|
||
import io.opentelemetry.api.trace.Tracer | ||
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter | ||
import io.opentelemetry.sdk.trace.SdkTracerProvider | ||
import io.opentelemetry.sdk.trace.data.SpanData | ||
import io.opentelemetry.sdk.trace.`export`.SimpleSpanProcessor | ||
import zio.Runtime.removeDefaultLoggers | ||
import zio.telemetry.opentelemetry.context.ContextStorage | ||
import zio.telemetry.opentelemetry.tracing.Tracing | ||
import zio.test.{Spec, TestEnvironment, ZIOSpecDefault, assertTrue} | ||
import zio.{Scope, UIO, ULayer, URLayer, ZEnvironment, ZIO, ZLayer} | ||
|
||
import scala.collection.mutable | ||
import scala.jdk.CollectionConverters._ | ||
|
||
object TelemetryLogFormatsSpec extends ZIOSpecDefault { | ||
|
||
val inMemoryTracer: UIO[(InMemorySpanExporter, Tracer)] = for { | ||
spanExporter <- ZIO.succeed(InMemorySpanExporter.create()) | ||
spanProcessor <- ZIO.succeed(SimpleSpanProcessor.create(spanExporter)) | ||
tracerProvider <- ZIO.succeed(SdkTracerProvider.builder().addSpanProcessor(spanProcessor).build()) | ||
tracer = tracerProvider.get("TracingTest") | ||
} yield (spanExporter, tracer) | ||
|
||
val inMemoryTracerLayer: ULayer[InMemorySpanExporter with Tracer] = | ||
ZLayer.fromZIOEnvironment(inMemoryTracer.map { case (inMemorySpanExporter, tracer) => | ||
ZEnvironment(inMemorySpanExporter).add(tracer) | ||
}) | ||
|
||
def tracingMockLayer( | ||
logAnnotated: Boolean = false | ||
): URLayer[ContextStorage, Tracing with InMemorySpanExporter with Tracer] = | ||
inMemoryTracerLayer >>> (Tracing.live(logAnnotated) ++ inMemoryTracerLayer) | ||
|
||
def getFinishedSpans: ZIO[InMemorySpanExporter, Nothing, List[SpanData]] = | ||
ZIO.serviceWith[InMemorySpanExporter](_.getFinishedSpanItems.asScala.toList) | ||
|
||
override def spec: Spec[TestEnvironment with Scope, Any] = | ||
suiteAll("opentelemetry-zio-logging LogFormats") { | ||
test("SpanId and traceId are extracted") { | ||
ZIO.serviceWithZIO[Tracing] { tracing => | ||
import tracing.aspects._ | ||
val logs = mutable.Buffer[String]() | ||
|
||
for { | ||
logFormats <- ZIO.service[LogFormats] | ||
format = logFormats.spanIdLabel |-| logFormats.traceIdLabel | ||
zLogger = format.toLogger.map(logs.append(_)) | ||
_ <- zio.ZIO.logInfo("TEST").withLogger(zLogger) @@ span("Span") @@ root("Root") | ||
spans <- getFinishedSpans | ||
child = spans.find(_.getName == "Span").get | ||
log = logs.head | ||
} yield assertTrue(log == s"spanId=${child.getSpanId} traceId=${child.getTraceId}") | ||
} | ||
} | ||
}.provide(removeDefaultLoggers, tracingMockLayer(), ContextStorage.fiberRef, ZioLogging.logFormats) | ||
|
||
} |
Oops, something went wrong.