From 56ecd12300251b9d6e6301304dddd3b91a2ac87b Mon Sep 17 00:00:00 2001 From: Alar Floren Date: Fri, 10 May 2024 01:10:49 +0300 Subject: [PATCH] Update Ruuter to SpringBoot3 --- Dockerfile | 4 +-- build.gradle | 18 +++++++++-- gradle/wrapper/gradle-wrapper.properties | 2 +- .../configuration/OpenTelemetryConfig.java | 32 +++++++++++++++++++ .../ruuter/controller/DslController.java | 6 +--- .../buerokratt/ruuter/domain/DslInstance.java | 5 ++- .../ruuter/domain/steps/DslStep.java | 7 ++-- .../ruuter/domain/steps/http/HttpStep.java | 2 +- .../buerokratt/ruuter/service/DslService.java | 2 +- src/main/resources/application.yml | 9 ++++++ 10 files changed, 67 insertions(+), 20 deletions(-) create mode 100644 src/main/java/ee/buerokratt/ruuter/configuration/OpenTelemetryConfig.java diff --git a/Dockerfile b/Dockerfile index ef68c582..5d4836e3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM eclipse-temurin:17-jdk-alpine as build +FROM eclipse-temurin:21-jdk-alpine as build WORKDIR /workspace/app COPY gradlew . @@ -12,7 +12,7 @@ RUN chmod 754 ./gradlew RUN ./gradlew -Pprod clean bootJar RUN mkdir -p build/libs && (cd build/libs; jar -xf *.jar) -FROM eclipse-temurin:17-jdk-alpine +FROM eclipse-temurin:21-jdk-alpine VOLUME /build/tmp ARG DEPENDENCY=/workspace/app/build/libs diff --git a/build.gradle b/build.gradle index ddf5e29f..9a17574e 100644 --- a/build.gradle +++ b/build.gradle @@ -1,10 +1,16 @@ +import org.springframework.boot.loader.tools.LoaderImplementation + plugins { - id 'org.springframework.boot' version '2.7.0' + id 'org.springframework.boot' version '3.2.5' id 'io.spring.dependency-management' version '1.1.3' id 'java' id "org.sonarqube" version "3.5.0.2730" } +bootJar { + loaderImplementation = LoaderImplementation.CLASSIC +} + group = 'ee.buerokratt' version = '0.0.1-SNAPSHOT' @@ -27,7 +33,13 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-webflux' - implementation 'org.springframework.cloud:spring-cloud-starter-sleuth:3.1.10' + + implementation platform('io.opentelemetry:opentelemetry-bom:1.15.0') + implementation 'io.opentelemetry:opentelemetry-api' + implementation 'io.opentelemetry:opentelemetry-sdk' + implementation 'io.opentelemetry:opentelemetry-context' + implementation 'io.opentelemetry:opentelemetry-exporter-logging' + implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml' implementation 'org.graalvm.js:js:23.0.1' implementation 'org.graalvm.js:js-scriptengine:23.0.1' @@ -45,7 +57,7 @@ dependencies { testImplementation 'org.mockito:mockito-core:5.6.0' testImplementation 'org.mockito:mockito-inline:4.9.0' testImplementation 'org.mockito:mockito-junit-jupiter:5.6.0' - testImplementation "com.github.tomakehurst:wiremock-jre8:2.35.0" + testImplementation 'com.github.tomakehurst:wiremock-jre8:2.35.1' } tasks.named('test') { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 00e33ede..48c0a02c 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/ee/buerokratt/ruuter/configuration/OpenTelemetryConfig.java b/src/main/java/ee/buerokratt/ruuter/configuration/OpenTelemetryConfig.java new file mode 100644 index 00000000..5cd6851d --- /dev/null +++ b/src/main/java/ee/buerokratt/ruuter/configuration/OpenTelemetryConfig.java @@ -0,0 +1,32 @@ +package ee.buerokratt.ruuter.configuration; + +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.sdk.OpenTelemetrySdk; +import io.opentelemetry.sdk.trace.SdkTracerProvider; +import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; +import io.opentelemetry.exporter.logging.LoggingSpanExporter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class OpenTelemetryConfig { + + @Bean + public OpenTelemetry openTelemetry() { + // Configure the SDK (feel free to replace the exporter with one that suits your needs) + SdkTracerProvider tracerProvider = SdkTracerProvider.builder() + .addSpanProcessor(BatchSpanProcessor.builder(new LoggingSpanExporter()).build()) + .build(); + + return OpenTelemetrySdk.builder() + .setTracerProvider(tracerProvider) + .buildAndRegisterGlobal(); + } + + @Bean + public Tracer tracer() { + // This assumes that a default OpenTelemetry has been configured elsewhere + return OpenTelemetry.noop().getTracer("ee.buerokratt.ruuter"); + } +} diff --git a/src/main/java/ee/buerokratt/ruuter/controller/DslController.java b/src/main/java/ee/buerokratt/ruuter/controller/DslController.java index 65125cfc..9651c8cc 100644 --- a/src/main/java/ee/buerokratt/ruuter/controller/DslController.java +++ b/src/main/java/ee/buerokratt/ruuter/controller/DslController.java @@ -4,9 +4,7 @@ import ee.buerokratt.ruuter.domain.DslInstance; import ee.buerokratt.ruuter.domain.RuuterResponse; import ee.buerokratt.ruuter.service.DslService; -import ee.buerokratt.ruuter.service.exception.StepExecutionException; import ee.buerokratt.ruuter.util.LoggingUtils; -import io.swagger.v3.core.util.Json; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -17,13 +15,11 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import javax.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequest; import java.io.IOException; -import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.Arrays; -import java.util.Base64; import java.util.HashMap; import java.util.Map; diff --git a/src/main/java/ee/buerokratt/ruuter/domain/DslInstance.java b/src/main/java/ee/buerokratt/ruuter/domain/DslInstance.java index 74ba3e1f..58c58560 100644 --- a/src/main/java/ee/buerokratt/ruuter/domain/DslInstance.java +++ b/src/main/java/ee/buerokratt/ruuter/domain/DslInstance.java @@ -9,10 +9,11 @@ import ee.buerokratt.ruuter.service.OpenSearchSender; import ee.buerokratt.ruuter.service.exception.StepExecutionException; import ee.buerokratt.ruuter.util.LoggingUtils; +import io.opentelemetry.api.trace.Tracer; import lombok.Data; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.cloud.sleuth.Tracer; + import org.springframework.http.HttpStatus; import java.util.HashMap; @@ -20,7 +21,6 @@ import java.util.Map; import java.util.Objects; import java.util.function.Function; -import java.util.logging.Logger; import java.util.stream.Collectors; @Slf4j @Data @@ -59,7 +59,6 @@ public void execute() { recursions = stepNames.stream().collect(Collectors.toMap(Function.identity(), a -> 0)); try { executeStep(stepNames.get(0), stepNames); - } catch (Exception e) { LoggingUtils.logError(log, "Error executing DSL: %s".formatted(name), requestOrigin, "", e); clearReturnValues(); diff --git a/src/main/java/ee/buerokratt/ruuter/domain/steps/DslStep.java b/src/main/java/ee/buerokratt/ruuter/domain/steps/DslStep.java index 0e668e13..88162154 100644 --- a/src/main/java/ee/buerokratt/ruuter/domain/steps/DslStep.java +++ b/src/main/java/ee/buerokratt/ruuter/domain/steps/DslStep.java @@ -9,8 +9,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.cloud.sleuth.Span; -import org.springframework.cloud.sleuth.Tracer; +import io.opentelemetry.api.trace.Span; import org.springframework.http.HttpStatus; import org.springframework.web.client.HttpClientErrorException; @@ -30,10 +29,10 @@ public abstract class DslStep { private boolean reloadDsl = false; public final void execute(DslInstance di) throws StepExecutionException { - Span newSpan = di.getTracer().nextSpan().name(name); + Span newSpan = di.getTracer().spanBuilder(name).startSpan(); long startTime = System.currentTimeMillis(); - try (Tracer.SpanInScope ws = di.getTracer().withSpan(newSpan.start())) { + try { if (sleep != null) { Thread.sleep(sleep); } diff --git a/src/main/java/ee/buerokratt/ruuter/domain/steps/http/HttpStep.java b/src/main/java/ee/buerokratt/ruuter/domain/steps/http/HttpStep.java index b262ea00..4dbb62b1 100644 --- a/src/main/java/ee/buerokratt/ruuter/domain/steps/http/HttpStep.java +++ b/src/main/java/ee/buerokratt/ruuter/domain/steps/http/HttpStep.java @@ -67,7 +67,7 @@ protected void executeStepAction(DslInstance di) { setNextStepName(getOnErrorStep()); } else { - di.setErrorStatus(response.getStatusCode()); + di.setErrorStatus(HttpStatus.valueOf(response.getStatusCodeValue())); di.setErrorMessage("HTTP returned with non-OK"); throw new IllegalArgumentException(); } diff --git a/src/main/java/ee/buerokratt/ruuter/service/DslService.java b/src/main/java/ee/buerokratt/ruuter/service/DslService.java index 24f9f3bd..704e1600 100644 --- a/src/main/java/ee/buerokratt/ruuter/service/DslService.java +++ b/src/main/java/ee/buerokratt/ruuter/service/DslService.java @@ -11,7 +11,7 @@ import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.core.util.Yaml; import lombok.extern.slf4j.Slf4j; -import org.springframework.cloud.sleuth.Tracer; +import io.opentelemetry.api.trace.Tracer; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index bd77ab09..12a0d198 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -36,6 +36,15 @@ application: # index: ruuterlog httpResponseSizeLimit: 256 +management: + endpoints: + web: + exposure: + include=prometheus: + tracing: + enabled: + true + spring: application: name: ruuter