From f9cc7b5d2e63906e7e0595fdcf36fc6d0d05340b Mon Sep 17 00:00:00 2001 From: Matt Richardson Date: Wed, 20 Nov 2024 16:46:45 +1100 Subject: [PATCH] Add env var TEAMCITY_OTEL_PLUGIN_TRACE_ID to builds --- .../server/BuildParameterPreProcessor.java | 36 +++++++++++++++++++ ...-plugin-Octopus.TeamCity.Opentelemetry.xml | 1 + 2 files changed, 37 insertions(+) create mode 100644 server/src/main/java/com/octopus/teamcity/opentelemetry/server/BuildParameterPreProcessor.java diff --git a/server/src/main/java/com/octopus/teamcity/opentelemetry/server/BuildParameterPreProcessor.java b/server/src/main/java/com/octopus/teamcity/opentelemetry/server/BuildParameterPreProcessor.java new file mode 100644 index 0000000..db894b5 --- /dev/null +++ b/server/src/main/java/com/octopus/teamcity/opentelemetry/server/BuildParameterPreProcessor.java @@ -0,0 +1,36 @@ +package com.octopus.teamcity.opentelemetry.server; + +import jetbrains.buildServer.serverSide.ParametersPreprocessor; +import jetbrains.buildServer.serverSide.SRunningBuild; +import org.apache.log4j.Logger; +import org.apache.logging.log4j.CloseableThreadContext; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public class BuildParameterPreProcessor implements ParametersPreprocessor { + private static final Logger LOG = Logger.getLogger(BuildParameterPreProcessor.class.getName()); + + @NotNull + private final BuildStorageManager buildStorageManager; + + BuildParameterPreProcessor(@NotNull BuildStorageManager buildStorageManager) { + this.buildStorageManager = buildStorageManager; + } + + @Override + public void fixRunBuildParameters(@NotNull SRunningBuild build, @NotNull Map runParameters, @NotNull Map buildParams) { + try (var ignored = CloseableThreadContext.put("teamcity.build.id", String.valueOf(build.getBuildId()))) { + LOG.debug(String.format("Looking up launch uuid for for build id %d, to add to the build parameters", build.getBuildId())); + var traceId = buildStorageManager.getTraceId(build); + + if (traceId == null) { + LOG.warn(String.format("Unable to get launch uuid for build id %d; we cant set the build parameters", build.getBuildId())); + return; + } + LOG.debug(String.format("Adding trace id '%s' to build parameters for build %d", traceId, build.getBuildId())); + + buildParams.put("env.TEAMCITY_OTEL_PLUGIN_TRACE_ID", traceId); + } + } +} diff --git a/server/src/main/resources/META-INF/build-server-plugin-Octopus.TeamCity.Opentelemetry.xml b/server/src/main/resources/META-INF/build-server-plugin-Octopus.TeamCity.Opentelemetry.xml index 9236211..24c2d69 100644 --- a/server/src/main/resources/META-INF/build-server-plugin-Octopus.TeamCity.Opentelemetry.xml +++ b/server/src/main/resources/META-INF/build-server-plugin-Octopus.TeamCity.Opentelemetry.xml @@ -9,6 +9,7 @@ +