Skip to content

Commit 06c592b

Browse files
committed
add ot
1 parent 7acfccb commit 06c592b

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

mse-simple-demo/SpringCloudGateway/pom.xml

+24
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@
2222
</properties>
2323

2424
<dependencies>
25+
<dependency>
26+
<groupId>io.opentelemetry</groupId>
27+
<artifactId>opentelemetry-api</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>io.opentelemetry</groupId>
31+
<artifactId>opentelemetry-sdk-trace</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>io.opentelemetry</groupId>
35+
<artifactId>opentelemetry-sdk</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>io.opentelemetry</groupId>
39+
<artifactId>opentelemetry-exporter-logging</artifactId>
40+
</dependency>
41+
2542
<dependency>
2643
<groupId>com.alibaba.cloud</groupId>
2744
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
@@ -54,6 +71,13 @@
5471

5572
<dependencyManagement>
5673
<dependencies>
74+
<dependency>
75+
<groupId>io.opentelemetry</groupId>
76+
<artifactId>opentelemetry-bom</artifactId>
77+
<version>1.23.0</version>
78+
<type>pom</type>
79+
<scope>import</scope>
80+
</dependency>
5781
<dependency>
5882
<groupId>org.springframework.cloud</groupId>
5983
<artifactId>spring-cloud-dependencies</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.alibabcloud.mse.demo;
2+
3+
import io.opentelemetry.api.baggage.Baggage;
4+
import io.opentelemetry.context.Context;
5+
import io.opentelemetry.context.Scope;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
9+
import org.springframework.cloud.gateway.filter.GlobalFilter;
10+
import org.springframework.core.Ordered;
11+
import org.springframework.stereotype.Component;
12+
import org.springframework.util.MultiValueMap;
13+
import org.springframework.web.server.ServerWebExchange;
14+
import reactor.core.publisher.Mono;
15+
16+
@Component
17+
public class GrayGatewayFilter implements GlobalFilter, Ordered {
18+
private static final Logger log = LoggerFactory.getLogger(GrayGatewayFilter.class);
19+
20+
@Override
21+
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
22+
MultiValueMap<String, String> params = exchange.getRequest().getQueryParams();
23+
Scope baggageScope = null;
24+
try {
25+
if ("12345".equals(params.getFirst("userId"))) {
26+
String tag = "gray";
27+
// 标记流量标签
28+
// key固定为__microservice_tag__
29+
// value为合法json,同时tag为流量的标签结果
30+
Baggage baggage = Baggage.builder()
31+
.put("__microservice_tag__", "[{\"name\":\"" + tag + "\"}]")
32+
.build();
33+
log.info("request with userId: {}, tagged {}", params.getFirst("userId"), tag);
34+
baggageScope = baggage.storeInContext(Context.current()).makeCurrent();
35+
}
36+
return chain.filter(exchange);
37+
} finally {
38+
if (baggageScope != null) {
39+
baggageScope.close();
40+
}
41+
}
42+
}
43+
44+
/**
45+
* 由于正常的业务请求处理也是一个Filter,所以这儿要确保顺序在业务Filter之前
46+
* @return
47+
*/
48+
@Override
49+
public int getOrder() {
50+
return -1;
51+
}
52+
}
53+

0 commit comments

Comments
 (0)