diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/clients/DatadogAgentClient.java b/src/main/java/org/datadog/jenkins/plugins/datadog/clients/DatadogAgentClient.java index 8507db19..9fab0011 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/clients/DatadogAgentClient.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/clients/DatadogAgentClient.java @@ -51,7 +51,7 @@ of this software and associated documentation files (the "Software"), to deal import org.datadog.jenkins.plugins.datadog.DatadogUtilities; import org.datadog.jenkins.plugins.datadog.traces.mapper.JsonTraceSpanMapper; import org.datadog.jenkins.plugins.datadog.traces.write.AgentTraceWriteStrategy; -import org.datadog.jenkins.plugins.datadog.traces.write.Span; +import org.datadog.jenkins.plugins.datadog.traces.write.Payload; import org.datadog.jenkins.plugins.datadog.traces.write.TraceWriteStrategy; import org.datadog.jenkins.plugins.datadog.traces.write.TraceWriteStrategyImpl; import org.datadog.jenkins.plugins.datadog.traces.write.Track; @@ -536,7 +536,7 @@ boolean isEvpProxySupported() { /** * Posts a given payload to the Agent EVP Proxy, so it is forwarded to the Webhook Intake. */ - private void sendSpansToWebhook(Collection spans) { + private void sendSpansToWebhook(Collection spans) { DatadogGlobalConfiguration datadogGlobalDescriptor = DatadogUtilities.getDatadogGlobalDescriptor(); String urlParameters = datadogGlobalDescriptor != null ? "?service=" + datadogGlobalDescriptor.getCiInstanceName() : ""; String url = String.format("http://%s:%d/evp_proxy/v1/api/v2/webhook/%s", hostname, traceCollectionPort, urlParameters); @@ -545,13 +545,13 @@ private void sendSpansToWebhook(Collection spans) { headers.put("X-Datadog-EVP-Subdomain", "webhook-intake"); headers.put("DD-CI-PROVIDER-NAME", "jenkins"); - for (Span span : spans) { + for (Payload span : spans) { if (span.getTrack() != Track.WEBHOOK) { logger.severe("Expected webhook track, got " + span.getTrack() + ", dropping span"); continue; } - byte[] body = span.getPayload().toString().getBytes(StandardCharsets.UTF_8); + byte[] body = span.getJson().toString().getBytes(StandardCharsets.UTF_8); // webhook intake does not support batch requests logger.fine("Sending webhook"); @@ -559,15 +559,15 @@ private void sendSpansToWebhook(Collection spans) { } } - private void sendSpansToApm(Collection spans) { + private void sendSpansToApm(Collection spans) { try { Map tracesById = new HashMap<>(); - for (Span span : spans) { + for (Payload span : spans) { if (span.getTrack() != Track.APM) { logger.severe("Expected APM track, got " + span.getTrack() + ", dropping span"); continue; } - tracesById.computeIfAbsent(span.getPayload().getString(JsonTraceSpanMapper.TRACE_ID), k -> new net.sf.json.JSONArray()).add(span.getPayload()); + tracesById.computeIfAbsent(span.getJson().getString(JsonTraceSpanMapper.TRACE_ID), k -> new net.sf.json.JSONArray()).add(span.getJson()); } final JSONArray jsonTraces = new JSONArray(); diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/clients/DatadogApiClient.java b/src/main/java/org/datadog/jenkins/plugins/datadog/clients/DatadogApiClient.java index 0fba0167..2573dbe1 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/clients/DatadogApiClient.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/clients/DatadogApiClient.java @@ -46,7 +46,7 @@ of this software and associated documentation files (the "Software"), to deal import org.datadog.jenkins.plugins.datadog.DatadogEvent; import org.datadog.jenkins.plugins.datadog.DatadogGlobalConfiguration; import org.datadog.jenkins.plugins.datadog.DatadogUtilities; -import org.datadog.jenkins.plugins.datadog.traces.write.Span; +import org.datadog.jenkins.plugins.datadog.traces.write.Payload; import org.datadog.jenkins.plugins.datadog.traces.write.TraceWriteStrategy; import org.datadog.jenkins.plugins.datadog.traces.write.TraceWriteStrategyImpl; import org.datadog.jenkins.plugins.datadog.traces.write.Track; @@ -495,7 +495,7 @@ public TraceWriteStrategy createTraceWriteStrategy() { return new TraceWriteStrategyImpl(Track.WEBHOOK, this::sendSpans); } - private void sendSpans(Collection spans) { + private void sendSpans(Collection spans) { if (this.webhookIntakeConnectionBroken) { throw new RuntimeException("Your client is not initialized properly; webhook intake connection is broken."); } @@ -508,13 +508,13 @@ private void sendSpans(Collection spans) { headers.put("DD-API-KEY", Secret.toString(apiKey)); headers.put("DD-CI-PROVIDER-NAME", "jenkins"); - for (Span span : spans) { + for (Payload span : spans) { if (span.getTrack() != Track.WEBHOOK) { logger.severe("Expected webhook track, got " + span.getTrack() + ", dropping span"); continue; } - byte[] body = span.getPayload().toString().getBytes(StandardCharsets.UTF_8); + byte[] body = span.getJson().toString().getBytes(StandardCharsets.UTF_8); // webhook intake does not support batch requests logger.fine("Sending webhook"); diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/AgentTraceWriteStrategy.java b/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/AgentTraceWriteStrategy.java index e3c20628..04d875f5 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/AgentTraceWriteStrategy.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/AgentTraceWriteStrategy.java @@ -47,24 +47,24 @@ public AgentTraceWriteStrategy(TraceWriteStrategy evpProxyStrategy, TraceWriteSt } @Override - public Span createSpan(BuildData buildData, Run run) { - return getCurrentStrategy().createSpan(buildData, run); + public Payload serialize(BuildData buildData, Run run) { + return getCurrentStrategy().serialize(buildData, run); } @Nonnull @Override - public Collection createSpan(FlowNode flowNode, Run run) { - return getCurrentStrategy().createSpan(flowNode, run); + public Collection serialize(FlowNode flowNode, Run run) { + return getCurrentStrategy().serialize(flowNode, run); } @Override - public void send(Collection spans) { + public void send(Collection spans) { // we have to check the track for every span, // because the serialization strategy might've changed in between serialize() and send() - Map> spansByTrack = spans.stream().collect(Collectors.groupingBy(Span::getTrack)); - for (Map.Entry> e : spansByTrack.entrySet()) { + Map> spansByTrack = spans.stream().collect(Collectors.groupingBy(Payload::getTrack)); + for (Map.Entry> e : spansByTrack.entrySet()) { Track track = e.getKey(); - List trackSpans = e.getValue(); + List trackSpans = e.getValue(); if (track == Track.WEBHOOK) { evpProxyStrategy.send(trackSpans); diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/Span.java b/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/Payload.java similarity index 57% rename from src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/Span.java rename to src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/Payload.java index bbb7a98f..d9c05fd1 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/Span.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/Payload.java @@ -3,19 +3,19 @@ import javax.annotation.Nonnull; import net.sf.json.JSONObject; -public class Span { +public class Payload { - private final JSONObject payload; + private final JSONObject json; private final Track track; - public Span(@Nonnull JSONObject payload, @Nonnull Track track) { - this.payload = payload; + public Payload(@Nonnull JSONObject json, @Nonnull Track track) { + this.json = json; this.track = track; } @Nonnull - public JSONObject getPayload() { - return payload; + public JSONObject getJson() { + return json; } @Nonnull diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/TraceWriteStrategy.java b/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/TraceWriteStrategy.java index 504cea8d..ce35751f 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/TraceWriteStrategy.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/TraceWriteStrategy.java @@ -9,10 +9,10 @@ public interface TraceWriteStrategy { @Nullable - Span createSpan(BuildData buildData, Run run); + Payload serialize(BuildData buildData, Run run); @Nonnull - Collection createSpan(FlowNode flowNode, Run run); + Collection serialize(FlowNode flowNode, Run run); - void send(Collection spans); + void send(Collection spans); } diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/TraceWriteStrategyImpl.java b/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/TraceWriteStrategyImpl.java index a74ad61d..39cbd4a9 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/TraceWriteStrategyImpl.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/TraceWriteStrategyImpl.java @@ -2,7 +2,6 @@ import hudson.model.Run; import java.util.Collection; -import java.util.Collections; import java.util.function.Consumer; import java.util.logging.Logger; import java.util.stream.Collectors; @@ -26,9 +25,9 @@ public class TraceWriteStrategyImpl implements TraceWriteStrategy { private final Track track; private final DatadogBaseBuildLogic buildLogic; private final DatadogBasePipelineLogic pipelineLogic; - private final CircuitBreaker> sendSpansCircuitBreaker; + private final CircuitBreaker> sendSpansCircuitBreaker; - public TraceWriteStrategyImpl(Track track, Consumer> spansSender) { + public TraceWriteStrategyImpl(Track track, Consumer> spansSender) { if (track == Track.APM) { this.buildLogic = new DatadogTraceBuildLogic(); this.pipelineLogic = new DatadogTracePipelineLogic(); @@ -47,24 +46,24 @@ public TraceWriteStrategyImpl(Track track, Consumer> spansSende } @Override - public Span createSpan(final BuildData buildData, final Run run) { + public Payload serialize(final BuildData buildData, final Run run) { JSONObject buildSpan = buildLogic.finishBuildTrace(buildData, run); - return buildSpan != null ? new Span(buildSpan, track) : null; + return buildSpan != null ? new Payload(buildSpan, track) : null; } @Nonnull @Override - public Collection createSpan(FlowNode flowNode, Run run) { + public Collection serialize(FlowNode flowNode, Run run) { Collection stepSpans = pipelineLogic.execute(flowNode, run); - return stepSpans.stream().map(payload -> new Span(payload, track)).collect(Collectors.toList()); + return stepSpans.stream().map(payload -> new Payload(payload, track)).collect(Collectors.toList()); } @Override - public void send(Collection serializationResult) { + public void send(Collection serializationResult) { sendSpansCircuitBreaker.accept(serializationResult); } - private void logTransportBroken(Collection spans) { + private void logTransportBroken(Collection spans) { logger.fine("Ignoring " + spans.size() + " because transport is broken"); } diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/TraceWriter.java b/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/TraceWriter.java index c536ace2..7d911a63 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/TraceWriter.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/TraceWriter.java @@ -31,7 +31,7 @@ public final class TraceWriter { private static final int DEFAULT_BATCH_SIZE_LIMIT = 100; private final TraceWriteStrategy traceWriteStrategy; - private final BlockingQueue queue; + private final BlockingQueue queue; private final Thread poller; public TraceWriter(DatadogClient datadogClient) { @@ -54,18 +54,18 @@ public void stopSynchronously() throws InterruptedException { } public void submitBuild(final BuildData buildData, final Run run) throws InterruptedException, TimeoutException { - Span span = traceWriteStrategy.createSpan(buildData, run); + Payload span = traceWriteStrategy.serialize(buildData, run); submit(span); } public void submitPipelineStep(FlowNode flowNode, Run run) throws InterruptedException, TimeoutException { - Collection spans = traceWriteStrategy.createSpan(flowNode, run); - for (Span span : spans) { + Collection spans = traceWriteStrategy.serialize(flowNode, run); + for (Payload span : spans) { submit(span); } } - private void submit(@Nullable Span span) throws InterruptedException, TimeoutException { + private void submit(@Nullable Payload span) throws InterruptedException, TimeoutException { if (span != null && !queue.offer(span, getEnv(SUBMIT_TIMEOUT_ENV_VAR, DEFAULT_SUBMIT_TIMEOUT_SECONDS), TimeUnit.SECONDS)) { throw new TimeoutException("Timed out while submitting span"); } @@ -75,14 +75,14 @@ private void runPollingLoop() { long stopPollingAt = Long.MAX_VALUE; while (System.currentTimeMillis() < stopPollingAt) { try { - Span span = queue.poll(getEnv(POLLING_TIMEOUT_ENV_VAR, DEFAULT_POLLING_TIMEOUT_SECONDS), TimeUnit.SECONDS); + Payload span = queue.poll(getEnv(POLLING_TIMEOUT_ENV_VAR, DEFAULT_POLLING_TIMEOUT_SECONDS), TimeUnit.SECONDS); if (span == null) { // nothing to send continue; } int batchSize = getEnv(BATCH_SIZE_LIMIT_ENV_VAR, DEFAULT_BATCH_SIZE_LIMIT); - List spans = new ArrayList<>(batchSize); + List spans = new ArrayList<>(batchSize); spans.add(span); queue.drainTo(spans, batchSize - 1); diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/Track.java b/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/Track.java index 5114713b..e3b75653 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/Track.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/traces/write/Track.java @@ -1,3 +1,5 @@ package org.datadog.jenkins.plugins.datadog.traces.write; -public enum Track {APM, WEBHOOK} +public enum Track { + APM, WEBHOOK +} diff --git a/src/test/java/org/datadog/jenkins/plugins/datadog/clients/DatadogClientStub.java b/src/test/java/org/datadog/jenkins/plugins/datadog/clients/DatadogClientStub.java index 36c81009..0fd5e589 100644 --- a/src/test/java/org/datadog/jenkins/plugins/datadog/clients/DatadogClientStub.java +++ b/src/test/java/org/datadog/jenkins/plugins/datadog/clients/DatadogClientStub.java @@ -49,7 +49,7 @@ of this software and associated documentation files (the "Software"), to deal import org.datadog.jenkins.plugins.datadog.traces.DatadogWebhookPipelineLogic; import org.datadog.jenkins.plugins.datadog.traces.mapper.JsonTraceSpanMapper; import org.datadog.jenkins.plugins.datadog.traces.message.TraceSpan; -import org.datadog.jenkins.plugins.datadog.traces.write.Span; +import org.datadog.jenkins.plugins.datadog.traces.write.Payload; import org.datadog.jenkins.plugins.datadog.traces.write.TraceWriteStrategy; import org.datadog.jenkins.plugins.datadog.traces.write.Track; import org.jenkinsci.plugins.workflow.graph.FlowNode; @@ -268,14 +268,14 @@ private static final class StubTraceWriteStrategy implements TraceWriteStrategy private final Collection webhooks = new LinkedBlockingQueue<>(); @Override - public Span createSpan(BuildData buildData, Run run) { + public Payload serialize(BuildData buildData, Run run) { if (isWebhook) { JSONObject json = new DatadogWebhookBuildLogic().finishBuildTrace(buildData, run); if (json == null) { return null; } webhooks.add(json); - return new Span(json, Track.WEBHOOK); + return new Payload(json, Track.WEBHOOK); } else { TraceSpan span = new DatadogTraceBuildLogic().createSpan(buildData, run); if (span == null) { @@ -283,28 +283,28 @@ public Span createSpan(BuildData buildData, Run run) { } traces.add(span); JSONObject json = new JsonTraceSpanMapper().map(span); - return new Span(json, Track.APM); + return new Payload(json, Track.APM); } } @Nonnull @Override - public Collection createSpan(FlowNode flowNode, Run run) { + public Collection serialize(FlowNode flowNode, Run run) { if (isWebhook) { Collection jsons = new DatadogWebhookPipelineLogic().execute(flowNode, run); webhooks.addAll(jsons); - return jsons.stream().map(payload -> new Span(payload, Track.WEBHOOK)).collect(Collectors.toList()); + return jsons.stream().map(payload -> new Payload(payload, Track.WEBHOOK)).collect(Collectors.toList()); } else { Collection traceSpans = new DatadogTracePipelineLogic().collectTraces(flowNode, run); traces.addAll(traceSpans); JsonTraceSpanMapper mapper = new JsonTraceSpanMapper(); List jsons = traceSpans.stream().map(mapper::map).collect(Collectors.toList()); - return jsons.stream().map(payload -> new Span(payload, Track.APM)).collect(Collectors.toList()); + return jsons.stream().map(payload -> new Payload(payload, Track.APM)).collect(Collectors.toList()); } } @Override - public void send(Collection spans) { + public void send(Collection spans) { // no op }