Skip to content

Commit 4f0781e

Browse files
committed
refactor(zio-opentelemetry): refactor logging configuration for extensibility
Refactor the `Logging` trait to allow overriding the log level and log endpoint via protected methods (`oltpLogLevel` and `otelLogEndpoint`) instead of directly accessing environment variables. This enables easier customization in subclasses.
1 parent 801feb3 commit 4f0781e

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

observability/zio-opentelemetry/src/main/scala/sttp/tapir/server/ziopentelemetry/Logger.scala

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@ trait Logging {
1616

1717
/** The log level to use for the OpenTelemetry logger provider.
1818
*
19-
* Uses the `OTEL_LOG_LEVEL` environment variable to determine the log level.
19+
* Uses the [[oltpLogLevel]] method to determine the log level, can be overridden to provide a different log level logic.
2020
*
2121
* By default, this is set to `INFO`. You can override this to change the log level, e.g. to `DEBUG` for more verbose logging.
2222
*/
23-
def logLevel = sys.env.getOrElse("OTEL_LOG_LEVEL", "INFO") match {
23+
def logLevel = oltpLogLevel
24+
25+
/** Uses the `OTEL_LOG_LEVEL` environment variable to determine the log level.
26+
*
27+
* By default, this is set to `INFO`. You can override this to change the log level, e.g. to `DEBUG` for more verbose logging.
28+
*/
29+
final protected def oltpLogLevel = sys.env.getOrElse("OTEL_LOG_LEVEL", "INFO").toUpperCase match {
2430
case "DEBUG" => LogLevel.Debug
2531
case "INFO" => LogLevel.Info
2632
case "WARN" => LogLevel.Warning
@@ -30,14 +36,23 @@ trait Logging {
3036
case _ => LogLevel.Info
3137
}
3238

39+
/** The OTLP endpoint to use for logging.
40+
*
41+
* Default uses the `OTEL_EXPORTER_OTLP_LOGS_ENDPOINT` environment variable to determine the endpoint. If not set, it will use the
42+
* `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable. If neither is set, it will return `None`.
43+
*
44+
* @return
45+
*/
46+
def logEndpoint = otelLogEndpoint
47+
3348
/** The OTLP endpoint to use for logging.
3449
*
3550
* Uses the `OTEL_EXPORTER_OTLP_LOGS_ENDPOINT` environment variable to determine the endpoint. If not set, it will use the
3651
* `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable. If neither is set, it will return `None`.
3752
*
3853
* @return
3954
*/
40-
def logEndpoint: ZIO[Any, Nothing, Option[String]] = OtlpEndpoint("OTEL_EXPORTER_OTLP_LOGS_ENDPOINT") match {
55+
final protected def otelLogEndpoint: ZIO[Any, Nothing, Option[String]] = OtlpEndpoint("OTEL_EXPORTER_OTLP_LOGS_ENDPOINT") match {
4156
case None =>
4257
ZIO.logInfo(
4358
"No OTLP logs endpoint configured, skipping OpenTelemetry logging setup. To enable it, set either OTEL_EXPORTER_OTLP_LOGS_ENDPOINT or OTEL_EXPORTER_OTLP_ENDPOINT environment variable."

observability/zio-opentelemetry/src/main/scala/sttp/tapir/server/ziopentelemetry/ZIOpenTelemetryTracing.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ class ZIOpenTelemetryTracing(
138138
case _ =>
139139
ZIO.when(response.isServerError)(
140140
spanError(span, finalize)(Left(response.code))
141-
) *> finalize
142-
*> ZIO.succeed(response)
141+
) *> finalize *> ZIO.succeed(response)
143142
})
144143

145144
private def handleStreamError(cause: Cause[Any], span: Span, finalize: UIO[Any]): UIO[Unit] =

0 commit comments

Comments
 (0)