-
Notifications
You must be signed in to change notification settings - Fork 163
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 10 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,117 @@ | ||
# 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 |
48 changes: 48 additions & 0 deletions
48
...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,48 @@ | ||
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; | ||
|
||
public final class MNSHelper { | ||
|
||
public static <REQUEST extends MNSProcessRequest> MNSProcessWrapperBuilder<REQUEST> processWrapperBuilder() { | ||
return new MNSProcessWrapperBuilder<>(); | ||
} | ||
|
||
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} | ||
* */ | ||
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() { | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...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,13 @@ | ||
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(); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...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,71 @@ | ||
package io.opentelemetry.contrib.messaging.wrappers.mns; | ||
|
||
import static java.util.Collections.emptyList; | ||
|
||
import com.aliyun.mns.model.*; | ||
import io.opentelemetry.context.propagation.TextMapGetter; | ||
import io.opentelemetry.contrib.messaging.wrappers.mns.semconv.MNSProcessRequest; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
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; | ||
} | ||
|
||
@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() { | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...dk/src/main/java/io/opentelemetry/contrib/messaging/wrappers/mns/example/MNSConsumer.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,72 @@ | ||
package io.opentelemetry.contrib.messaging.wrappers.mns.example; | ||
|
||
import com.aliyun.mns.client.CloudAccount; | ||
import com.aliyun.mns.client.CloudQueue; | ||
import com.aliyun.mns.client.MNSClient; | ||
import com.aliyun.mns.common.ClientException; | ||
import com.aliyun.mns.common.ServiceException; | ||
import com.aliyun.mns.model.Message; | ||
import com.aliyun.mns.model.MessageSystemPropertyValue; | ||
import io.opentelemetry.contrib.messaging.wrappers.MessagingProcessWrapper; | ||
import io.opentelemetry.contrib.messaging.wrappers.mns.MNSHelper; | ||
import io.opentelemetry.contrib.messaging.wrappers.mns.semconv.MNSProcessRequest; | ||
|
||
import static com.aliyun.mns.model.MessageSystemPropertyName.TRACE_PARENT; | ||
|
||
public class MNSConsumer { | ||
|
||
public static void main(String[] args) { | ||
// 1. create wrapper by default | ||
MessagingProcessWrapper<MNSProcessRequest> wrapper = MNSHelper.processWrapperBuilder().build(); | ||
CloudAccount account = new CloudAccount("my-ak", "my-sk", "endpoint"); | ||
MNSClient client = account.getMNSClient(); | ||
|
||
final String queueName = "test-queue"; | ||
try { | ||
CloudQueue queue = client.getQueueRef(queueName); | ||
while (true) { | ||
Message popMsg = queue.popMessage(5); | ||
if (popMsg != null) { | ||
// 2. wrap your consume block | ||
String result = wrapper.doProcess(MNSProcessRequest.of(popMsg, queueName), () -> { | ||
System.out.println("message handle: " + popMsg.getReceiptHandle()); | ||
System.out.println("message body: " + popMsg.getMessageBodyAsString()); | ||
System.out.println("message id: " + popMsg.getMessageId()); | ||
System.out.println("message dequeue count:" + popMsg.getDequeueCount()); | ||
MessageSystemPropertyValue systemProperty = popMsg.getSystemProperty(TRACE_PARENT); | ||
if (systemProperty != null) { | ||
System.out.println("message trace parent: " + systemProperty.getStringValueByType()); | ||
} else { | ||
System.out.println("empty system property"); | ||
} | ||
//<<to add your special logic.>> | ||
|
||
queue.deleteMessage(popMsg.getReceiptHandle()); | ||
System.out.println("delete message successfully\n"); | ||
return "success"; | ||
}); | ||
} | ||
} | ||
} catch (ClientException ce) { | ||
System.out.println("Something wrong with the network connection between client and MNS service." | ||
+ "Please check your network and DNS availablity."); | ||
ce.printStackTrace(); | ||
} catch (ServiceException se) { | ||
if (se.getErrorCode().equals("QueueNotExist")) { | ||
System.out.println("Queue is not exist.Please create queue before use"); | ||
} else if (se.getErrorCode().equals("TimeExpired")) { | ||
System.out.println("The request is time expired. Please check your local machine timeclock"); | ||
} | ||
/* | ||
you can get more MNS service error code in following link. | ||
https://help.aliyun.com/document_detail/mns/api_reference/error_code/error_code.html?spm=5176.docmns/api_reference/error_code/error_response | ||
*/ | ||
se.printStackTrace(); | ||
} catch (Exception e) { | ||
System.out.println("Unknown exception happened!"); | ||
e.printStackTrace(); | ||
} | ||
|
||
client.close(); | ||
} | ||
} |
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; |
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.