|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.opentelemetry.javaagent.providers; |
| 17 | + |
| 18 | +import io.opentelemetry.exporter.internal.otlp.traces.TraceRequestMarshaler; |
| 19 | +import io.opentelemetry.sdk.common.CompletableResultCode; |
| 20 | +import io.opentelemetry.sdk.trace.data.SpanData; |
| 21 | +import io.opentelemetry.sdk.trace.export.SpanExporter; |
| 22 | +import java.io.ByteArrayOutputStream; |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.util.Base64; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 27 | +import java.util.logging.Level; |
| 28 | +import java.util.logging.Logger; |
| 29 | +import javax.annotation.concurrent.Immutable; |
| 30 | + |
| 31 | +/** |
| 32 | + * Exports spans via UDP, using OpenTelemetry's protobuf model. The protobuf modelled spans are |
| 33 | + * Base64 encoded and prefixed with AWS X-Ray specific information before being sent over to {@link |
| 34 | + * UdpSender}. |
| 35 | + * |
| 36 | + * <p>This exporter is NOT meant for generic use since the payload is prefixed with AWS X-Ray |
| 37 | + * specific information. |
| 38 | + */ |
| 39 | +@Immutable |
| 40 | +class OtlpUdpSpanExporter implements SpanExporter { |
| 41 | + |
| 42 | + private static final Logger logger = Logger.getLogger(OtlpUdpSpanExporter.class.getName()); |
| 43 | + |
| 44 | + private final AtomicBoolean isShutdown = new AtomicBoolean(); |
| 45 | + |
| 46 | + private final UdpSender sender; |
| 47 | + private final String payloadPrefix; |
| 48 | + |
| 49 | + OtlpUdpSpanExporter(UdpSender sender, String payloadPrefix) { |
| 50 | + this.sender = sender; |
| 51 | + this.payloadPrefix = payloadPrefix; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public CompletableResultCode export(Collection<SpanData> spans) { |
| 56 | + if (isShutdown.get()) { |
| 57 | + return CompletableResultCode.ofFailure(); |
| 58 | + } |
| 59 | + |
| 60 | + TraceRequestMarshaler exportRequest = TraceRequestMarshaler.create(spans); |
| 61 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 62 | + try { |
| 63 | + exportRequest.writeBinaryTo(baos); |
| 64 | + String payload = payloadPrefix + Base64.getEncoder().encodeToString(baos.toByteArray()); |
| 65 | + sender.send(payload.getBytes(StandardCharsets.UTF_8)); |
| 66 | + return CompletableResultCode.ofSuccess(); |
| 67 | + } catch (Exception e) { |
| 68 | + logger.log(Level.SEVERE, "Failed to export spans. Error: " + e.getMessage(), e); |
| 69 | + return CompletableResultCode.ofFailure(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public CompletableResultCode flush() { |
| 75 | + // TODO: implement |
| 76 | + return CompletableResultCode.ofSuccess(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public CompletableResultCode shutdown() { |
| 81 | + if (!isShutdown.compareAndSet(false, true)) { |
| 82 | + logger.log(Level.INFO, "Calling shutdown() multiple times."); |
| 83 | + return CompletableResultCode.ofSuccess(); |
| 84 | + } |
| 85 | + return sender.shutdown(); |
| 86 | + } |
| 87 | + |
| 88 | + // Visible for testing |
| 89 | + UdpSender getSender() { |
| 90 | + return sender; |
| 91 | + } |
| 92 | + |
| 93 | + // Visible for testing |
| 94 | + String getPayloadPrefix() { |
| 95 | + return payloadPrefix; |
| 96 | + } |
| 97 | +} |
0 commit comments