Skip to content

Commit

Permalink
Log build id with metrics (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-richardson authored Dec 1, 2024
1 parent 673c501 commit 107e6f4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.octopus.teamcity.opentelemetry.server.SetProjectConfigurationSettingsRequest;
import io.opentelemetry.sdk.trace.SpanProcessor;
import jetbrains.buildServer.serverSide.BuildPromotion;
import jetbrains.buildServer.serverSide.SBuild;
import org.springframework.web.servlet.ModelAndView;

Expand All @@ -11,7 +12,7 @@
public interface IOTELEndpointHandler {
ModelAndView getBuildOverviewModelAndView(SBuild build, Map<String, String> params, String traceId);

SpanProcessor buildSpanProcessor(String endpoint, Map<String, String> params);
SpanProcessor buildSpanProcessor(BuildPromotion buildPromotion, String endpoint, Map<String, String> params);

SetProjectConfigurationSettingsRequest getSetProjectConfigurationSettingsRequest(HttpServletRequest request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import jetbrains.buildServer.serverSide.BuildPromotion;
import jetbrains.buildServer.serverSide.SBuild;
import jetbrains.buildServer.serverSide.crypt.EncryptUtil;
import jetbrains.buildServer.web.openapi.PluginDescriptor;
Expand Down Expand Up @@ -34,7 +35,7 @@ public ModelAndView getBuildOverviewModelAndView(SBuild build, Map<String, Strin
}

@Override
public SpanProcessor buildSpanProcessor(String endpoint, Map<String, String> params) {
public SpanProcessor buildSpanProcessor(BuildPromotion buildPromotion, String endpoint, Map<String, String> params) {
Map<String, String> headers = new HashMap<>();
params.forEach((k, v) -> {
if (k.startsWith(PROPERTY_KEY_HEADERS)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.semconv.ServiceAttributes;
import jetbrains.buildServer.serverSide.BuildPromotion;
import jetbrains.buildServer.serverSide.SBuild;
import jetbrains.buildServer.serverSide.TeamCityNodes;
import jetbrains.buildServer.serverSide.crypt.EncryptUtil;
Expand All @@ -24,7 +25,6 @@

import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -63,15 +63,15 @@ public ModelAndView getBuildOverviewModelAndView(SBuild build, Map<String, Strin
}

@Override
public SpanProcessor buildSpanProcessor(String endpoint, Map<String, String> params) {
public SpanProcessor buildSpanProcessor(BuildPromotion buildPromotion, String endpoint, Map<String, String> params) {
Map<String, String> headers = new HashMap<>();
//todo: add a setting to say "use classic" or "use environments"
headers.put("x-honeycomb-dataset", params.get(PROPERTY_KEY_HONEYCOMB_DATASET));
headers.put("x-honeycomb-team", EncryptUtil.unscramble(params.get(PROPERTY_KEY_HONEYCOMB_APIKEY)));

var metricsExporter = buildMetricsExporter(endpoint, params);

return buildGrpcSpanProcessor(headers, endpoint, metricsExporter);
return buildGrpcSpanProcessor(buildPromotion, headers, endpoint, metricsExporter);
}

@Nullable
Expand All @@ -86,12 +86,17 @@ private MetricExporter buildMetricsExporter(String endpoint, Map<String, String>
return null;
}

private SpanProcessor buildGrpcSpanProcessor(Map<String, String> headers, String exporterEndpoint, @Nullable MetricExporter metricsExporter) {
private SpanProcessor buildGrpcSpanProcessor(
BuildPromotion buildPromotion,
Map<String, String> headers,
String exporterEndpoint,
@Nullable MetricExporter metricsExporter) {

//todo: centralise the definition of this
var serviceNameResource = Resource
.create(Attributes.of(
ServiceAttributes.SERVICE_NAME, PluginConstants.SERVICE_NAME,
AttributeKey.stringKey("teamcity.build_promotion.id"), Long.toString(buildPromotion.getId()),
AttributeKey.stringKey("teamcity.node.id"), nodesService.getCurrentNode().getId()
));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import jetbrains.buildServer.serverSide.BuildPromotion;
import jetbrains.buildServer.serverSide.SBuild;
import jetbrains.buildServer.web.openapi.PluginDescriptor;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -34,7 +35,7 @@ public ModelAndView getBuildOverviewModelAndView(SBuild build, Map<String, Strin
}

@Override
public SpanProcessor buildSpanProcessor(String endpoint, Map<String, String> params) {
public SpanProcessor buildSpanProcessor(BuildPromotion buildPromotion, String endpoint, Map<String, String> params) {
return buildZipkinSpanProcessor(endpoint);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public HelperPerBuildOTELHelperFactory(
this.otelHelpers = new ConcurrentHashMap<>();
}

public OTELHelper getOTELHelper(BuildPromotion build) {
var buildId = build.getId();
public OTELHelper getOTELHelper(BuildPromotion buildPromotion) {
var buildId = buildPromotion.getId();

return otelHelpers.computeIfAbsent(buildId, key -> {
LOG.debug(String.format("Creating OTELHelper for build %d.", buildId));
var projectId = build.getProjectExternalId();
var projectId = buildPromotion.getProjectExternalId();
var project = projectManager.findProjectByExternalId(projectId);

var features = project.getAvailableFeaturesOfType(PLUGIN_NAME);
Expand All @@ -46,7 +46,7 @@ public OTELHelper getOTELHelper(BuildPromotion build) {
SpanProcessor spanProcessor;

var otelHandler = otelEndpointFactory.getOTELEndpointHandler(params.get(PROPERTY_KEY_SERVICE));
spanProcessor = otelHandler.buildSpanProcessor(endpoint, params);
spanProcessor = otelHandler.buildSpanProcessor(buildPromotion, endpoint, params);

long startTime = System.nanoTime();
var otelHelper = new OTELHelperImpl(spanProcessor, String.valueOf(buildId));
Expand Down

0 comments on commit 107e6f4

Please sign in to comment.