generated from amazon-archives/__template_Apache-2.0
    
        
        - 
                Notifications
    
You must be signed in to change notification settings  - Fork 66
 
SigV4 Authentication support for http exporter #1019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 11 commits
      Commits
    
    
            Show all changes
          
          
            13 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      779da66
              
                [OTLP] Add custom OTLP SigV4 OkHttp exporter
              
              
                majanjua-amzn 0f1ddd1
              
                added SigV4 support for HttpSpan Exporter
              
              
                liustve 10355ff
              
                Merge branch 'main' into sigv4
              
              
                liustve e5bf335
              
                formatting fix
              
              
                liustve 5c5f8e1
              
                Merge remote-tracking branch 'origin/sigv4' into sigv4
              
              
                liustve b5a3529
              
                checking instance
              
              
                liustve a460a84
              
                updated gradle and exporter
              
              
                liustve 8c35d35
              
                added unit tests
              
              
                liustve fc76008
              
                added documentation
              
              
                liustve 03d99f7
              
                added copier constructor
              
              
                liustve 51a4478
              
                Merge branch 'main' into sigv4
              
              
                liustve 4a7f845
              
                cleaning up code
              
              
                liustve 8bbbd7a
              
                Merge remote-tracking branch 'origin/sigv4' into sigv4
              
              
                liustve File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -41,9 +41,12 @@ dependencies { | |
| // Import AWS SDK v1 core for ARN parsing utilities | ||
| implementation("com.amazonaws:aws-java-sdk-core:1.12.773") | ||
| // Export configuration | ||
| compileOnly("io.opentelemetry:opentelemetry-exporter-otlp") | ||
| implementation("io.opentelemetry:opentelemetry-exporter-otlp") | ||
| // For Udp emitter | ||
| compileOnly("io.opentelemetry:opentelemetry-exporter-otlp-common") | ||
| // For HTTP SigV4 emitter | ||
| implementation("software.amazon.awssdk:auth:2.30.14") | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why use fixed versions of http auth. Will it cause conflicts when customers use a different version in their application?  | 
||
| implementation("software.amazon.awssdk:http-auth-aws:2.30.14") | ||
| 
     | 
||
| testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") | ||
| testImplementation("io.opentelemetry:opentelemetry-sdk-testing") | ||
| 
          
            
          
           | 
    ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
  
    
      
          
            158 changes: 158 additions & 0 deletions
          
          158 
        
  .../src/main/java/software/amazon/opentelemetry/javaagent/providers/OtlpAwsSpanExporter.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
| 
     | 
||
| package software.amazon.opentelemetry.javaagent.providers; | ||
| 
     | 
||
| import io.opentelemetry.exporter.internal.otlp.traces.TraceRequestMarshaler; | ||
| import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter; | ||
| import io.opentelemetry.sdk.common.CompletableResultCode; | ||
| import io.opentelemetry.sdk.trace.data.SpanData; | ||
| import io.opentelemetry.sdk.trace.export.SpanExporter; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.net.URI; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Supplier; | ||
| import javax.annotation.concurrent.Immutable; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import software.amazon.awssdk.auth.credentials.AwsCredentials; | ||
| import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; | ||
| import software.amazon.awssdk.http.SdkHttpFullRequest; | ||
| import software.amazon.awssdk.http.SdkHttpMethod; | ||
| import software.amazon.awssdk.http.SdkHttpRequest; | ||
| import software.amazon.awssdk.http.auth.aws.signer.AwsV4HttpSigner; | ||
| import software.amazon.awssdk.http.auth.spi.signer.SignedRequest; | ||
| 
     | 
||
| /** | ||
| * This exporter extends the functionality of the OtlpHttpSpanExporter to allow spans to be exported | ||
| * to the XRay OTLP endpoint https://xray.[AWSRegion].amazonaws.com/v1/traces. Utilizes the AWSSDK | ||
| * library to sign and directly inject SigV4 Authentication to the exported request's headers. <a | ||
| * href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-OTLPEndpoint.html">...</a> | ||
| */ | ||
| @Immutable | ||
| public class OtlpAwsSpanExporter implements SpanExporter { | ||
| private static final Logger logger = LoggerFactory.getLogger(OtlpAwsSpanExporter.class); | ||
| 
     | 
||
| private final OtlpHttpSpanExporter parentExporter; | ||
| private final String awsRegion; | ||
| private final String endpoint; | ||
| private Collection<SpanData> spanData; | ||
| 
     | 
||
| public OtlpAwsSpanExporter(String endpoint) { | ||
| this.parentExporter = | ||
| OtlpHttpSpanExporter.builder() | ||
| .setEndpoint(endpoint) | ||
| .setHeaders(new SigV4AuthHeaderSupplier()) | ||
| .build(); | ||
| 
     | 
||
| this.awsRegion = endpoint.split("\\.")[1]; | ||
                
      
                  majanjua-amzn marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| this.endpoint = endpoint; | ||
| this.spanData = new ArrayList<>(); | ||
| } | ||
| 
     | 
||
| public OtlpAwsSpanExporter(OtlpHttpSpanExporter parentExporter, String endpoint) { | ||
| this.parentExporter = | ||
| parentExporter.toBuilder() | ||
| .setEndpoint(endpoint) | ||
| .setHeaders(new SigV4AuthHeaderSupplier()) | ||
| .build(); | ||
| 
     | 
||
| this.awsRegion = endpoint.split("\\.")[1]; | ||
| this.endpoint = endpoint; | ||
| this.spanData = new ArrayList<>(); | ||
| } | ||
| 
     | 
||
| /** | ||
| * Overrides the upstream implementation of export. All behaviors are the same except if the | ||
| * endpoint is an XRay OTLP endpoint, we will sign the request with SigV4 in headers before | ||
| * sending it to the endpoint. Otherwise, we will skip signing. | ||
| */ | ||
| @Override | ||
| public CompletableResultCode export(Collection<SpanData> spans) { | ||
| this.spanData = spans; | ||
| return this.parentExporter.export(spans); | ||
| } | ||
| 
     | 
||
| @Override | ||
| public CompletableResultCode flush() { | ||
| return this.parentExporter.flush(); | ||
| } | ||
| 
     | 
||
| @Override | ||
| public CompletableResultCode shutdown() { | ||
| return this.parentExporter.shutdown(); | ||
| } | ||
| 
     | 
||
| @Override | ||
| public String toString() { | ||
| return this.parentExporter.toString(); | ||
| } | ||
| 
     | 
||
| private final class SigV4AuthHeaderSupplier implements Supplier<Map<String, String>> { | ||
| 
     | 
||
| @Override | ||
| public Map<String, String> get() { | ||
| try { | ||
| ByteArrayOutputStream encodedSpans = new ByteArrayOutputStream(); | ||
| TraceRequestMarshaler.create(OtlpAwsSpanExporter.this.spanData).writeBinaryTo(encodedSpans); | ||
| 
     | 
||
| SdkHttpRequest httpRequest = | ||
| SdkHttpFullRequest.builder() | ||
| .uri(URI.create(OtlpAwsSpanExporter.this.endpoint)) | ||
| .method(SdkHttpMethod.POST) | ||
| .putHeader("Content-Type", "application/x-protobuf") | ||
| .contentStreamProvider(() -> new ByteArrayInputStream(encodedSpans.toByteArray())) | ||
| .build(); | ||
| 
     | 
||
| AwsCredentials credentials = DefaultCredentialsProvider.create().resolveCredentials(); | ||
| 
     | 
||
| SignedRequest signedRequest = | ||
| AwsV4HttpSigner.create() | ||
| .sign( | ||
| b -> | ||
| b.identity(credentials) | ||
| .request(httpRequest) | ||
| .putProperty(AwsV4HttpSigner.SERVICE_SIGNING_NAME, "xray") | ||
                
      
                  liustve marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| .putProperty( | ||
| AwsV4HttpSigner.REGION_NAME, OtlpAwsSpanExporter.this.awsRegion) | ||
| .payload(() -> new ByteArrayInputStream(encodedSpans.toByteArray()))); | ||
| 
     | 
||
| Map<String, String> result = new HashMap<>(); | ||
| 
     | 
||
| Map<String, List<String>> headers = signedRequest.request().headers(); | ||
| headers.forEach( | ||
| (key, values) -> { | ||
| if (!values.isEmpty()) { | ||
| result.put(key, values.get(0)); | ||
| } | ||
| }); | ||
| 
     | 
||
| return result; | ||
| 
     | 
||
| } catch (Exception e) { | ||
| logger.error( | ||
| "Failed to sign/authenticate the given exported Span request to OTLP CloudWatch endpoint with error: {}", | ||
| e.getMessage()); | ||
| 
     | 
||
| return new HashMap<>(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we change it to
implementation?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it from compileOnly to implementation is because the http exporter is now a required dependency when I began executing the auto instrumentation at runtime. Otherwise I believe I was getting a ClassNotFoundException everytime I ran the exporter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We changed it to
compileOnlydue to a regression it introduced, see #651. You need to figure out a way to avoid the same issue before submit the change.