-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[telemetry] Add telemetry with first metric
- Loading branch information
1 parent
ca535ad
commit 2280c15
Showing
5 changed files
with
141 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
openbas-api/src/main/java/io/openbas/telemetry/OpenTelemetryConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.openbas.telemetry; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator; | ||
import io.opentelemetry.api.metrics.Meter; | ||
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; | ||
import io.opentelemetry.context.propagation.ContextPropagators; | ||
import io.opentelemetry.context.propagation.TextMapPropagator; | ||
import io.opentelemetry.exporter.logging.LoggingMetricExporter; | ||
import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
import io.opentelemetry.sdk.metrics.SdkMeterProvider; | ||
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import io.opentelemetry.semconv.ResourceAttributes; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.stereotype.Service; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class OpenTelemetryConfig { | ||
|
||
private final Environment env; | ||
|
||
@Bean | ||
public OpenTelemetry openTelemetry() { | ||
Resource resource = Resource.getDefault() | ||
.toBuilder() | ||
.put(ResourceAttributes.SERVICE_NAME, requireNonNull(this.env.getProperty("info.app.name"))) | ||
.put(ResourceAttributes.SERVICE_VERSION, requireNonNull(this.env.getProperty("info.app.version"))) | ||
.build(); | ||
|
||
SdkMeterProvider sdkMeterProvider = SdkMeterProvider.builder() | ||
.registerMetricReader(PeriodicMetricReader.builder(LoggingMetricExporter.create()).build()) | ||
.setResource(resource) | ||
.build(); | ||
|
||
// TODO: Add file exporter | ||
|
||
return OpenTelemetrySdk.builder() | ||
.setMeterProvider(sdkMeterProvider) | ||
.setPropagators(ContextPropagators.create( | ||
TextMapPropagator.composite(W3CTraceContextPropagator.getInstance(), W3CBaggagePropagator.getInstance()) | ||
)) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Meter meter(@NotNull final OpenTelemetry openTelemetry) { | ||
return openTelemetry.meterBuilder(requireNonNull(this.env.getProperty("info.app.version")).replace(" ", "-") + "-metric") | ||
.setInstrumentationVersion("1.0.0") | ||
.build(); | ||
} | ||
|
||
} | ||
|
37 changes: 37 additions & 0 deletions
37
openbas-api/src/main/java/io/openbas/telemetry/OpenTelemetryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.openbas.telemetry; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.metrics.LongCounter; | ||
import io.opentelemetry.api.metrics.Meter; | ||
import jakarta.annotation.PostConstruct; | ||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class OpenTelemetryService { | ||
|
||
private final Meter meter; | ||
|
||
private LongCounter longCounter; | ||
|
||
@PostConstruct | ||
private void init() { | ||
this.longCounter = this.meter | ||
.counterBuilder("app.login") | ||
.setDescription("Number of login connections") | ||
.setUnit("connections") | ||
.build(); | ||
} | ||
|
||
public void login(@NotBlank final String email) { | ||
this.longCounter.add( | ||
1, | ||
Attributes.of(AttributeKey.stringKey("user.email"), email) | ||
); | ||
this.longCounter.add(1); // Global login count | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters