Skip to content

Commit 7ec8b2b

Browse files
committed
Configure OpenTelemetry agent and custom spans
1 parent c202851 commit 7ec8b2b

7 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎Dockerfile‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ FROM eclipse-temurin:26-alpine AS builder
22

33
WORKDIR /app
44

5+
# bump: OpenTelemetry /v([\d.]+)/ github:open-telemetry/opentelemetry-java-instrumentation|/^v[\d.]+$/|sort
6+
ADD https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v2.31.1/opentelemetry-javaagent.jar \
7+
/app/opentelemetry-agent.jar
8+
59
# bump: FFmpeg /static-ffmpeg:([\d.]+)/ docker:mwader/static-ffmpeg|/\d+\./|*
610
COPY --from=mwader/static-ffmpeg:9.0.2 /ff* /usr/bin/
711

@@ -11,7 +15,10 @@ RUN --mount=type=cache,target=/root/.gradle ./gradlew check installDist --no-dae
1115
FROM alpine:3.24.2
1216

1317
COPY --from=builder /usr/bin/ff* /usr/bin/
18+
COPY --from=builder /app/opentelemetry-agent.jar /app/opentelemetry-agent.jar
1419
COPY --from=builder /app/build/install/Stickerify/ .
1520

21+
ENV OTEL_SDK_DISABLED=true
22+
ENV JAVA_TOOL_OPTIONS=-javaagent:/app/opentelemetry-agent.jar
1623
ENV CONCURRENT_PROCESSES=5
1724
CMD ["./bin/Stickerify"]

‎Railway.dockerfile‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
ARG DOCKER_TAG=latest
2+
ARG OTEL_SDK_DISABLED=false
23
FROM rob93c/stickerify:$DOCKER_TAG
34
ARG STICKERIFY_TOKEN
45
ARG LOG_LEVEL
56
ARG CONCURRENT_PROCESSES
67
ENV STICKERIFY_TOKEN=$STICKERIFY_TOKEN \
78
LOG_LEVEL=$LOG_LEVEL \
8-
CONCURRENT_PROCESSES=$CONCURRENT_PROCESSES
9+
CONCURRENT_PROCESSES=$CONCURRENT_PROCESSES \
10+
OTEL_SDK_DISABLED=$OTEL_SDK_DISABLED

