|
| 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.exporter.otlp.http.trace.OtlpHttpSpanExporter; |
| 20 | +import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder; |
| 21 | +import io.opentelemetry.sdk.common.CompletableResultCode; |
| 22 | +import io.opentelemetry.sdk.common.export.MemoryMode; |
| 23 | +import io.opentelemetry.sdk.trace.data.SpanData; |
| 24 | +import io.opentelemetry.sdk.trace.export.SpanExporter; |
| 25 | +import java.io.ByteArrayInputStream; |
| 26 | +import java.io.ByteArrayOutputStream; |
| 27 | +import java.net.URI; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.HashMap; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.StringJoiner; |
| 34 | +import java.util.concurrent.atomic.AtomicReference; |
| 35 | +import java.util.function.Supplier; |
| 36 | +import java.util.logging.Level; |
| 37 | +import java.util.logging.Logger; |
| 38 | +import javax.annotation.Nonnull; |
| 39 | +import software.amazon.awssdk.auth.credentials.AwsCredentials; |
| 40 | +import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; |
| 41 | +import software.amazon.awssdk.http.SdkHttpFullRequest; |
| 42 | +import software.amazon.awssdk.http.SdkHttpMethod; |
| 43 | +import software.amazon.awssdk.http.SdkHttpRequest; |
| 44 | +import software.amazon.awssdk.http.auth.aws.signer.AwsV4HttpSigner; |
| 45 | +import software.amazon.awssdk.http.auth.spi.signer.SignedRequest; |
| 46 | + |
| 47 | +/** |
| 48 | + * This exporter extends the functionality of the OtlpHttpSpanExporter to allow spans to be exported |
| 49 | + * to the XRay OTLP endpoint https://xray.[AWSRegion].amazonaws.com/v1/traces. Utilizes the AWSSDK |
| 50 | + * library to sign and directly inject SigV4 Authentication to the exported request's headers. <a |
| 51 | + * href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-OTLPEndpoint.html">...</a> |
| 52 | + */ |
| 53 | +public class OtlpAwsSpanExporter implements SpanExporter { |
| 54 | + private static final String SERVICE_NAME = "xray"; |
| 55 | + private static final Logger logger = Logger.getLogger(OtlpAwsSpanExporter.class.getName()); |
| 56 | + |
| 57 | + private final OtlpHttpSpanExporterBuilder parentExporterBuilder; |
| 58 | + private final OtlpHttpSpanExporter parentExporter; |
| 59 | + private final AtomicReference<Collection<SpanData>> spanData; |
| 60 | + private final String awsRegion; |
| 61 | + private final String endpoint; |
| 62 | + |
| 63 | + static OtlpAwsSpanExporter getDefault(String endpoint) { |
| 64 | + return new OtlpAwsSpanExporter(endpoint); |
| 65 | + } |
| 66 | + |
| 67 | + static OtlpAwsSpanExporter create(OtlpHttpSpanExporter parent, String endpoint) { |
| 68 | + return new OtlpAwsSpanExporter(parent, endpoint); |
| 69 | + } |
| 70 | + |
| 71 | + private OtlpAwsSpanExporter(String endpoint) { |
| 72 | + this(null, endpoint); |
| 73 | + } |
| 74 | + |
| 75 | + private OtlpAwsSpanExporter(OtlpHttpSpanExporter parentExporter, String endpoint) { |
| 76 | + this.awsRegion = endpoint.split("\\.")[1]; |
| 77 | + this.endpoint = endpoint; |
| 78 | + this.spanData = new AtomicReference<>(Collections.emptyList()); |
| 79 | + |
| 80 | + if (parentExporter == null) { |
| 81 | + this.parentExporterBuilder = |
| 82 | + OtlpHttpSpanExporter.builder() |
| 83 | + .setMemoryMode(MemoryMode.IMMUTABLE_DATA) |
| 84 | + .setEndpoint(endpoint) |
| 85 | + .setHeaders(new SigV4AuthHeaderSupplier()); |
| 86 | + this.parentExporter = this.parentExporterBuilder.build(); |
| 87 | + return; |
| 88 | + } |
| 89 | + this.parentExporterBuilder = |
| 90 | + parentExporter.toBuilder() |
| 91 | + .setMemoryMode(MemoryMode.IMMUTABLE_DATA) |
| 92 | + .setEndpoint(endpoint) |
| 93 | + .setHeaders(new SigV4AuthHeaderSupplier()); |
| 94 | + this.parentExporter = this.parentExporterBuilder.build(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Overrides the upstream implementation of export. All behaviors are the same except if the |
| 99 | + * endpoint is an XRay OTLP endpoint, we will sign the request with SigV4 in headers before |
| 100 | + * sending it to the endpoint. Otherwise, we will skip signing. |
| 101 | + */ |
| 102 | + @Override |
| 103 | + public CompletableResultCode export(@Nonnull Collection<SpanData> spans) { |
| 104 | + this.spanData.set(spans); |
| 105 | + return this.parentExporter.export(spans); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public CompletableResultCode flush() { |
| 110 | + return this.parentExporter.flush(); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public CompletableResultCode shutdown() { |
| 115 | + return this.parentExporter.shutdown(); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public String toString() { |
| 120 | + StringJoiner joiner = new StringJoiner(", ", "OtlpAwsSpanExporter{", "}"); |
| 121 | + joiner.add(this.parentExporterBuilder.toString()); |
| 122 | + joiner.add("memoryMode=" + MemoryMode.IMMUTABLE_DATA); |
| 123 | + return joiner.toString(); |
| 124 | + } |
| 125 | + |
| 126 | + private final class SigV4AuthHeaderSupplier implements Supplier<Map<String, String>> { |
| 127 | + |
| 128 | + @Override |
| 129 | + public Map<String, String> get() { |
| 130 | + try { |
| 131 | + Collection<SpanData> spans = OtlpAwsSpanExporter.this.spanData.get(); |
| 132 | + ByteArrayOutputStream encodedSpans = new ByteArrayOutputStream(); |
| 133 | + TraceRequestMarshaler.create(spans).writeBinaryTo(encodedSpans); |
| 134 | + |
| 135 | + SdkHttpRequest httpRequest = |
| 136 | + SdkHttpFullRequest.builder() |
| 137 | + .uri(URI.create(OtlpAwsSpanExporter.this.endpoint)) |
| 138 | + .method(SdkHttpMethod.POST) |
| 139 | + .putHeader("Content-Type", "application/x-protobuf") |
| 140 | + .contentStreamProvider(() -> new ByteArrayInputStream(encodedSpans.toByteArray())) |
| 141 | + .build(); |
| 142 | + |
| 143 | + AwsCredentials credentials = DefaultCredentialsProvider.create().resolveCredentials(); |
| 144 | + |
| 145 | + SignedRequest signedRequest = |
| 146 | + AwsV4HttpSigner.create() |
| 147 | + .sign( |
| 148 | + b -> |
| 149 | + b.identity(credentials) |
| 150 | + .request(httpRequest) |
| 151 | + .putProperty(AwsV4HttpSigner.SERVICE_SIGNING_NAME, SERVICE_NAME) |
| 152 | + .putProperty( |
| 153 | + AwsV4HttpSigner.REGION_NAME, OtlpAwsSpanExporter.this.awsRegion) |
| 154 | + .payload(() -> new ByteArrayInputStream(encodedSpans.toByteArray()))); |
| 155 | + |
| 156 | + Map<String, String> result = new HashMap<>(); |
| 157 | + |
| 158 | + Map<String, List<String>> headers = signedRequest.request().headers(); |
| 159 | + headers.forEach( |
| 160 | + (key, values) -> { |
| 161 | + if (!values.isEmpty()) { |
| 162 | + result.put(key, values.get(0)); |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + return result; |
| 167 | + |
| 168 | + } catch (Exception e) { |
| 169 | + logger.log( |
| 170 | + Level.WARNING, |
| 171 | + String.format( |
| 172 | + "Failed to sign/authenticate the given exported Span request to OTLP CloudWatch endpoint with error: %s", |
| 173 | + e.getMessage())); |
| 174 | + |
| 175 | + return Collections.emptyMap(); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments