Skip to content

Commit d496bc2

Browse files
committed
feat:support baggage close.
1 parent 0234c19 commit d496bc2

File tree

6 files changed

+146
-74
lines changed

6 files changed

+146
-74
lines changed

polaris-assembly/polaris-assembly-api/src/main/java/com/tencent/polaris/assembly/api/pojo/TraceAttributes.java

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,41 @@
2121

2222
public class TraceAttributes {
2323

24-
/**
25-
* The location to put attributes
26-
*/
27-
public enum AttributeLocation {
28-
SPAN,
29-
BAGGAGE
30-
}
24+
/**
25+
* The location to put attributes
26+
*/
27+
public enum AttributeLocation {
28+
SPAN,
29+
BAGGAGE
30+
}
3131

32-
private Map<String, String> attributes;
32+
private Map<String, String> attributes;
3333

34+
private AttributeLocation attributeLocation;
3435

35-
private AttributeLocation attributeLocation;
36+
private Object otScope;
3637

37-
public Map<String, String> getAttributes() {
38-
return attributes;
39-
}
38+
public Map<String, String> getAttributes() {
39+
return attributes;
40+
}
4041

41-
public void setAttributes(Map<String, String> attributes) {
42-
this.attributes = attributes;
43-
}
42+
public void setAttributes(Map<String, String> attributes) {
43+
this.attributes = attributes;
44+
}
4445

45-
public AttributeLocation getAttributeLocation() {
46-
return attributeLocation;
47-
}
46+
public AttributeLocation getAttributeLocation() {
47+
return attributeLocation;
48+
}
4849

49-
public void setAttributeLocation(AttributeLocation attributeLocation) {
50-
this.attributeLocation = attributeLocation;
51-
}
50+
public void setAttributeLocation(AttributeLocation attributeLocation) {
51+
this.attributeLocation = attributeLocation;
52+
}
53+
54+
public Object getOtScope() {
55+
return otScope;
56+
}
57+
58+
public void setOtScope(Object otScope) {
59+
this.otScope = otScope;
60+
}
5261
}

polaris-assembly/polaris-assembly-client/src/main/java/com/tencent/polaris/assembly/client/flow/DefaultAssemblyFlow.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,19 @@ public void updateTraceAttributes(TraceAttributes traceAttributes) {
191191
return;
192192
}
193193
TraceReporter traceReporter = extensions.getTraceReporter();
194-
if (null == traceReporter) {
194+
if (null == traceReporter || !traceReporter.isEnabled()) {
195195
return;
196196
}
197197
switch (traceAttributes.getAttributeLocation()) {
198-
case SPAN:
199-
traceReporter.setSpanAttributes(traceAttributes.getAttributes());
200-
break;
201-
case BAGGAGE:
202-
traceReporter.setBaggageAttributes(traceAttributes.getAttributes());
203-
break;
204-
default:
205-
break;
198+
case SPAN:
199+
traceReporter.setSpanAttributes(traceAttributes.getAttributes());
200+
break;
201+
case BAGGAGE:
202+
Object otScope = traceReporter.setBaggageAttributes(traceAttributes.getAttributes());
203+
traceAttributes.setOtScope(otScope);
204+
break;
205+
default:
206+
break;
206207
}
207208
}
208209

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.api.utils;
19+
20+
import com.tencent.polaris.logging.LoggerFactory;
21+
import org.slf4j.Logger;
22+
23+
/**
24+
* Utils for class.
25+
*
26+
* @author Haotian Zhang
27+
*/
28+
public class ClassUtils {
29+
30+
private static final Logger LOG = LoggerFactory.getLogger(RuleUtils.class);
31+
32+
/**
33+
* Check if class is present.
34+
*
35+
* @param className class name
36+
* @return true if present, false otherwise
37+
*/
38+
public static boolean isClassPresent(String className) {
39+
try {
40+
Class.forName(className);
41+
return true;
42+
} catch (ClassNotFoundException e) {
43+
return false;
44+
} catch (Throwable throwable) {
45+
LOG.warn("Failed to check class present", throwable);
46+
return false;
47+
}
48+
}
49+
}

polaris-plugins/polaris-plugin-api/src/main/java/com/tencent/polaris/api/plugin/stat/TraceReporter.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
package com.tencent.polaris.api.plugin.stat;
1919

20-
import java.util.Map;
21-
2220
import com.tencent.polaris.api.plugin.Plugin;
2321

