-
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.
- Loading branch information
Showing
4 changed files
with
262 additions
and
60 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
64 changes: 64 additions & 0 deletions
64
opentelemetry/src/main/scala/zio/telemetry/opentelemetry/logging/Logging.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,64 @@ | ||
package zio.telemetry.opentelemetry.logging | ||
|
||
import io.opentelemetry.api.common.AttributeKey | ||
import io.opentelemetry.api.logs.{Logger, LoggerProvider} | ||
import io.opentelemetry.context.Context | ||
import zio._ | ||
import zio.telemetry.opentelemetry.context.ContextStorage | ||
|
||
object Logging { | ||
|
||
def live( | ||
instrumentationScopeName: String, | ||
logLevel: LogLevel = LogLevel.Info | ||
): ZLayer[ContextStorage with LoggerProvider, Nothing, Unit] = | ||
ZLayer | ||
.fromZIO( | ||
for { | ||
loggerProvider <- ZIO.service[LoggerProvider] | ||
contextStorage <- ZIO.service[ContextStorage] | ||
logger <- ZIO.succeed( | ||
zioLogger(instrumentationScopeName)(contextStorage, loggerProvider) | ||
.filterLogLevel(l => l >= logLevel) | ||
) | ||
} yield logger | ||
) | ||
.flatMap(env => Runtime.addLogger(env.get)) | ||
|
||
private def zioLogger(instrumentationScopeName: String)( | ||
contextStorage: ContextStorage, | ||
loggerProvider: LoggerProvider | ||
): ZLogger[String, Unit] = | ||
new ZLogger[String, Unit] { | ||
|
||
val logger: Logger = loggerProvider.get(instrumentationScopeName) | ||
|
||
override def apply( | ||
trace: Trace, | ||
fiberId: FiberId, | ||
logLevel: LogLevel, | ||
message: () => String, | ||
cause: Cause[Any], | ||
context: FiberRefs, | ||
spans: List[LogSpan], | ||
annotations: Map[String, String] | ||
): Unit = { | ||
val builder = logger.logRecordBuilder() | ||
|
||
builder.setBody(message()) | ||
builder.setSeverityText(logLevel.label) | ||
annotations.foreach { case (k, v) => builder.setAttribute(AttributeKey.stringKey(k), v) } | ||
|
||
contextStorage match { | ||
case cs: ContextStorage.FiberRefContextStorage => | ||
context.get(cs.ref).foreach(builder.setContext) | ||
case _: ContextStorage.OpenTelemetryContextStorage => | ||
builder.setContext(Context.current()) | ||
} | ||
|
||
builder.emit() | ||
} | ||
|
||
} | ||
|
||
} |
136 changes: 136 additions & 0 deletions
136
opentelemetry/src/test/scala/zio/telemetry/opentelemetry/logging/LoggingTest.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,136 @@ | ||
package zio.telemetry.opentelemetry.logging | ||
|
||
import io.opentelemetry.api.logs.LoggerProvider | ||
import io.opentelemetry.sdk.logs.SdkLoggerProvider | ||
import io.opentelemetry.sdk.logs.`export`.SimpleLogRecordProcessor | ||
import io.opentelemetry.sdk.logs.data.LogRecordData | ||
import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter | ||
import zio._ | ||
import zio.telemetry.opentelemetry.context.ContextStorage | ||
import zio.telemetry.opentelemetry.tracing.{Tracing, TracingTest} | ||
import zio.test._ | ||
import zio.test.Assertion._ | ||
|
||
import scala.jdk.CollectionConverters._ | ||
|
||
object LoggingTest extends ZIOSpecDefault { | ||
|
||
val inMemoryLogLoggerProvider = | ||
for { | ||
logRecordExporter <- ZIO.succeed(InMemoryLogRecordExporter.create()) | ||
logRecordProcessor <- ZIO.succeed(SimpleLogRecordProcessor.create(logRecordExporter)) | ||
loggerProvider <- ZIO.succeed(SdkLoggerProvider.builder().addLogRecordProcessor(logRecordProcessor).build()) | ||
} yield (logRecordExporter, loggerProvider) | ||
|
||
val inMemoryLoggerProviderLayer: ULayer[InMemoryLogRecordExporter with LoggerProvider] = | ||
ZLayer.fromZIOEnvironment(inMemoryLogLoggerProvider.map { case (inMemoryLogRecordExporter, loggerProvider) => | ||
ZEnvironment(inMemoryLogRecordExporter).add(loggerProvider) | ||
}) | ||
|
||
def loggingMockLayer( | ||
instrumentationScopeName: String | ||
): URLayer[ContextStorage, InMemoryLogRecordExporter with LoggerProvider] = | ||
inMemoryLoggerProviderLayer >>> ( | ||
Logging.live(instrumentationScopeName) ++ inMemoryLoggerProviderLayer | ||
) | ||
|
||
def getFinishedLogRecords: ZIO[InMemoryLogRecordExporter, Nothing, List[LogRecordData]] = | ||
ZIO.service[InMemoryLogRecordExporter].map(_.getFinishedLogRecordItems.asScala.toList) | ||
|
||
override def spec: Spec[TestEnvironment with Scope, Any] = | ||
suite("zio opentelemetry")( | ||
suite("Logging")( | ||
test("works with empty tracing context") { | ||
for { | ||
_ <- ZIO.logInfo("test") | ||
logRecords <- getFinishedLogRecords | ||
} yield { | ||
val r = logRecords.head | ||
val body = r.getBody.asString() | ||
val severityNumber = r.getSeverity.getSeverityNumber | ||
val severityText = r.getSeverityText | ||
val instrumentationScopeName = r.getInstrumentationScopeInfo.getName | ||
val attributes = r.getAttributes.asMap().asScala.toMap.map { case (k, v) => k.getKey -> v.toString } | ||
val traceId = r.getSpanContext.getTraceId | ||
val spanId = r.getSpanContext.getSpanId | ||
|
||
assert(logRecords.length)(equalTo(1)) && | ||
assert(body)(equalTo("test")) && | ||
assert(severityNumber)(equalTo(0)) && // TODO: set it | ||
assert(severityText)(equalTo("INFO")) && | ||
assert(instrumentationScopeName)(equalTo("test1")) && | ||
assert(attributes)(equalTo(Map.empty[String, String])) && | ||
assert(traceId)(equalTo("00000000000000000000000000000000")) && | ||
assert(spanId)(equalTo("0000000000000000")) | ||
} | ||
}.provide(Runtime.removeDefaultLoggers >>> loggingMockLayer("test1"), ContextStorage.fiberRef), | ||
test("works in a tracing context (fiberRef)") { | ||
ZIO.serviceWithZIO[Tracing] { tracing => | ||
tracing.root("ROOT")( | ||
for { | ||
spanCtx <- tracing.getCurrentSpanContextUnsafe | ||
_ <- ZIO.logInfo("test") | ||
logRecords <- getFinishedLogRecords | ||
} yield { | ||
val r = logRecords.head | ||
val body = r.getBody.asString() | ||
val severityNumber = r.getSeverity.getSeverityNumber | ||
val severityText = r.getSeverityText | ||
val instrumentationScopeName = r.getInstrumentationScopeInfo.getName | ||
val attributes = r.getAttributes.asMap().asScala.toMap.map { case (k, v) => k.getKey -> v.toString } | ||
val traceId = r.getSpanContext.getTraceId | ||
val spanId = r.getSpanContext.getSpanId | ||
|
||
assert(logRecords.length)(equalTo(1)) && | ||
assert(body)(equalTo("test")) && | ||
assert(severityNumber)(equalTo(0)) && // TODO: set it | ||
assert(severityText)(equalTo("INFO")) && | ||
assert(instrumentationScopeName)(equalTo("test2")) && | ||
assert(attributes)(equalTo(Map.empty[String, String])) && | ||
assert(traceId)(equalTo(spanCtx.getTraceId)) && | ||
assert(spanId)(equalTo(spanCtx.getSpanId)) | ||
} | ||
) | ||
} | ||
}.provide( | ||
Runtime.removeDefaultLoggers >>> loggingMockLayer("test2"), | ||
TracingTest.tracingMockLayer, | ||
ContextStorage.fiberRef | ||
), | ||
test("works in a tracing context (openTelemtryContext") { | ||
ZIO.serviceWithZIO[Tracing] { tracing => | ||
tracing.root("ROOT")( | ||
for { | ||
spanCtx <- tracing.getCurrentSpanContextUnsafe | ||
_ <- ZIO.logInfo("test") | ||
logRecords <- getFinishedLogRecords | ||
} yield { | ||
val r = logRecords.head | ||
val body = r.getBody.asString() | ||
val severityNumber = r.getSeverity.getSeverityNumber | ||
val severityText = r.getSeverityText | ||
val instrumentationScopeName = r.getInstrumentationScopeInfo.getName | ||
val attributes = r.getAttributes.asMap().asScala.toMap.map { case (k, v) => k.getKey -> v.toString } | ||
val traceId = r.getSpanContext.getTraceId | ||
val spanId = r.getSpanContext.getSpanId | ||
|
||
assert(logRecords.length)(equalTo(1)) && | ||
assert(body)(equalTo("test")) && | ||
assert(severityNumber)(equalTo(0)) && // TODO: set it | ||
assert(severityText)(equalTo("INFO")) && | ||
assert(instrumentationScopeName)(equalTo("test3")) && | ||
assert(attributes)(equalTo(Map.empty[String, String])) && | ||
assert(traceId)(equalTo(spanCtx.getTraceId)) && | ||
assert(spanId)(equalTo(spanCtx.getSpanId)) | ||
} | ||
) | ||
} | ||
}.provide( | ||
Runtime.removeDefaultLoggers >>> loggingMockLayer("test3"), | ||
TracingTest.tracingMockLayer, | ||
ContextStorage.openTelemetryContext | ||
) | ||
) | ||
) | ||
|
||
} |
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