Skip to content

Commit eddb143

Browse files
committed
refactor readme.md
1 parent b40d6c4 commit eddb143

File tree

2 files changed

+115
-20
lines changed

2 files changed

+115
-20
lines changed

messaging-wrappers/README.md

Lines changed: 111 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@ type of messaging system client. To further ease the burden of instrumentation,
55
predefined implementations for certain messaging systems, helping you seamlessly address the issue
66
of broken traces.
77

8+
<details>
9+
<summary>Table of Contents</summary>
10+
11+
- [Overview](#overview)
12+
- [Predefined Implementations](#predefined-implementations)
13+
- [Quickstart For Given Implementations](#quickstart-for-given-implementations)
14+
- [\[Given\] Step 1 Add dependencies](#given-step-1-add-dependencies)
15+
- [\[Given\] Step 2 Initializing MessagingWrappers](#given-step-2-initializing-messagingwrappers)
16+
- [\[Given\] Step 3 Wrapping the Process](#given-step-3-wrapping-the-process)
17+
- [Manual Implementation](#manual-implementation)
18+
- [\[Manual\] Step 1 Add dependencies](#manual-step-1-add-dependencies)
19+
- [\[Manual\] Step 2 Initializing MessagingWrappers](#manual-step-2-initializing-messagingwrappers)
20+
- [\[Manual\] Step 3 Wrapping the Process](#manual-step-3-wrapping-the-process)
21+
- [Component Owners](#component-owners)
22+
23+
</details>
24+
825
## Overview
926

1027
The primary goal of this API is to simplify the process of adding instrumentation to your messaging
@@ -20,15 +37,96 @@ this tool aims to streamline the tracing and monitoring process.
2037
| kafka-clients | `[0.11.0.0,)` | process |
2138
| aliyun mns-client | `[1.3.0,)` | process |
2239

23-
## Quickstart
40+
## Quickstart For Given Implementations
41+
42+
This example will demonstrate how to add automatic instrumentation to your Kafka consumer with process wrapper. For
43+
detailed example, please check out [KafkaClientTest](./kafka-clients/src/test/java/io/opentelemetry/contrib/messaging/wrappers/kafka/KafkaClientTest.java).
2444

25-
### Step 1 Add dependencies
45+
### [Given] Step 1 Add dependencies
2646

2747
To use OpenTelemetry in your project, you need to add the necessary dependencies. Below are the configurations for both
2848
Gradle and Maven.
2949

3050
#### Gradle
3151

52+
```kotlin
53+
dependencies {
54+
implementation("io.opentelemetry.contrib:opentelemetry-messaging-wrappers-kafka-clients:${latest_version}")
55+
}
56+
```
57+
58+
#### Maven
59+
60+
```xml
61+
<dependency>
62+
<groupId>io.opentelemetry.contrib</groupId>
63+
<artifactId>opentelemetry-messaging-wrappers-kafka-clients</artifactId>
64+
<version>${latest_version}</version>
65+
</dependency>
66+
```
67+
68+
### [Given] Step 2 Initializing MessagingWrappers
69+
70+
For `kafka-clients`, we provide pre-implemented wrappers that allow for out-of-the-box integration. We provide
71+
an implementation based on the OpenTelemetry semantic convention by default.
72+
73+
```java
74+
public class KafkaDemo {
75+
76+
public static MessagingProcessWrapper<KafkaProcessRequest> createWrapper() {
77+
return KafkaHelper.processWrapperBuilder().build();
78+
}
79+
}
80+
```
81+
82+
### [Given] Step 3 Wrapping the Process
83+
84+
Once the MessagingWrapper are initialized, you can wrap your message processing logic to ensure that tracing spans are
85+
properly created and propagated.
86+
87+
**P.S.** Some instrumentations may also [generate process spans](https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/supported-libraries.md).
88+
If both are enabled, it might result in duplicate nested process spans. It is recommended to disable one of them.
89+
90+
```java
91+
public class Demo {
92+
93+
private static final MessagingProcessWrapper<KafkaProcessRequest> WRAPPER = createWrapper();
94+
95+
// please initialize consumer
96+
private Consumer<Integer, String> consumer;
97+
98+
public String consume() {
99+
ConsumerRecords<?, ?> records = consumer.poll(Duration.ofSeconds(5));
100+
ConsumerRecord<?, ?> record = records.iterator().next();
101+
102+
return WRAPPER.doProcess(
103+
KafkaProcessRequest.of(record, groupId, clientId), () -> {
104+
// your processing logic
105+
return "success";
106+
});
107+
}
108+
109+
public void consumeWithoutResult() {
110+
ConsumerRecords<?, ?> records = consumer.poll(Duration.ofSeconds(5));
111+
ConsumerRecord<?, ?> record = records.iterator().next();
112+
113+
WRAPPER.doProcess(
114+
KafkaProcessRequest.of(record, groupId, clientId), () -> {
115+
// your processing logic
116+
});
117+
}
118+
}
119+
```
120+
121+
## Manual Implementation
122+
123+
You can also build implementations based on the `messaging-wrappers-api` for any messaging system to accommodate your
124+
custom message protocol. For detailed example, please check out [UserDefinedMessageSystemTest](./api/src/test/java/io/opentelemetry/contrib/messaging/wrappers/UserDefinedMessageSystemTest.java).
125+
126+
### [Manual] Step 1 Add dependencies
127+
128+
#### Gradle
129+
32130
```kotlin
33131
dependencies {
34132
implementation("io.opentelemetry.contrib:opentelemetry-messaging-wrappers-api:${latest_version}")
@@ -45,7 +143,7 @@ dependencies {
45143
</dependency>
46144
```
47145

48-
### Step 2 Initializing MessagingWrappers
146+
### [Manual] Step 2 Initializing MessagingWrappers
49147

50148
Below is an example of how to initialize a messaging wrapper.
51149

@@ -75,21 +173,9 @@ public class MyTextMapGetter implements TextMapGetter<MyMessagingProcessRequest>
75173
```
76174

77175
For arbitrary messaging systems, you need to manually define `MessagingProcessRequest` and the corresponding `TextMapGetter`.
78-
You can also customize your messaging spans by adding an AttributesExtractor.
79-
80-
For popular messaging systems, we provide pre-implemented wrappers that allow for out-of-the-box integration. We provide
81-
an implementation based on the OpenTelemetry semantic convention by default.
82-
83-
```java
84-
public class KafkaDemo {
85-
86-
public static MessagingProcessWrapper<KafkaProcessRequest> createWrapper() {
87-
return KafkaHelper.processWrapperBuilder().build();
88-
}
89-
}
90-
```
176+
You can also customize your messaging spans by adding an `AttributesExtractor`.
91177

92-
### Step 3 Wrapping your process
178+
### [Manual] Step 3 Wrapping the Process
93179

94180
Once the MessagingWrapper are initialized, you can wrap your message processing logic to ensure that tracing spans are
95181
properly created and propagated.
@@ -103,14 +189,21 @@ public class Demo {
103189
private static final MessagingProcessWrapper<MyMessagingProcessRequest> WRAPPER = createWrapper();
104190

105191
public String consume(Message message) {
192+
return WRAPPER.doProcess(new MyMessagingProcessRequest(message), () -> {
193+
// your processing logic
194+
return "success";
195+
});
196+
}
197+
198+
public void consumeWithoutReturn(Message message) {
106199
WRAPPER.doProcess(new MyMessagingProcessRequest(message), () -> {
107200
// your processing logic
108201
});
109202
}
110203
}
111204
```
112205

113-
## Component owners
206+
## Component Owners
114207

115208
- [Minghui Zhang](https://github.com/Cirilla-zmh), Alibaba
116209
- [Steve Rao](https://github.com/steverao), Alibaba

messaging-wrappers/aliyun-mns-sdk/build.gradle.kts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ dependencies {
1212

1313
compileOnly("com.aliyun.mns:aliyun-sdk-mns:1.3.0")
1414

15+
testImplementation(project(":messaging-wrappers:testing"))
1516
testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")
1617
testImplementation("io.opentelemetry:opentelemetry-sdk-trace")
1718
testImplementation("io.opentelemetry:opentelemetry-sdk-testing")
18-
1919
testImplementation("io.opentelemetry:opentelemetry-sdk-extension-incubator")
20-
testImplementation("uk.org.webcompere:system-stubs-jupiter:2.0.3")
20+
21+
testImplementation("com.aliyun.mns:aliyun-sdk-mns:1.3.0")
22+
testImplementation("org.springframework.boot:spring-boot-starter-web:3.0.0")
2123
}

0 commit comments

Comments
 (0)