- 
                Notifications
    You must be signed in to change notification settings 
- Fork 168
Add messaging wrappers to support lightweight manual instrumentation #1803
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
          
     Closed
      
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            11 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      8a6dc8c
              
                add messaging wrappers to support lightweight manual instrumentation
              
              
                Cirilla-zmh 8ef16f8
              
                refactor messaging wrappers & add the kafka-clients implementation
              
              
                Cirilla-zmh 6f3587b
              
                add test for kafka clients
              
              
                Cirilla-zmh b6ed6c6
              
                add init params by jvm args
              
              
                Cirilla-zmh 77e3001
              
                improve the integration test of kafka-clients messaging wrapper
              
              
                Cirilla-zmh 8194500
              
                Merge branch 'open-telemetry:main' into main
              
              
                Cirilla-zmh 7ae2731
              
                Merge branch 'open-telemetry:main' into main
              
              
                Cirilla-zmh 2180b1a
              
                change company names of code owners
              
              
                Cirilla-zmh 5686199
              
                add test for api module
              
              
                Cirilla-zmh e588d1e
              
                fix test for api and kafka-clients
              
              
                Cirilla-zmh c73a7ec
              
                fix build and format test
              
              
                Cirilla-zmh 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 | 
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| # OpenTelemetry Messaging Wrappers | ||
|  | ||
| This is a lightweight messaging wrappers API designed to help you quickly add instrumentation to any | ||
| type of messaging system client. To further ease the burden of instrumentation, we will also provide | ||
| predefined implementations for certain messaging systems, helping you seamlessly address the issue | ||
| of broken traces. | ||
|  | ||
| ## Overview | ||
|  | ||
| The primary goal of this API is to simplify the process of adding instrumentation to your messaging | ||
| systems, thereby enhancing observability without introducing significant overhead. Inspired by | ||
| [#13340](https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/13340) and | ||
| [opentelemetry-java-instrumentation](https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/messaging/MessagingAttributesExtractor.java), | ||
| this tool aims to streamline the tracing and monitoring process. | ||
|  | ||
| ## Predefined Implementations | ||
|  | ||
| | Messaging system | Version Scope | Wrapper type | | ||
| |-------------------|---------------|--------------| | ||
| | kafka-clients | `[0.11.0.0,)` | process | | ||
| | aliyun mns-client | `[1.3.0,)` | process | | ||
|  | ||
| ## Quickstart | ||
|  | ||
| ### Step 1 Add dependencies | ||
|  | ||
| To use OpenTelemetry in your project, you need to add the necessary dependencies. Below are the configurations for both | ||
| Gradle and Maven. | ||
|  | ||
| #### Gradle | ||
|  | ||
| ```kotlin | ||
| dependencies { | ||
| implementation("io.opentelemetry.contrib:opentelemetry-messaging-wrappers-api") | ||
| } | ||
| ``` | ||
|  | ||
| #### Maven | ||
|  | ||
| ```xml | ||
| <dependency> | ||
| <groupId>io.opentelemetry.contrib</groupId> | ||
| <artifactId>opentelemetry-messaging-wrappers-api</artifactId> | ||
| </dependency> | ||
| ``` | ||
|  | ||
| ### Step 2 Initializing MessagingWrappers | ||
|  | ||
| Below is an example of how to initialize a messaging wrapper. | ||
|  | ||
| ```java | ||
| public class Demo { | ||
|  | ||
| public static MessagingProcessWrapper<MyMessagingProcessRequest> createWrapper( | ||
| OpenTelemetry openTelemetry, | ||
| MyTextMapGetter textMapGetter, | ||
| List<AttributesExtractor<MyMessagingProcessRequest, Void>> additionalExtractor) { | ||
|  | ||
| return MessagingProcessWrapper.<MyMessagingProcessRequest>defaultBuilder() | ||
| .openTelemetry(openTelemetry) | ||
| .textMapGetter(textMapGetter) | ||
| .addAttributesExtractors(additionalExtractor) | ||
| .build(); | ||
| } | ||
| } | ||
|  | ||
| public class MyMessagingProcessRequest implements MessagingProcessRequest { | ||
| // your implementation here | ||
| } | ||
|  | ||
| public class MyTextMapGetter implements TextMapGetter<MyMessagingProcessRequest> { | ||
| // your implementation here | ||
| } | ||
| ``` | ||
|  | ||
| For arbitrary messaging systems, you need to manually define `MessagingProcessRequest` and the corresponding `TextMapGetter`. | ||
| You can also customize your messaging spans by adding an AttributesExtractor. | ||
|  | ||
| For popular messaging systems, we provide pre-implemented wrappers that allow for out-of-the-box integration. We provide | ||
| an implementation based on the OpenTelemetry semantic convention by default. | ||
|  | ||
| ```java | ||
| public class KafkaDemo { | ||
|  | ||
| public static MessagingProcessWrapper<KafkaProcessRequest> createWrapper() { | ||
| return KafkaHelper.processWrapperBuilder().build(); | ||
| } | ||
| } | ||
| ``` | ||
|  | ||
| ### Step 3 Wrapping your process | ||
|  | ||
| Once the MessagingWrapper are initialized, you can wrap your message processing logic to ensure that tracing spans are | ||
| properly created and propagated. | ||
|  | ||
| **P.S.** Some instrumentations may also [generate process spans](https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/supported-libraries.md). | ||
| If both are enabled, it might result in duplicate nested process spans. It is recommended to disable one of them. | ||
|  | ||
| ```java | ||
| public class Demo { | ||
|  | ||
| private static final MessagingProcessWrapper<MyMessagingProcessRequest> WRAPPER = createWrapper(); | ||
|  | ||
| public String consume(Message message) { | ||
| WRAPPER.doProcess(new MyMessagingProcessRequest(message), () -> { | ||
| // your processing logic | ||
| }); | ||
| } | ||
| } | ||
| ``` | ||
|  | ||
| ## Component owners | ||
|  | ||
| - [Minghui Zhang](https://github.com/Cirilla-zmh), Alibaba | ||
| - [Steve Rao](https://github.com/steverao), Alibaba | ||
|  | ||
| Learn more about component owners in [component_owners.yml](../.github/component_owners.yml). | ||
|  | ||
  
    
      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,21 @@ | ||
| plugins { | ||
| id("otel.java-conventions") | ||
|  | ||
| id("otel.publish-conventions") | ||
| } | ||
|  | ||
| description = "OpenTelemetry Messaging Wrappers - aliyun-mns-sdk implementation" | ||
| otelJava.moduleName.set("io.opentelemetry.contrib.messaging.wrappers.aliyun-mns-sdk") | ||
|  | ||
| dependencies { | ||
| api(project(":messaging-wrappers:api")) | ||
|  | ||
| compileOnly("com.aliyun.mns:aliyun-sdk-mns:1.3.0") | ||
|  | ||
| testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") | ||
| testImplementation("io.opentelemetry:opentelemetry-sdk-trace") | ||
| testImplementation("io.opentelemetry:opentelemetry-sdk-testing") | ||
|  | ||
| testImplementation("io.opentelemetry:opentelemetry-sdk-extension-incubator") | ||
| testImplementation("uk.org.webcompere:system-stubs-jupiter:2.0.3") | ||
| } | 
  
    
      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,2 @@ | ||
| # TODO: uncomment when ready to mark as stable | ||
| # otel.stable=true | 
        
          
  
    
      
          
            55 changes: 55 additions & 0 deletions
          
          55 
        
  ...iyun-mns-sdk/src/main/java/io/opentelemetry/contrib/messaging/wrappers/mns/MnsHelper.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,55 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|  | ||
| package io.opentelemetry.contrib.messaging.wrappers.mns; | ||
|  | ||
| import com.aliyun.mns.model.BaseMessage; | ||
| import com.aliyun.mns.model.MessagePropertyValue; | ||
| import com.aliyun.mns.model.MessageSystemPropertyName; | ||
| import com.aliyun.mns.model.MessageSystemPropertyValue; | ||
| import io.opentelemetry.contrib.messaging.wrappers.mns.semconv.MnsProcessRequest; | ||
|  | ||
| import javax.annotation.Nullable; | ||
|  | ||
| public final class MnsHelper { | ||
|  | ||
| public static <REQUEST extends MnsProcessRequest> | ||
| MnsProcessWrapperBuilder<REQUEST> processWrapperBuilder() { | ||
| return new MnsProcessWrapperBuilder<>(); | ||
| } | ||
|  | ||
| @Nullable | ||
| public static String getMessageHeader(BaseMessage message, String name) { | ||
| MessageSystemPropertyName key = convert2SystemPropertyName(name); | ||
| if (key != null) { | ||
| MessageSystemPropertyValue systemProperty = message.getSystemProperty(key); | ||
| if (systemProperty != null) { | ||
| return systemProperty.getStringValueByType(); | ||
| } | ||
| } | ||
| MessagePropertyValue messagePropertyValue = message.getUserProperties().get(name); | ||
| if (messagePropertyValue != null) { | ||
| return messagePropertyValue.getStringValueByType(); | ||
| } | ||
| return null; | ||
| } | ||
|  | ||
| /** see {@link MessageSystemPropertyName} */ | ||
| @Nullable | ||
| public static MessageSystemPropertyName convert2SystemPropertyName(String name) { | ||
| if (name == null) { | ||
| return null; | ||
| } else if (name.equals(MessageSystemPropertyName.TRACE_PARENT.getValue())) { | ||
| return MessageSystemPropertyName.TRACE_PARENT; | ||
| } else if (name.equals(MessageSystemPropertyName.BAGGAGE.getValue())) { | ||
| return MessageSystemPropertyName.BAGGAGE; | ||
| } else if (name.equals(MessageSystemPropertyName.TRACE_STATE.getValue())) { | ||
| return MessageSystemPropertyName.TRACE_STATE; | ||
| } | ||
| return null; | ||
| } | ||
|  | ||
| private MnsHelper() {} | ||
| } | 
        
          
  
    
      
          
            18 changes: 18 additions & 0 deletions
          
          18 
        
  ...c/main/java/io/opentelemetry/contrib/messaging/wrappers/mns/MnsProcessWrapperBuilder.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,18 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|  | ||
| package io.opentelemetry.contrib.messaging.wrappers.mns; | ||
|  | ||
| import io.opentelemetry.contrib.messaging.wrappers.DefaultMessagingProcessWrapperBuilder; | ||
| import io.opentelemetry.contrib.messaging.wrappers.mns.semconv.MnsProcessRequest; | ||
|  | ||
| public class MnsProcessWrapperBuilder<REQUEST extends MnsProcessRequest> | ||
| extends DefaultMessagingProcessWrapperBuilder<REQUEST> { | ||
|  | ||
| MnsProcessWrapperBuilder() { | ||
| super(); | ||
| super.textMapGetter = MnsTextMapGetter.create(); | ||
| } | ||
| } | 
        
          
  
    
      
          
            78 changes: 78 additions & 0 deletions
          
          78 
        
  ...s-sdk/src/main/java/io/opentelemetry/contrib/messaging/wrappers/mns/MnsTextMapGetter.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,78 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|  | ||
| package io.opentelemetry.contrib.messaging.wrappers.mns; | ||
|  | ||
| import static java.util.Collections.emptyList; | ||
|  | ||
| import com.aliyun.mns.model.Message; | ||
| import com.aliyun.mns.model.MessagePropertyValue; | ||
| import com.aliyun.mns.model.MessageSystemPropertyName; | ||
| import com.aliyun.mns.model.MessageSystemPropertyValue; | ||
| import io.opentelemetry.context.propagation.TextMapGetter; | ||
| import io.opentelemetry.contrib.messaging.wrappers.mns.semconv.MnsProcessRequest; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import javax.annotation.Nullable; | ||
|  | ||
| public class MnsTextMapGetter<REQUEST extends MnsProcessRequest> implements TextMapGetter<REQUEST> { | ||
|  | ||
| public static <REQUEST extends MnsProcessRequest> TextMapGetter<REQUEST> create() { | ||
| return new MnsTextMapGetter<>(); | ||
| } | ||
|  | ||
| @Override | ||
| public Iterable<String> keys(@Nullable REQUEST carrier) { | ||
| if (carrier == null || carrier.getMessage() == null) { | ||
| return emptyList(); | ||
| } | ||
| Message message = carrier.getMessage(); | ||
|  | ||
| Map<String, MessageSystemPropertyValue> systemProperties = message.getSystemProperties(); | ||
| if (systemProperties == null) { | ||
| systemProperties = Collections.emptyMap(); | ||
| } | ||
| Map<String, MessagePropertyValue> userProperties = message.getUserProperties(); | ||
| if (userProperties == null) { | ||
| userProperties = Collections.emptyMap(); | ||
| } | ||
| List<String> keys = new ArrayList<>(systemProperties.size() + userProperties.size()); | ||
| keys.addAll(systemProperties.keySet()); | ||
| keys.addAll(userProperties.keySet()); | ||
| return keys; | ||
| } | ||
|  | ||
| @Nullable | ||
| @Override | ||
| public String get(@Nullable REQUEST carrier, String key) { | ||
| if (carrier == null || carrier.getMessage() == null) { | ||
| return null; | ||
| } | ||
| Message message = carrier.getMessage(); | ||
|  | ||
| // the system property should always take precedence over the user property | ||
| MessageSystemPropertyName systemPropertyName = MnsHelper.convert2SystemPropertyName(key); | ||
| if (systemPropertyName != null) { | ||
| MessageSystemPropertyValue value = message.getSystemProperty(systemPropertyName); | ||
| if (value != null) { | ||
| return value.getStringValueByType(); | ||
| } | ||
| } | ||
|  | ||
| Map<String, MessagePropertyValue> userProperties = message.getUserProperties(); | ||
| if (userProperties == null || userProperties.isEmpty()) { | ||
| return null; | ||
| } | ||
| MessagePropertyValue value = userProperties.getOrDefault(key, null); | ||
| if (value != null) { | ||
| return value.getStringValueByType(); | ||
| } | ||
| return null; | ||
| } | ||
|  | ||
| private MnsTextMapGetter() {} | ||
| } | 
        
          
  
    
      
          
            2 changes: 2 additions & 0 deletions
          
          2 
        
  ...n-mns-sdk/src/main/java/io/opentelemetry/contrib/messaging/wrappers/mns/package-info.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,2 @@ | ||
| /** OpenTelemetry messaging wrappers extension - mns implementation. */ | ||
| package io.opentelemetry.contrib.messaging.wrappers.mns; | 
        
          
  
    
      
          
            97 changes: 97 additions & 0 deletions
          
          97 
        
  .../main/java/io/opentelemetry/contrib/messaging/wrappers/mns/semconv/MnsProcessRequest.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,97 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|  | ||
| package io.opentelemetry.contrib.messaging.wrappers.mns.semconv; | ||
|  | ||
| import com.aliyun.mns.model.Message; | ||
| import io.opentelemetry.contrib.messaging.wrappers.mns.MnsHelper; | ||
| import io.opentelemetry.contrib.messaging.wrappers.semconv.MessagingProcessRequest; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import javax.annotation.Nullable; | ||
|  | ||
| public class MnsProcessRequest implements MessagingProcessRequest { | ||
|  | ||
| private final Message message; | ||
|  | ||
| @Nullable private final String destination; | ||
|  | ||
| public static MnsProcessRequest of(Message message) { | ||
| return of(message, null); | ||
| } | ||
|  | ||
| public static MnsProcessRequest of(Message message, @Nullable String destination) { | ||
| return new MnsProcessRequest(message, destination); | ||
| } | ||
|  | ||
| @Override | ||
| public String getSystem() { | ||
| return "smq"; | ||
| } | ||
|  | ||
| @Nullable | ||
| @Override | ||
| public String getDestination() { | ||
| return this.destination; | ||
| } | ||
|  | ||
| @Nullable | ||
| @Override | ||
| public String getDestinationTemplate() { | ||
| return null; | ||
| } | ||
|  | ||
| @Override | ||
| public boolean isTemporaryDestination() { | ||
| return false; | ||
| } | ||
|  | ||
| @Override | ||
| public boolean isAnonymousDestination() { | ||
| return false; | ||
| } | ||
|  | ||
| @Nullable | ||
| @Override | ||
| public String getConversationId() { | ||
| return null; | ||
| } | ||
|  | ||
| @Nullable | ||
| @Override | ||
| public Long getMessageBodySize() { | ||
| return (long) message.getMessageBodyAsBytes().length; | ||
| } | ||
|  | ||
| @Nullable | ||
| @Override | ||
| public Long getMessageEnvelopeSize() { | ||
| return null; | ||
| } | ||
|  | ||
| @Nullable | ||
| @Override | ||
| public String getMessageId() { | ||
| return message.getMessageId(); | ||
| } | ||
|  | ||
| @Override | ||
| public List<String> getMessageHeader(String name) { | ||
| String header = MnsHelper.getMessageHeader(message, name); | ||
| if (header == null) { | ||
| return Collections.emptyList(); | ||
| } | ||
| return Collections.singletonList(header); | ||
| } | ||
|  | ||
| public Message getMessage() { | ||
| return message; | ||
| } | ||
|  | ||
| private MnsProcessRequest(Message message, @Nullable String destination) { | ||
| this.message = message; | ||
| this.destination = destination; | ||
| } | ||
| } | 
      
      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.
Add following version like other modules? https://github.com/open-telemetry/opentelemetry-java-contrib/tree/main/inferred-spans#usage
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.
Please see #1855 for newest version.