Skip to content

Commit

Permalink
refactor: observability
Browse files Browse the repository at this point in the history
  • Loading branch information
oproprioleonardo committed Nov 10, 2024
1 parent 062857c commit 0033a3b
Show file tree
Hide file tree
Showing 17 changed files with 479 additions and 38 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ bin/
app.key
app.pub
*.key
*.pub
*.pub

prometheus-data/
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
FROM openjdk:21-slim-buster

WORKDIR /ticketsifsp/api/
ARG JAR_FILE=tickets-ifsp-api.jar
WORKDIR /ticketsapi/api/
ARG JAR_FILE=tickets-api.jar

COPY ./build/libs/${JAR_FILE} ./api.jar

VOLUME /ticketsifsp/uploads
VOLUME /ticketsapi/uploads

ENTRYPOINT ["java","-jar","api.jar", "-Dfile.encoding=UTF-8", "-Dsun.jnu.encoding=UTF-8"]
60 changes: 60 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
services:
db:
container_name: psql-db
image: postgres:16.4
environment:
POSTGRES_DB: ifspcbt
POSTGRES_USER: root
POSTGRES_PASSWORD: password
TZ: America/Sao_Paulo
volumes:
- db_data:/var/lib/postgresql/data
ports:
- "5432:5432"
networks:
- ifspcbt
restart: always

backend:
container_name: ifspcbt-api
image: ghcr.io/ifspcbt-devspace/ifspcbt-devspace/tickets-ifsp-api:latest
environment:
- TZ=America/Sao_Paulo
- JWT_SECRET_KEY=secret
- MAIL_HOST=smtp.gmail.com
- MAIL_PORT=587
- MAIL_USERNAME=example@gmail.com
- MAIL_PASSWORD=password
- DATASOURCE_URL=jdbc:postgresql://localhost:5432/ifspcbt
- DATASOURCE_USERNAME=root
- DATASOURCE_PASSWORD=password
- ENCRYPTOR_VALUE=secret
- FRONTEND_TICKET_CHECK_URL=https://eventos.ifspcbt.shop/admin/ticket/%s/check
- MERCADOPAGO_ACCESSTOKEN=token
- ENV_NAME=dev
- LOG_PATH=/ticketsapi/logs/
- UPLOAD_PATH=/ticketsapi/uploads/
volumes:
- /home/ticketsapi/uploads:/ticketsapi/uploads
- /home/ticketsapi/logs:/ticketsapi/logs
ports:
- "8091:8091"
depends_on:
- db
networks:
- ifspcbt

watchtower:
privileged: true
image: containrrr/watchtower
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /root/.docker/config.json:/config.json
command: --interval 30

volumes:
db_data:

networks:
ifspcbt:
driver: bridge
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ public interface IDomainEvent extends Serializable {

String subject();

String message();

String source();

String id();



}
36 changes: 31 additions & 5 deletions infrastructure/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ plugins {
id 'io.spring.dependency-management' version '1.1.5'
}

dependencyManagement {
imports {
mavenBom("io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom:2.6.0")
}
}

