Skip to content

Commit

Permalink
Merge pull request #883 from iRevive/sdk-common/config-env
Browse files Browse the repository at this point in the history
sdk: propagate `Env`
  • Loading branch information
iRevive authored Jan 1, 2025
2 parents 5181f98 + 5dc10a4 commit 9c09184
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import cats.Parallel
import cats.effect.Async
import cats.effect.Resource
import cats.effect.std.Console
import cats.effect.std.Env
import cats.effect.std.Random
import cats.effect.std.SystemProperties
import cats.syntax.apply._
Expand Down Expand Up @@ -87,7 +88,7 @@ object OpenTelemetrySdk {
* @param customize
* a function for customizing the auto-configured SDK builder
*/
def autoConfigured[F[_]: Async: Parallel: SystemProperties: Console: LocalContextProvider](
def autoConfigured[F[_]: Async: Parallel: Env: SystemProperties: Console: LocalContextProvider](
customize: AutoConfigured.Builder[F] => AutoConfigured.Builder[F] = (a: AutoConfigured.Builder[F]) => a
): Resource[F, AutoConfigured[F]] =
customize(AutoConfigured.builder[F]).build
Expand Down Expand Up @@ -157,39 +158,31 @@ object OpenTelemetrySdk {
* @param customizer
* the customizer to add
*/
def addPropertiesCustomizer(
customizer: Config => Map[String, String]
): Builder[F]
def addPropertiesCustomizer(customizer: Config => Map[String, String]): Builder[F]

/** Adds the meter provider builder customizer. Multiple customizers can be added, and they will be applied in the
* order they were added.
*
* @param customizer
* the customizer to add
*/
def addMeterProviderCustomizer(
customizer: Customizer[SdkMeterProvider.Builder[F]]
): Builder[F]
def addMeterProviderCustomizer(customizer: Customizer[SdkMeterProvider.Builder[F]]): Builder[F]

/** Adds the tracer provider builder customizer. Multiple customizers can be added, and they will be applied in
* the order they were added.
*
* @param customizer
* the customizer to add
*/
def addTracerProviderCustomizer(
customizer: Customizer[SdkTracerProvider.Builder[F]]
): Builder[F]
def addTracerProviderCustomizer(customizer: Customizer[SdkTracerProvider.Builder[F]]): Builder[F]

/** Adds the telemetry resource customizer. Multiple customizers can be added, and they will be applied in the
* order they were added.
*
* @param customizer
* the customizer to add
*/
def addResourceCustomizer(
customizer: Customizer[TelemetryResource]
): Builder[F]
def addResourceCustomizer(customizer: Customizer[TelemetryResource]): Builder[F]

/** Adds the telemetry resource detector. Multiple detectors can be added, and the detected telemetry resources
* will be merged.
Expand All @@ -204,9 +197,7 @@ object OpenTelemetrySdk {
* @param detector
* the detector to add
*/
def addResourceDetector(
detector: TelemetryResourceDetector[F]
): Builder[F]
def addResourceDetector(detector: TelemetryResourceDetector[F]): Builder[F]

/** Adds both metric and span exporter configurers. Can be used to register exporters that aren't included in the
* SDK.
Expand All @@ -227,9 +218,7 @@ object OpenTelemetrySdk {
* @param configurer
* the configurer to add
*/
def addExportersConfigurer(
configurer: ExportersAutoConfigure[F]
): Builder[F]
def addExportersConfigurer(configurer: ExportersAutoConfigure[F]): Builder[F]

/** Adds the exporter configurer. Can be used to register exporters that aren't included in the SDK.
*
Expand All @@ -249,9 +238,7 @@ object OpenTelemetrySdk {
* @param configurer
* the configurer to add
*/
def addMetricExporterConfigurer(
configurer: AutoConfigure.Named[F, MetricExporter[F]]
): Builder[F]
def addMetricExporterConfigurer(configurer: AutoConfigure.Named[F, MetricExporter[F]]): Builder[F]

/** Adds the exporter configurer. Can be used to register exporters that aren't included in the SDK.
*
Expand All @@ -271,9 +258,7 @@ object OpenTelemetrySdk {
* @param configurer
* the configurer to add
*/
def addSpanExporterConfigurer(
configurer: AutoConfigure.Named[F, SpanExporter[F]]
): Builder[F]
def addSpanExporterConfigurer(configurer: AutoConfigure.Named[F, SpanExporter[F]]): Builder[F]

/** Adds the sampler configurer. Can be used to register samplers that aren't included in the SDK.
*
Expand All @@ -287,9 +272,7 @@ object OpenTelemetrySdk {
* @param configurer
* the configurer to add
*/
def addTextMapPropagatorConfigurer(
configurer: AutoConfigure.Named[F, TextMapPropagator[Context]]
): Builder[F]
def addTextMapPropagatorConfigurer(configurer: AutoConfigure.Named[F, TextMapPropagator[Context]]): Builder[F]

/** Creates [[OpenTelemetrySdk]] using the configuration of this builder.
*/
Expand All @@ -298,7 +281,7 @@ object OpenTelemetrySdk {

/** Creates a [[Builder]].
*/
def builder[F[_]: Async: Parallel: SystemProperties: Console: LocalContextProvider]: Builder[F] =
def builder[F[_]: Async: Parallel: Env: SystemProperties: Console: LocalContextProvider]: Builder[F] =
BuilderImpl(
customConfig = None,
propertiesLoader = Async[F].pure(Map.empty),
Expand All @@ -313,81 +296,57 @@ object OpenTelemetrySdk {
textMapPropagatorConfigurers = Set.empty
)

private final case class BuilderImpl[F[_]: Async: Parallel: SystemProperties: Console: LocalContextProvider](
private final case class BuilderImpl[F[_]: Async: Parallel: Env: SystemProperties: Console: LocalContextProvider](
customConfig: Option[Config],
propertiesLoader: F[Map[String, String]],
propertiesCustomizers: List[Config => Map[String, String]],
resourceCustomizer: Customizer[TelemetryResource],
meterProviderCustomizer: Customizer[SdkMeterProvider.Builder[F]],
tracerProviderCustomizer: Customizer[SdkTracerProvider.Builder[F]],
resourceDetectors: Set[TelemetryResourceDetector[F]],
metricExporterConfigurers: Set[
AutoConfigure.Named[F, MetricExporter[F]]
],
metricExporterConfigurers: Set[AutoConfigure.Named[F, MetricExporter[F]]],
spanExporterConfigurers: Set[AutoConfigure.Named[F, SpanExporter[F]]],
samplerConfigurers: Set[AutoConfigure.Named[F, Sampler[F]]],
textMapPropagatorConfigurers: Set[
AutoConfigure.Named[F, TextMapPropagator[Context]]
]
textMapPropagatorConfigurers: Set[AutoConfigure.Named[F, TextMapPropagator[Context]]]
) extends Builder[F] {

def withConfig(config: Config): Builder[F] =
copy(customConfig = Some(config))

def addPropertiesLoader(
loader: F[Map[String, String]]
): Builder[F] =
def addPropertiesLoader(loader: F[Map[String, String]]): Builder[F] =
copy(propertiesLoader = (this.propertiesLoader, loader).mapN(_ ++ _))

def addPropertiesCustomizer(
customizer: Config => Map[String, String]
): Builder[F] =
def addPropertiesCustomizer(customizer: Config => Map[String, String]): Builder[F] =
copy(propertiesCustomizers = this.propertiesCustomizers :+ customizer)

def addResourceCustomizer(
customizer: Customizer[TelemetryResource]
): Builder[F] =
def addResourceCustomizer(customizer: Customizer[TelemetryResource]): Builder[F] =
copy(resourceCustomizer = merge(this.resourceCustomizer, customizer))

def addMeterProviderCustomizer(
customizer: Customizer[SdkMeterProvider.Builder[F]]
): Builder[F] =
def addMeterProviderCustomizer(customizer: Customizer[SdkMeterProvider.Builder[F]]): Builder[F] =
copy(meterProviderCustomizer = merge(this.meterProviderCustomizer, customizer))

def addTracerProviderCustomizer(
customizer: Customizer[SdkTracerProvider.Builder[F]]
): Builder[F] =
def addTracerProviderCustomizer(customizer: Customizer[SdkTracerProvider.Builder[F]]): Builder[F] =
copy(tracerProviderCustomizer = merge(this.tracerProviderCustomizer, customizer))

def addResourceDetector(
detector: TelemetryResourceDetector[F]
): Builder[F] =
def addResourceDetector(detector: TelemetryResourceDetector[F]): Builder[F] =
copy(resourceDetectors = this.resourceDetectors + detector)

def addExportersConfigurer(
configurer: ExportersAutoConfigure[F]
): Builder[F] =
def addExportersConfigurer(configurer: ExportersAutoConfigure[F]): Builder[F] =
copy(
metricExporterConfigurers = metricExporterConfigurers + configurer.metricExporterAutoConfigure,
spanExporterConfigurers = spanExporterConfigurers + configurer.spanExporterAutoConfigure
)

def addMetricExporterConfigurer(
configurer: AutoConfigure.Named[F, MetricExporter[F]]
): Builder[F] =
def addMetricExporterConfigurer(configurer: AutoConfigure.Named[F, MetricExporter[F]]): Builder[F] =
copy(metricExporterConfigurers = metricExporterConfigurers + configurer)

def addSpanExporterConfigurer(
configurer: AutoConfigure.Named[F, SpanExporter[F]]
): Builder[F] =
def addSpanExporterConfigurer(configurer: AutoConfigure.Named[F, SpanExporter[F]]): Builder[F] =
copy(spanExporterConfigurers = spanExporterConfigurers + configurer)

def addSamplerConfigurer(configurer: AutoConfigure.Named[F, Sampler[F]]): Builder[F] =
copy(samplerConfigurers = samplerConfigurers + configurer)

def addTextMapPropagatorConfigurer(
configurer: AutoConfigure.Named[F, TextMapPropagator[Context]]
): Builder[F] =
def addTextMapPropagatorConfigurer(configurer: AutoConfigure.Named[F, TextMapPropagator[Context]]): Builder[F] =
copy(textMapPropagatorConfigurers = textMapPropagatorConfigurers + configurer)

def build: Resource[F, AutoConfigured[F]] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ object Config {
* @param default
* the default properties
*/
def load[F[_]: Sync](default: Map[String, String]): F[Config] =
def load[F[_]: Sync: Env](default: Map[String, String]): F[Config] =
for {
envVars <- Env.make[F].entries
envVars <- Env[F].entries
systemProps <- Sync[F].delay(sys.props.toMap)
} yield apply(systemProps, envVars.toMap, default)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import cats.Applicative
import cats.effect.Async
import cats.effect.Resource
import cats.effect.std.Console
import cats.effect.std.Env
import cats.effect.std.Random
import cats.effect.std.SystemProperties
import cats.mtl.Ask
Expand Down Expand Up @@ -71,7 +72,7 @@ object SdkMetrics {
* @param customize
* a function for customizing the auto-configured SDK builder
*/
def autoConfigured[F[_]: Async: SystemProperties: Console](
def autoConfigured[F[_]: Async: Env: SystemProperties: Console](
customize: AutoConfigured.Builder[F] => AutoConfigured.Builder[F] = (a: AutoConfigured.Builder[F]) => a
): Resource[F, SdkMetrics[F]] =
customize(AutoConfigured.builder[F]).build
Expand Down Expand Up @@ -112,29 +113,23 @@ object SdkMetrics {
* @param customizer
* the customizer to add
*/
def addPropertiesCustomizer(
customizer: Config => Map[String, String]
): Builder[F]
def addPropertiesCustomizer(customizer: Config => Map[String, String]): Builder[F]

/** Adds the meter provider builder customizer. Multiple customizers can be added, and they will be applied in the
* order they were added.
*
* @param customizer
* the customizer to add
*/
def addMeterProviderCustomizer(
customizer: Customizer[SdkMeterProvider.Builder[F]]
): Builder[F]
def addMeterProviderCustomizer(customizer: Customizer[SdkMeterProvider.Builder[F]]): Builder[F]

/** Adds the telemetry resource customizer. Multiple customizers can be added, and they will be applied in the
* order they were added.
*
* @param customizer
* the customizer to add
*/
def addResourceCustomizer(
customizer: Customizer[TelemetryResource]
): Builder[F]
def addResourceCustomizer(customizer: Customizer[TelemetryResource]): Builder[F]

/** Adds the telemetry resource detector. Multiple detectors can be added, and the detected telemetry resources
* will be merged.
Expand All @@ -149,9 +144,7 @@ object SdkMetrics {
* @param detector
* the detector to add
*/
def addResourceDetector(
detector: TelemetryResourceDetector[F]
): Builder[F]
def addResourceDetector(detector: TelemetryResourceDetector[F]): Builder[F]

/** Adds the exporter configurer. Can be used to register exporters that aren't included in the SDK.
*
Expand All @@ -171,9 +164,7 @@ object SdkMetrics {
* @param configurer
* the configurer to add
*/
def addExporterConfigurer(
configurer: AutoConfigure.Named[F, MetricExporter[F]]
): Builder[F]
def addExporterConfigurer(configurer: AutoConfigure.Named[F, MetricExporter[F]]): Builder[F]

/** Creates [[SdkMetrics]] using the configuration of this builder.
*/
Expand All @@ -182,7 +173,7 @@ object SdkMetrics {

/** Creates a [[Builder]].
*/
def builder[F[_]: Async: SystemProperties: Console]: Builder[F] =
def builder[F[_]: Async: Env: SystemProperties: Console]: Builder[F] =
BuilderImpl(
customConfig = None,
propertiesLoader = Async[F].pure(Map.empty),
Expand All @@ -193,7 +184,7 @@ object SdkMetrics {
exporterConfigurers = Set.empty
)

private final case class BuilderImpl[F[_]: Async: SystemProperties: Console](
private final case class BuilderImpl[F[_]: Async: Env: SystemProperties: Console](
customConfig: Option[Config],
propertiesLoader: F[Map[String, String]],
propertiesCustomizers: List[Config => Map[String, String]],
Expand All @@ -206,34 +197,22 @@ object SdkMetrics {
def withConfig(config: Config): Builder[F] =
copy(customConfig = Some(config))

def addPropertiesLoader(
loader: F[Map[String, String]]
): Builder[F] =
def addPropertiesLoader(loader: F[Map[String, String]]): Builder[F] =
copy(propertiesLoader = (this.propertiesLoader, loader).mapN(_ ++ _))

def addPropertiesCustomizer(
customizer: Config => Map[String, String]
): Builder[F] =
def addPropertiesCustomizer(customizer: Config => Map[String, String]): Builder[F] =
copy(propertiesCustomizers = this.propertiesCustomizers :+ customizer)

def addResourceCustomizer(
customizer: Customizer[TelemetryResource]
): Builder[F] =
def addResourceCustomizer(customizer: Customizer[TelemetryResource]): Builder[F] =
copy(resourceCustomizer = merge(this.resourceCustomizer, customizer))

def addMeterProviderCustomizer(
customizer: Customizer[SdkMeterProvider.Builder[F]]
): Builder[F] =
def addMeterProviderCustomizer(customizer: Customizer[SdkMeterProvider.Builder[F]]): Builder[F] =
copy(meterProviderCustomizer = merge(this.meterProviderCustomizer, customizer))

def addResourceDetector(
detector: TelemetryResourceDetector[F]
): Builder[F] =
def addResourceDetector(detector: TelemetryResourceDetector[F]): Builder[F] =
copy(resourceDetectors = this.resourceDetectors + detector)

def addExporterConfigurer(
configurer: AutoConfigure.Named[F, MetricExporter[F]]
): Builder[F] =
def addExporterConfigurer(configurer: AutoConfigure.Named[F, MetricExporter[F]]): Builder[F] =
copy(exporterConfigurers = this.exporterConfigurers + configurer)

def build: Resource[F, SdkMetrics[F]] = {
Expand Down
Loading

0 comments on commit 9c09184

Please sign in to comment.