22+
import java.util.Map;
23+
2424
/**
2525
* 【扩展点接口】上报调用链
2626
*
@@ -29,15 +29,22 @@
2929
*/
3030
public interface TraceReporter extends Plugin {
3131

32-
/**
33-
* set the attributes in trace span
34-
* @param attributes span attributes
35-
*/
36-
void setSpanAttributes(Map<String, String> attributes);
32+
/**
33+
* if the reporter is enabled
34+
*/
35+
boolean isEnabled();
36+
37+
/**
38+
* set the attributes in trace span
39+
*
40+
* @param attributes span attributes
41+
*/
42+
void setSpanAttributes(Map<String, String> attributes);
3743

38-
/**
39-
* set the attributes in baggage span
40-
* @param attributes baggage attributes
41-
*/
42-
void setBaggageAttributes(Map<String, String> attributes);
44+
/**
45+
* set the attributes in baggage span
46+
*
47+
* @param attributes baggage attributes
48+
*/
49+
Object setBaggageAttributes(Map<String, String> attributes);
4350
}

polaris-plugins/polaris-plugins-observability/trace-otel/src/main/java/com/tencent/polaris/plugins/stat/otel/OtelTraceReporter.java

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.tencent.polaris.api.plugin.common.PluginTypes;
2727
import com.tencent.polaris.api.plugin.compose.Extensions;
2828
import com.tencent.polaris.api.plugin.stat.TraceReporter;
29+
import com.tencent.polaris.api.utils.ClassUtils;
2930
import com.tencent.polaris.logging.LoggerFactory;
3031
import com.tencent.polaris.logging.PolarisLogging;
3132
import io.opentelemetry.api.baggage.Baggage;
@@ -35,45 +36,50 @@
3536

3637
public class OtelTraceReporter implements TraceReporter {
3738

38-
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisLogging.class);
39+
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisLogging.class);
3940

40-
@Override
41-
public String getName() {
42-
return TraceReporterConfig.DEFAULT_REPORTER_OTEL;
43-
}
41+
@Override
42+
public String getName() {
43+
return TraceReporterConfig.DEFAULT_REPORTER_OTEL;
44+
}
4445

45-
@Override
46-
public PluginType getType() {
47-
return PluginTypes.TRACE_REPORTER.getBaseType();
48-
}
46+
@Override
47+
public PluginType getType() {
48+
return PluginTypes.TRACE_REPORTER.getBaseType();
49+
}
4950

50-
@Override
51-
public void init(InitContext ctx) throws PolarisException {
51+
@Override
52+
public void init(InitContext ctx) throws PolarisException {
5253

53-
}
54+
}
5455

55-
@Override
56-
public void postContextInit(Extensions ctx) throws PolarisException {
56+
@Override
57+
public void postContextInit(Extensions ctx) throws PolarisException {
5758

58-
}
59+
}
5960

60-
@Override
61-
public void destroy() {
61+
@Override
62+
public void destroy() {
6263

63-
}
64+
}
6465

65-
@Override
66-
public void setSpanAttributes(Map<String, String> attributes) {
67-
LOGGER.debug("OtelTraceReporter: setSpanAttributes: {}", attributes);
68-
Span span = Span.current();
69-
attributes.forEach(span::setAttribute);
70-
}
66+
@Override
67+
public boolean isEnabled() {
68+
return ClassUtils.isClassPresent("io.opentelemetry.api.trace.Span");
69+
}
7170

72-
@Override
73-
public void setBaggageAttributes(Map<String, String> attributes) {
74-
LOGGER.debug("OtelTraceReporter: setBaggageAttributes: {}", attributes);
75-
BaggageBuilder builder = Baggage.current().toBuilder();
76-
attributes.forEach(builder::put);
77-
builder.build().makeCurrent();
78-
}
71+
@Override
72+
public void setSpanAttributes(Map<String, String> attributes) {
73+
LOGGER.debug("OtelTraceReporter: setSpanAttributes: {}", attributes);
74+
Span span = Span.current();
75+
attributes.forEach(span::setAttribute);
76+
}
77+
78+
@Override
79+
public Object setBaggageAttributes(Map<String, String> attributes) {
80+
LOGGER.debug("OtelTraceReporter: setBaggageAttributes: {}", attributes);
81+
BaggageBuilder builder = Baggage.current().toBuilder();
82+
attributes.forEach(builder::put);
83+
return builder.build().makeCurrent();
84+
}
7985
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

6767
<properties>
6868
<!-- Project revision -->
69-
<revision>2.0.0.0-RC5</revision>
69+
<revision>2.0.0.0-SNAPSHOT</revision>
7070

7171
<timestamp>${maven.build.timestamp}</timestamp>
7272
<skip.maven.deploy>false</skip.maven.deploy>

0 commit comments

Comments
 (0)