diff --git a/openbas-api/pom.xml b/openbas-api/pom.xml index c7272a4246..53792c6da4 100644 --- a/openbas-api/pom.xml +++ b/openbas-api/pom.xml @@ -225,6 +225,10 @@ io.opentelemetry opentelemetry-exporter-logging + + io.opentelemetry + opentelemetry-exporter-otlp + io.opentelemetry.semconv opentelemetry-semconv diff --git a/openbas-api/src/main/java/io/openbas/telemetry/InitTelemetryCommandLineRunner.java b/openbas-api/src/main/java/io/openbas/telemetry/InitTelemetryCommandLineRunner.java new file mode 100644 index 0000000000..3e60e3b406 --- /dev/null +++ b/openbas-api/src/main/java/io/openbas/telemetry/InitTelemetryCommandLineRunner.java @@ -0,0 +1,19 @@ +package io.openbas.telemetry; + +import lombok.RequiredArgsConstructor; +import org.springframework.boot.CommandLineRunner; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +@Order(2) +public class InitTelemetryCommandLineRunner implements CommandLineRunner { + + private final OpenTelemetryService openTelemetryService; + + @Override + public void run(String... args) { + this.openTelemetryService.registerVersionMetric(); + } +} diff --git a/openbas-api/src/main/java/io/openbas/telemetry/OpenTelemetryConfig.java b/openbas-api/src/main/java/io/openbas/telemetry/OpenTelemetryConfig.java index a6aaaf8c6c..a5426d774e 100644 --- a/openbas-api/src/main/java/io/openbas/telemetry/OpenTelemetryConfig.java +++ b/openbas-api/src/main/java/io/openbas/telemetry/OpenTelemetryConfig.java @@ -7,8 +7,10 @@ import io.opentelemetry.context.propagation.ContextPropagators; import io.opentelemetry.context.propagation.TextMapPropagator; import io.opentelemetry.exporter.logging.LoggingMetricExporter; +import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.metrics.SdkMeterProvider; +import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder; import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.semconv.ResourceAttributes; @@ -19,6 +21,7 @@ import org.springframework.stereotype.Service; import static java.util.Objects.requireNonNull; +import static org.springframework.util.StringUtils.hasText; @Service @RequiredArgsConstructor @@ -34,13 +37,22 @@ public OpenTelemetry openTelemetry() { .put(ResourceAttributes.SERVICE_VERSION, requireNonNull(this.env.getProperty("info.app.version"))) .build(); - SdkMeterProvider sdkMeterProvider = SdkMeterProvider.builder() - .registerMetricReader(PeriodicMetricReader.builder(LoggingMetricExporter.create()).build()) + // Log exporter + SdkMeterProviderBuilder sdkMeterProviderBuilder = SdkMeterProvider.builder() + .registerMetricReader(PeriodicMetricReader.builder(LoggingMetricExporter.create()).build()); + + // OTLP exporter + String exporterOtlpEndpoint = this.env.getProperty("telemetry.exporter.otlp.endpoint"); + if (hasText(exporterOtlpEndpoint)) { + sdkMeterProviderBuilder.registerMetricReader(PeriodicMetricReader.builder( + OtlpHttpMetricExporter.builder().setEndpoint(exporterOtlpEndpoint).build() + ).build()); + } + + SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder .setResource(resource) .build(); - // TODO: Add file exporter - return OpenTelemetrySdk.builder() .setMeterProvider(sdkMeterProvider) .setPropagators(ContextPropagators.create( diff --git a/openbas-api/src/main/java/io/openbas/telemetry/OpenTelemetryService.java b/openbas-api/src/main/java/io/openbas/telemetry/OpenTelemetryService.java index 545943d952..6ca4161014 100644 --- a/openbas-api/src/main/java/io/openbas/telemetry/OpenTelemetryService.java +++ b/openbas-api/src/main/java/io/openbas/telemetry/OpenTelemetryService.java @@ -4,16 +4,22 @@ import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.metrics.LongCounter; import io.opentelemetry.api.metrics.Meter; +import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; import jakarta.annotation.PostConstruct; import jakarta.validation.constraints.NotBlank; import lombok.RequiredArgsConstructor; +import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; +import java.util.Objects; + @Service @RequiredArgsConstructor public class OpenTelemetryService { - private static final String PREFIX_PRODUCT; + private static final String PREFIX_PRODUCT = "openbas"; + + private final Environment env; private final Meter meter; @@ -36,4 +42,17 @@ public void login(@NotBlank final String email) { this.longCounter.add(1); // Global login count } + // -- STATIC -- + + // Not sure if it's the good way + public void registerVersionMetric() { + ObservableDoubleMeasurement longGauge = this.meter + .gaugeBuilder(PREFIX_PRODUCT + "app.version") + .setDescription("Software version") + .setUnit("version") + .buildObserver(); + String appVersion = Objects.requireNonNull(this.env.getProperty("info.app.version")); + longGauge.record(1, Attributes.of(AttributeKey.stringKey("app.version"), appVersion)); + } + }