Skip to content

Commit

Permalink
[telemetry] Add telemetry with first metric
Browse files Browse the repository at this point in the history
  • Loading branch information
RomuDeuxfois committed Mar 11, 2024
1 parent ca535ad commit 2280c15
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 5 deletions.
38 changes: 33 additions & 5 deletions openbas-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<opensaml.version>4.1.1</opensaml.version>
<springdoc.version>2.3.0</springdoc.version>
<springdoc-plugin.version>1.4</springdoc-plugin.version>
<opentelemetry.version>1.35.0</opentelemetry.version>
<opentelemetry-semconv.version>1.23.1-alpha</opentelemetry-semconv.version>
</properties>

<profiles>
Expand Down Expand Up @@ -71,6 +73,18 @@
</profile>
</profiles>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-bom</artifactId>
<version>${opentelemetry.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>io.openbas</groupId>
Expand Down Expand Up @@ -198,6 +212,25 @@
<artifactId>httpclient5</artifactId>
<version>${httpclient5.version}</version>
</dependency>
<!-- OpenTelemetry -->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-logging</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry.semconv</groupId>
<artifactId>opentelemetry-semconv</artifactId>
<version>${opentelemetry-semconv.version}</version>
</dependency>

<!-- TEST -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -215,11 +248,6 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
8 changes: 8 additions & 0 deletions openbas-api/src/main/java/io/openbas/rest/user/UserApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.openbas.rest.user.form.user.UpdateUserInput;
import io.openbas.service.MailingService;
import io.openbas.service.UserService;
import io.openbas.telemetry.OpenTelemetryService;
import jakarta.annotation.Resource;
import jakarta.transaction.Transactional;
import jakarta.validation.Valid;
Expand Down Expand Up @@ -41,6 +42,7 @@ public class UserApi extends RestBehavior {
private TagRepository tagRepository;
private UserService userService;
private MailingService mailingService;
private OpenTelemetryService openTelemetryService;

@Autowired
public void setMailingService(MailingService mailingService) {
Expand All @@ -67,13 +69,19 @@ public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}

@Autowired
public void setOpenTelemetryService(OpenTelemetryService openTelemetryService) {
this.openTelemetryService = openTelemetryService;
}

@PostMapping("/api/login")
public User login(@Valid @RequestBody LoginUserInput input) {
Optional<User> optionalUser = userRepository.findByEmailIgnoreCase(input.getLogin());
if (optionalUser.isPresent()) {
User user = optionalUser.get();
if (userService.isUserPasswordValid(user, input.getPassword())) {
userService.createUserSession(user);
this.openTelemetryService.login(user.getEmail());
return user;
}
}
Expand Down
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();
}

}

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
}

}
3 changes: 3 additions & 0 deletions openbas-api/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ logging.logback.rollingpolicy.file-name-pattern=${LOG_FILE}.-%d{yyyy-MM-dd}.%i
logging.logback.rollingpolicy.max-file-size=10MB
logging.logback.rollingpolicy.max-history=7

# Telemetry
logging.level.io.opentelemetry.exporter.logging=info

#############
# INJECTORS #
#############
Expand Down

0 comments on commit 2280c15

Please sign in to comment.