‎build.gradle.kts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies {
2020
implementation(libs.jspecify)
2121
implementation(libs.logback.classic)
2222
implementation(libs.logstash.logback.encoder)
23+
implementation(libs.opentelemetry.instrumentation.annotations)
2324
implementation(libs.telegram.bot.api)
2425
implementation(libs.tika)
2526

@@ -54,6 +55,7 @@ val jlink = tasks.register<JlinkTask>("jlink") {
5455
modules = listOf(
5556
"java.instrument", // for junit
5657
"java.naming", // for logback
58+
"java.management", // for OpenTelemetry
5759
"java.sql", // for tika
5860
"jdk.unsupported" // for gson
5961
)

‎gradle/libs.versions.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ junit-platform = { module = "org.junit.platform:junit-platform-launcher", versio
1111
logback-classic = "ch.qos.logback:logback-classic:1.6.3"
1212
logstash-logback-encoder = "net.logstash.logback:logstash-logback-encoder:9.0"
1313
mockwebserver = "com.squareup.okhttp3:mockwebserver3-junit5:5.5.0"
14+
opentelemetry-instrumentation-annotations = "io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations:2.31.1"
1415
telegram-bot-api = "com.github.pengrad:java-telegram-bot-api:10.1.0"
1516
tika = "org.apache.tika:tika-core:4.0.0"
1617

‎src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.pengrad.telegrambot.request.richmessages.SendRichMessage;
3535
import com.pengrad.telegrambot.request.richmessages.SendRichMessageDraft;
3636
import com.pengrad.telegrambot.response.BaseResponse;
37+
import io.opentelemetry.instrumentation.annotations.WithSpan;
3738
import org.jspecify.annotations.Nullable;
3839
import org.slf4j.event.Level;
3940

@@ -106,6 +107,7 @@ public void close() {
106107
bot.shutdown();
107108
}
108109

110+
@WithSpan("bot.answer")
109111
private void answer(TelegramRequest request) {
110112
LOGGER.at(Level.INFO).log("Received request");
111113

@@ -118,6 +120,7 @@ private void answer(TelegramRequest request) {
118120
}
119121
}
120122

123+
@WithSpan("bot.answer.file")
121124
private void answerFile(TelegramRequest request, TelegramFile file) {
122125
if (file == TelegramFile.NOT_SUPPORTED) {
123126
answerText(ERROR, request);
@@ -163,6 +166,7 @@ private void answerFile(TelegramRequest request, String fileId) {
163166
}
164167
}
165168

169+
@WithSpan("bot.download_file")
166170
private File retrieveFile(String fileId) throws TelegramApiException, FileOperationException {
167171
var file = execute(new GetFile(fileId)).file();
168172

@@ -206,6 +210,7 @@ private boolean processTelegramFailure(TelegramApiException e, boolean logUnmatc
206210
return replyToUser;
207211
}
208212

213+
@WithSpan("bot.answer.text")
209214
private void answerText(TelegramRequest request) {
210215
var message = request.message();
211216
if (message.text() == null) {

‎src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.gson.Gson;
2626
import com.google.gson.JsonSyntaxException;
2727
import com.google.gson.annotations.SerializedName;
28+
import io.opentelemetry.instrumentation.annotations.WithSpan;
2829
import org.apache.tika.Tika;
2930
import org.jspecify.annotations.Nullable;
3031
import org.slf4j.event.Level;
@@ -66,6 +67,7 @@ public final class MediaHelper {
6667
* @throws Exception either if the file is not supported, if the conversion failed,
6768
* or if the current thread is interrupted while converting a video file
6869
*/
70+
@WithSpan("media.convert")
6971
public static @Nullable File convert(File inputFile) throws Exception {
7072
var mimeType = detectMimeType(inputFile);
7173

@@ -384,6 +386,7 @@ && isSizeCompliant(imageInfo.width(), imageInfo.height())
384386
* @throws MediaException if an error occurred processing passed-in image
385387
* @throws InterruptedException if the current thread is interrupted while converting the file
386388
*/
389+
@WithSpan("media.convert.image")
387390
private static File convertToWebp(File file) throws MediaException, InterruptedException {
388391
var webpImage = createTempFile("webp");
389392
var command = List.of(
@@ -465,6 +468,7 @@ private static void deleteFile(File file) throws FileOperationException {
465468
* @throws MediaException if file conversion is not successful
466469
* @throws InterruptedException if the current thread is interrupted while converting the video file
467470
*/
471+
@WithSpan("media.convert.video")
468472
private static File convertToWebm(File file) throws MediaException, InterruptedException {
469473
var webmVideo = createTempFile("webm");
470474
var logPrefix = webmVideo.getAbsolutePath() + "-passlog";

‎src/main/java/com/github/stickerifier/stickerify/process/ProcessHelper.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.github.stickerifier.stickerify.exception.ProcessException;
66
import com.github.stickerifier.stickerify.logger.StructuredLogger;
7+
import io.opentelemetry.instrumentation.annotations.WithSpan;
78
import org.slf4j.event.Level;
89

910
import java.io.IOException;
@@ -34,6 +35,7 @@ public final class ProcessHelper {
3435
* </ul>
3536
* @throws InterruptedException if the current thread is interrupted while waiting for the command to finish
3637
*/
38+
@WithSpan("process.execute")
3739
public static String executeCommand(final List<String> command) throws ProcessException, InterruptedException {
3840
SEMAPHORE.acquire();
3941

0 commit comments

Comments
 (0)