diff --git a/observability/opentelemetry-api/src/main/java/org/wildfly/extension/opentelemetry/api/WildFlyOpenTelemetryConfig.java b/observability/opentelemetry-api/src/main/java/org/wildfly/extension/opentelemetry/api/WildFlyOpenTelemetryConfig.java index 0034b0ef4edc..6618c0d8a06b 100644 --- a/observability/opentelemetry-api/src/main/java/org/wildfly/extension/opentelemetry/api/WildFlyOpenTelemetryConfig.java +++ b/observability/opentelemetry-api/src/main/java/org/wildfly/extension/opentelemetry/api/WildFlyOpenTelemetryConfig.java @@ -23,7 +23,6 @@ public final class WildFlyOpenTelemetryConfig implements OpenTelemetryConfig { public static final String OTEL_EXPORTER_OTLP_ENDPOINT = "otel.exporter.otlp.endpoint"; public static final String OTEL_EXPORTER_OTLP_PROTOCOL = "otel.exporter.otlp.protocol"; public static final String OTEL_EXPORTER_OTLP_TIMEOUT = "otel.exporter.otlp.timeout"; - public static final String OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = "otel.exporter.otlp.traces.protocol"; public static final String OTEL_LOGS_EXPORTER = "otel.logs.exporter"; public static final String OTEL_PROPAGATORS = "otel.propagators"; public static final String OTEL_METRICS_EXPORTER = "otel.metrics.exporter"; @@ -36,62 +35,11 @@ public final class WildFlyOpenTelemetryConfig implements OpenTelemetryConfig { private final Map properties; private final boolean mpTelemetryInstalled; - public WildFlyOpenTelemetryConfig(Map properties, boolean mpTelemetryInstalled) { + WildFlyOpenTelemetryConfig(Map properties, boolean mpTelemetryInstalled) { this.properties = Collections.unmodifiableMap(properties); this.mpTelemetryInstalled = mpTelemetryInstalled; } - public WildFlyOpenTelemetryConfig(String serviceName, String exporter, String endpoint, - Long batchDelay, Long maxQueueSize, Long maxExportBatchSize, - Long exportTimeout, String spanProcessorType, String sampler, Double ratio, - boolean mpTelemetryInstalled, boolean vertxInstalled) { - Map config = new HashMap<>(); - // Default to on - addValue(config, OTEL_SDK_DISABLED, "false"); - - addValue(config, OTEL_SERVICE_NAME, serviceName); - addValue(config, OTEL_TRACES_EXPORTER, exporter); - addValue(config, OTEL_LOGS_EXPORTER, exporter); - addValue(config, OTEL_METRICS_EXPORTER, exporter); - addValue(config, OTEL_PROPAGATORS, "tracecontext,baggage"); - - - if (exporter.equals("otlp")) { - addValue(config, OTEL_EXPORTER_OTLP_ENDPOINT, endpoint); - addValue(config, OTEL_EXPORTER_OTLP_PROTOCOL, "grpc"); - addValue(config, OTEL_EXPORTER_OTLP_TIMEOUT, exportTimeout); - addValue(config, "otel.metric.export.interval", batchDelay); - } else { - throw new IllegalArgumentException("An unexpected exporter type was found: " + exporter); - } - - addValue(config, OTEL_BSP_SCHEDULE_DELAY, batchDelay); - addValue(config, OTEL_BSP_MAX_QUEUE_SIZE, maxQueueSize); - addValue(config, OTEL_BSP_MAX_EXPORT_BATCH_SIZE, maxExportBatchSize); - - if (sampler != null) { - switch (sampler) { - case "on": - addValue(config, OTEL_TRACES_SAMPLER, "always_on"); - break; - case "off": - addValue(config, OTEL_TRACES_SAMPLER, "always_off"); - break; - case "ratio": - addValue(config, OTEL_TRACES_SAMPLER, "traceidratio"); - addValue(config, OTEL_TRACES_SAMPLER_ARG, ratio); - break; - } - } - - - if (vertxInstalled) { - addValue(config, "otel.exporter.vertx.cdi.identifier", "vertx"); - } - properties = Collections.unmodifiableMap(config); - this.mpTelemetryInstalled = mpTelemetryInstalled; - } - @Override public Map properties() { return properties; @@ -101,13 +49,114 @@ public boolean isMpTelemetryInstalled() { return mpTelemetryInstalled; } - /** - * Only add the value to the config if it is non-null, and convert the type to String to - * satisfy library requirements. - */ - private void addValue(Map config, String key, Object value) { - if (value != null) { - config.put(key, value.toString()); + public static class Builder { + final Map properties = new HashMap<>(); + private boolean mpTelemetryInstalled; + + public Builder() { + addValue(OTEL_EXPORTER_OTLP_PROTOCOL, "grpc"); + addValue(OTEL_PROPAGATORS, "tracecontext,baggage"); + addValue(OTEL_SDK_DISABLED, "false"); + + } + + public Builder setServiceName(String serviceName) { + addValue(OTEL_SERVICE_NAME, serviceName); + return this; + } + + public Builder setSdkDisabled(boolean sdkDisabled) { + addValue(OTEL_SDK_DISABLED, "false"); + return this; + } + + public Builder setExporter(String exporter) { + if (!exporter.equals("otlp")) { + throw new IllegalArgumentException("An unexpected exporter type was found: " + exporter); + } + addValue(OTEL_TRACES_EXPORTER, exporter); + addValue(OTEL_LOGS_EXPORTER, exporter); + addValue(OTEL_METRICS_EXPORTER, exporter); + + return this; + } + + public Builder setOtlpEndpoint(String endpoint) { + addValue(OTEL_EXPORTER_OTLP_ENDPOINT, endpoint); + return this; + } + + public Builder setExportTimeout(long timeout) { + addValue(OTEL_EXPORTER_OTLP_TIMEOUT, timeout); + return this; + } + + public Builder setExportInterval(Long interval) { + addValue("otel.metric.export.interval", interval); + return this; + } + + public Builder setBatchDelay(long delay) { + addValue(OTEL_BSP_SCHEDULE_DELAY, delay); + return this; + } + + public Builder setMaxQueueSize(long maxQueueSize) { + addValue(OTEL_BSP_MAX_QUEUE_SIZE, maxQueueSize); + return this; + } + + public Builder setMaxExportBatchSize(long maxExportBatchSize) { + addValue(OTEL_BSP_MAX_EXPORT_BATCH_SIZE, maxExportBatchSize); + return this; + } + + public Builder setSampler(String sampler) { + if (sampler != null) { + switch (sampler) { + case "on": + addValue(OTEL_TRACES_SAMPLER, "always_on"); + break; + case "off": + addValue(OTEL_TRACES_SAMPLER, "always_off"); + break; + case "ratio": + addValue(OTEL_TRACES_SAMPLER, "traceidratio"); + break; + } + } + return this; + } + + public Builder setSamplerRatio(Double ratio) { + addValue(OTEL_TRACES_SAMPLER_ARG, ratio); + return this; + } + + public Builder setInjectVertx(boolean injectVertx) { + if (injectVertx) { + addValue("otel.exporter.vertx.cdi.identifier", "vertx"); + } + return this; + } + + public Builder setMicroProfileTelemetryInstalled(boolean microProfileTelemetryInstalled) { + this.mpTelemetryInstalled = microProfileTelemetryInstalled; + return this; + } + + public WildFlyOpenTelemetryConfig build() { + return new WildFlyOpenTelemetryConfig(properties, mpTelemetryInstalled); + } + + /** + * Only add the value to the config if it is non-null, and convert the type to String to + * satisfy library requirements. + */ + private void addValue(String key, Object value) { + if (value != null) { + properties.put(key, value.toString()); + } } } } diff --git a/observability/opentelemetry/src/main/java/org/wildfly/extension/opentelemetry/OpenTelemetrySubsystemRegistrar.java b/observability/opentelemetry/src/main/java/org/wildfly/extension/opentelemetry/OpenTelemetrySubsystemRegistrar.java index 471a48392c91..4d59eb2567d4 100644 --- a/observability/opentelemetry/src/main/java/org/wildfly/extension/opentelemetry/OpenTelemetrySubsystemRegistrar.java +++ b/observability/opentelemetry/src/main/java/org/wildfly/extension/opentelemetry/OpenTelemetrySubsystemRegistrar.java @@ -204,20 +204,19 @@ public ResourceServiceInstaller configure(OperationContext context, ModelNode mo String exporter = OpenTelemetrySubsystemRegistrar.EXPORTER.resolveModelAttribute(context, model).asString(); validateExporter(context, exporter); - final WildFlyOpenTelemetryConfig config = new WildFlyOpenTelemetryConfig( - OpenTelemetrySubsystemRegistrar.SERVICE_NAME.resolveModelAttribute(context, model).asStringOrNull(), - exporter, - OpenTelemetrySubsystemRegistrar.ENDPOINT.resolveModelAttribute(context, model).asStringOrNull(), - OpenTelemetrySubsystemRegistrar.BATCH_DELAY.resolveModelAttribute(context, model).asLongOrNull(), - OpenTelemetrySubsystemRegistrar.MAX_QUEUE_SIZE.resolveModelAttribute(context, model).asLongOrNull(), - OpenTelemetrySubsystemRegistrar.MAX_EXPORT_BATCH_SIZE.resolveModelAttribute(context, model).asLongOrNull(), - OpenTelemetrySubsystemRegistrar.EXPORT_TIMEOUT.resolveModelAttribute(context, model).asLongOrNull(), - OpenTelemetrySubsystemRegistrar.SPAN_PROCESSOR_TYPE.resolveModelAttribute(context, model).asStringOrNull(), - OpenTelemetrySubsystemRegistrar.SAMPLER.resolveModelAttribute(context, model).asStringOrNull(), - OpenTelemetrySubsystemRegistrar.RATIO.resolveModelAttribute(context, model).asDoubleOrNull(), - context.getCapabilityServiceSupport().hasCapability("org.wildfly.extension.microprofile.telemetry"), - context.hasOptionalCapability("org.wildfly.extension.vertx", OPENTELEMETRY_CAPABILITY, null) - ); + final WildFlyOpenTelemetryConfig config = new WildFlyOpenTelemetryConfig.Builder() + .setServiceName(OpenTelemetrySubsystemRegistrar.SERVICE_NAME.resolveModelAttribute(context, model).asStringOrNull()) + .setExporter(exporter) + .setOtlpEndpoint(OpenTelemetrySubsystemRegistrar.ENDPOINT.resolveModelAttribute(context, model).asStringOrNull()) + .setBatchDelay(OpenTelemetrySubsystemRegistrar.BATCH_DELAY.resolveModelAttribute(context, model).asLongOrNull()) + .setMaxQueueSize(OpenTelemetrySubsystemRegistrar.MAX_QUEUE_SIZE.resolveModelAttribute(context, model).asLongOrNull()) + .setMaxExportBatchSize(OpenTelemetrySubsystemRegistrar.MAX_EXPORT_BATCH_SIZE.resolveModelAttribute(context, model).asLongOrNull()) + .setExportTimeout(OpenTelemetrySubsystemRegistrar.EXPORT_TIMEOUT.resolveModelAttribute(context, model).asLongOrNull()) + .setSampler(OpenTelemetrySubsystemRegistrar.SAMPLER.resolveModelAttribute(context, model).asStringOrNull()) + .setSamplerRatio(OpenTelemetrySubsystemRegistrar.RATIO.resolveModelAttribute(context, model).asDoubleOrNull()) + .setMicroProfileTelemetryInstalled(context.hasOptionalCapability("org.wildfly.extension.microprofile.telemetry", OPENTELEMETRY_CAPABILITY, null)) + .setInjectVertx(context.hasOptionalCapability("org.wildfly.extension.vertx", OPENTELEMETRY_CAPABILITY, null)) + .build(); return CapabilityServiceInstaller.builder(OPENTELEMETRY_CONFIG_CAPABILITY, config) .withCaptor(openTelemetryConfig::set)