Skip to content

Commit

Permalink
Add OTLP exporter and static metric
Browse files Browse the repository at this point in the history
  • Loading branch information
RomuDeuxfois committed Mar 20, 2024
1 parent 51ad3e9 commit 873f857
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
4 changes: 4 additions & 0 deletions openbas-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-logging</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry.semconv</groupId>
<artifactId>opentelemetry-semconv</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +21,7 @@
import org.springframework.stereotype.Service;

import static java.util.Objects.requireNonNull;
import static org.springframework.util.StringUtils.hasText;

@Service
@RequiredArgsConstructor
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
}

}

0 comments on commit 873f857

Please sign in to comment.