Skip to content

Commit

Permalink
sdk: use TelemetryResource.default as an initial resource in `Telem…
Browse files Browse the repository at this point in the history
…etryResourceAutoConfigure`
  • Loading branch information
iRevive committed Mar 8, 2024
1 parent c6ad789 commit d9ae870
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -300,20 +300,22 @@ object OpenTelemetrySdk {
cfg.withOverrides(c(cfg))
)

def loadNoop: Resource[F, OpenTelemetrySdk[F]] =
def loadNoop(config: Config): Resource[F, AutoConfigured[F]] =
Resource.eval(
for {
_ <- Console[F].println(
s"OpenTelemetrySdk: the '${CommonConfigKeys.SdkDisabled}' set to 'true'. Using no-op implementation"
)
local <- LocalProvider[F, Context].local
} yield OpenTelemetrySdk.noop[F](Async[F], local)
sdk = OpenTelemetrySdk.noop[F](Async[F], local)
resource = TelemetryResource.empty
} yield Impl(sdk, resource, config)
)

def loadSdk(
config: Config,
resource: TelemetryResource
): Resource[F, OpenTelemetrySdk[F]] = {
): Resource[F, AutoConfigured[F]] = {
def makeLocalContext = LocalProvider[F, Context].local

Resource.eval(makeLocalContext).flatMap { implicit local =>
Expand All @@ -333,11 +335,12 @@ object OpenTelemetrySdk {

for {
tracerProvider <- tracerProviderConfigure.configure(config)
} yield new OpenTelemetrySdk(
MeterProvider.noop,
tracerProvider,
propagators
)
sdk = new OpenTelemetrySdk(
MeterProvider.noop,
tracerProvider,
propagators
)
} yield Impl(sdk, resource, config)
}
}
}
Expand All @@ -356,8 +359,8 @@ object OpenTelemetrySdk {
)
)

sdk <- if (isDisabled) loadNoop else loadSdk(config, resource)
} yield Impl[F](sdk, resource, config)
sdk <- if (isDisabled) loadNoop(config) else loadSdk(config, resource)
} yield sdk
}

private def merge[A](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class OpenTelemetrySdkSuite extends CatsEffectSuite {

private val DefaultSdk =
sdkToString(
TelemetryResource.empty,
TelemetryResource.default,
Sampler.parentBased(Sampler.AlwaysOn)
)

Expand Down Expand Up @@ -261,7 +261,7 @@ class OpenTelemetrySdkSuite extends CatsEffectSuite {
}

private def sdkToString(
resource: TelemetryResource = TelemetryResource.empty,
resource: TelemetryResource = TelemetryResource.default,
sampler: Sampler = Sampler.parentBased(Sampler.AlwaysOn),
propagators: ContextPropagators[Context] = ContextPropagators.of(
W3CTraceContextPropagator.default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private final class TelemetryResourceAutoConfigure[F[_]: MonadThrow]
ConfigurationError("Unable to decode resource attributes", e)
}
}
.map(_.to(Attributes))

val attempt = for {
disabledKeys <-
Expand All @@ -80,9 +81,10 @@ private final class TelemetryResourceAutoConfigure[F[_]: MonadThrow]
.flatten
.map(value => ResourceAttributes.ServiceName(value))

TelemetryResource(
Attributes.fromSpecific(attributes ++ serviceName.toSeq)
)
val default = TelemetryResource.default
val fromEnv = TelemetryResource(attributes ++ serviceName)

default.mergeUnsafe(fromEnv)
}

Resource.eval(MonadThrow[F].fromEither(attempt))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import munit.CatsEffectSuite

class TelemetryResourceAutoConfigureSuite extends CatsEffectSuite {

test("load from an empty config") {
test("load from an empty config - use default as a fallback") {
val config = Config(Map.empty, Map.empty, Map.empty)
TelemetryResourceAutoConfigure[IO].configure(config).use { resource =>
IO(assertEquals(resource, TelemetryResource.empty))
IO(assertEquals(resource, TelemetryResource.default))
}
}

test("load from the config") {
test("load from the config - use default as an initial resource") {
val props = Map(
"otel.service.name" -> "some-service",
"otel.resource.attributes" -> "key1=val1,key2=val2,key3=val3",
Expand All @@ -44,8 +44,12 @@ class TelemetryResourceAutoConfigureSuite extends CatsEffectSuite {
Attribute("service.name", "some-service")
)

val expected = TelemetryResource.default.mergeUnsafe(
TelemetryResource(expectedAttributes)
)

TelemetryResourceAutoConfigure[IO].configure(config).use { resource =>
IO(assertEquals(resource, TelemetryResource(expectedAttributes)))
IO(assertEquals(resource, expected))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class SdkTracesSuite extends CatsEffectSuite {

private val DefaultTraces =
tracesToString(
TelemetryResource.empty,
TelemetryResource.default,
Sampler.parentBased(Sampler.AlwaysOn)
)

Expand Down Expand Up @@ -258,7 +258,7 @@ class SdkTracesSuite extends CatsEffectSuite {
}

private def tracesToString(
resource: TelemetryResource = TelemetryResource.empty,
resource: TelemetryResource = TelemetryResource.default,
sampler: Sampler = Sampler.parentBased(Sampler.AlwaysOn),
propagators: ContextPropagators[Context] = ContextPropagators.of(
W3CTraceContextPropagator.default,
Expand Down

0 comments on commit d9ae870

Please sign in to comment.