group = 'br.com.ifsp.tickets'
version = '0.2.0'
description = 'infrastructure'
Expand Down Expand Up @@ -35,19 +41,39 @@ dependencies {
implementation(project(":domain"))
implementation(project(":application"))


implementation group: 'org.jasypt', name: 'jasypt', version: '1.9.2'

// mercadopago sdk
implementation group: 'com.mercadopago', name: 'sdk-java', version: '2.1.29'

implementation 'io.micrometer:micrometer-registry-prometheus'
// spring boot
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0'
implementation "org.springframework.boot:spring-boot-starter-aop"

// observability
implementation 'io.micrometer:micrometer-registry-otlp'

implementation 'io.micrometer:micrometer-tracing-bridge-otel'
implementation 'io.opentelemetry:opentelemetry-api:1.38.0'
implementation 'io.opentelemetry:opentelemetry-context:1.38.0'
implementation 'io.opentelemetry:opentelemetry-sdk:1.38.0'
implementation 'io.opentelemetry:opentelemetry-sdk-trace:1.38.0'
implementation 'io.opentelemetry:opentelemetry-sdk-metrics:1.38.0'
implementation 'io.opentelemetry:opentelemetry-sdk-common:1.38.0'
implementation 'io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi:1.38.0'
implementation 'io.opentelemetry:opentelemetry-extension-trace-propagators:1.38.0'
implementation 'io.opentelemetry:opentelemetry-exporter-common:1.38.0'
implementation 'io.opentelemetry:opentelemetry-exporter-otlp:1.38.0'
implementation 'io.opentelemetry:opentelemetry-exporter-otlp-common:1.38.0'
implementation 'io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-semconv:1.33.6-alpha'

implementation 'io.opentelemetry.instrumentation:opentelemetry-logback-appender-1.0:1.33.6-alpha'

implementation 'jakarta.mail:jakarta.mail-api:2.1.3'
implementation 'jakarta.activation:jakarta.activation-api:2.1.3'
Expand Down Expand Up @@ -75,7 +101,7 @@ dependencies {
}

bootJar {
archiveFileName.set("tickets-ifsp-api.${archiveExtension.get()}")
archiveFileName.set("tickets-api.${archiveExtension.get()}")
destinationDirectory = file("../build/libs")
}

Expand All @@ -86,9 +112,9 @@ test {
springBoot {
buildInfo {
properties {
artifact = "tickets-ifsp-api.jar"
artifact = "tickets-api.jar"
group = "br.com.ifsp.tickets"
name = "TicketsIFSP API"
name = "Tickets API"
version = "0.2.0"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package br.com.ifsp.tickets.infra;

import br.com.ifsp.tickets.infra.config.WebServerConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {

public static void main(String[] args) {
SpringApplication.run(WebServerConfig.class, args);
SpringApplication.run(Main.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package br.com.ifsp.tickets.infra.config;

import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.config.MeterFilter;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter;
import io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.logs.LogRecordProcessor;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;
import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.semconv.ResourceAttributes;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.time.ZoneId;
import java.util.List;
import java.util.UUID;

@Configuration
public class ObservabilityConfig {

@Bean
public MeterFilter commonTagsMeterFilter() {
return MeterFilter.commonTags(
List.of(
Tag.of("instance.uuid", UUID.randomUUID().toString()),
Tag.of("zone.id", ZoneId.systemDefault().toString())
)
);
}

@Bean
public OpenTelemetry openTelemetry(SdkLoggerProvider sdkLoggerProvider, SdkTracerProvider sdkTracerProvider, ContextPropagators contextPropagators) {
final OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk.builder()
.setLoggerProvider(sdkLoggerProvider)
.setTracerProvider(sdkTracerProvider)
.setPropagators(contextPropagators)
.build();
OpenTelemetryAppender.install(openTelemetrySdk);
return openTelemetrySdk;
}

@Bean
public SdkLoggerProvider otelSdkLoggerProvider(Environment environment, ObjectProvider<LogRecordProcessor> logRecordProcessors) {
final String applicationName = environment.getProperty("spring.application.name", "tickets-api");
final Resource springResource = Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, applicationName));
final SdkLoggerProviderBuilder builder = SdkLoggerProvider.builder()
.setResource(Resource.getDefault().merge(springResource));
logRecordProcessors.orderedStream().forEach(builder::addLogRecordProcessor);
return builder.build();
}

@Bean
public LogRecordProcessor otelLogRecordProcessor() {
return BatchLogRecordProcessor
.builder(
OtlpGrpcLogRecordExporter.builder()
.setEndpoint("http://localhost:4317")
.build())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
Expand All @@ -17,12 +17,11 @@
@Slf4j
public class LocalFileStorage implements IFileStorage {

private final Path rootPath = FileSystems.getDefault().getRootDirectories().iterator().next();
@Value("${upload-dir}")
private String uploadDir;

private Path getUploadDir() {
final Path path = rootPath.resolve(uploadDir);
final Path path = Path.of(URI.create(uploadDir));
if (Files.notExists(path)) {
try {
Files.createDirectories(path);
Expand Down
35 changes: 26 additions & 9 deletions infrastructure/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,35 @@ mercadopago:
access:
token: ${MERCADOPAGO_ACCESSTOKEN}

logging:
file:
path: ${LOG_PATH}
pattern:
dateformat: "dd/MM/yyyy HH:mm:ss.SSS"

management:
endpoints:
web:
exposure:
include: [ "prometheus", "health" ]
endpoint:
metrics:
enabled: true
health:
show-details: always
prometheus:
include: health,metrics
base-path: /v1/actuator
otlp:
metrics:
export:
enabled: true
url: http://localhost:4318/v1/metrics
step: 10s
tracing:
endpoint: http://localhost:4318/v1/traces
tracing:
sampling:
probability: 1.0
metrics:
tags:
department: ${SERVICE_DEPARTMENT:software}
service: ${spring.application.name}
env: ${SERVICE_ENV:${ENV_NAME}}
region: ${SERVICE_REGION:brazil-south}
instance.id: ${SERVICE_INSTANCE_ID:default}

spring:
mail:
Expand Down Expand Up @@ -65,6 +80,8 @@ spring:
type: com.zaxxer.hikari.HikariDataSource
username: ${DATASOURCE_USERNAME}
password: ${DATASOURCE_PASSWORD}
application:
name: tickets-api

server:
error:
Expand All @@ -81,4 +98,4 @@ server:
force: true

fe-ticket-check-url: ${FRONTEND_TICKET_CHECK_URL}
upload-dir: ticketsifsp/uploads/
upload-dir: ${UPLOAD_PATH}
4 changes: 2 additions & 2 deletions infrastructure/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
## ## ## ## ## ## ## ## ######### ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ##
## #### ###### ## ## ######## ## ###### ## ## ## ####
Application: TicketsIFSP-API
Version: v1.0.0
Application: Tickets-API
Version: v0.2.0
Authors:
- Leonardo
- Gustavo
13 changes: 0 additions & 13 deletions infrastructure/src/main/resources/env-example.txt

This file was deleted.

Loading

0 comments on commit 0033a3b

Please sign in to comment.