Skip to content

Commit f2ad583

Browse files
feat:support baggage close. (#571)
1 parent 0234c19 commit f2ad583

File tree

7 files changed

+253
-75
lines changed

7 files changed

+253
-75
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-common/polaris-model/src/main/java/com/tencent/polaris/api/utils/StringUtils.java

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
public class StringUtils {
2323

24+
public static final String EMPTY = "";
25+
2426
private static final String[] EMPTY_STRING_ARRAY = new String[0];
2527

2628
public static boolean isBlank(String str) {
@@ -271,7 +273,111 @@ public static String nullSafeToString(Object obj) {
271273
return nullSafeToString((short[]) ((short[]) obj));
272274
} else {
273275
String str = obj.toString();
274-
return str != null ? str : "";
276+
return str != null ? str : EMPTY;
277+
}
278+
}
279+
280+
public static String substring(String str, int start) {
281+
if (str == null) {
282+
return null;
283+
}
284+
285+
// handle negatives, which means last n characters
286+
if (start < 0) {
287+
start = str.length() + start; // remember start is negative
288+
}
289+
290+
if (start < 0) {
291+
start = 0;
292+
}
293+
if (start > str.length()) {
294+
return EMPTY;
295+
}
296+
297+
return str.substring(start);
298+
}
299+
300+
public static String substring(String str, int start, int end) {
301+
if (str == null) {
302+
return null;
303+
}
304+
305+
// handle negatives
306+
if (end < 0) {
307+
end = str.length() + end; // remember end is negative
308+
}
309+
if (start < 0) {
310+
start = str.length() + start; // remember start is negative
311+
}
312+
313+
// check length next
314+
if (end > str.length()) {
315+
end = str.length();
316+
}
317+
318+
// if start is greater than end, return ""
319+
if (start > end) {
320+
return EMPTY;
321+
}
322+
323+
if (start < 0) {
324+
start = 0;
325+
}
326+
if (end < 0) {
327+
end = 0;
328+
}
329+
330+
return str.substring(start, end);
331+
}
332+
333+
public static boolean startsWith(String str, String prefix) {
334+
return startsWith(str, prefix, false);
335+
}
336+
337+
public static boolean startsWithIgnoreCase(String str, String prefix) {
338+
return startsWith(str, prefix, true);
339+
}
340+
341+
private static boolean startsWith(String str, String prefix, boolean ignoreCase) {
342+
if (str == null || prefix == null) {
343+
return (str == null && prefix == null);
344+
}
345+
if (prefix.length() > str.length()) {
346+
return false;
347+
}
348+
return str.regionMatches(ignoreCase, 0, prefix, 0, prefix.length());
349+
}
350+
351+
public static boolean endsWith(String str, String suffix) {
352+
return endsWith(str, suffix, false);
353+
}
354+
355+
public static boolean endsWithIgnoreCase(String str, String suffix) {
356+
return endsWith(str, suffix, true);
357+
}
358+
359+
private static boolean endsWith(String str, String suffix, boolean ignoreCase) {
360+
if (str == null || suffix == null) {
361+
return (str == null && suffix == null);
362+
}
363+
if (suffix.length() > str.length()) {
364+
return false;
365+
}
366+
int strOffset = str.length() - suffix.length();
367+
return str.regionMatches(ignoreCase, strOffset, suffix, 0, suffix.length());
368+
}
369+
370+
public static String upperCase(String str) {
371+
if (str == null) {
372+
return null;
373+
}
374+
return str.toUpperCase();
375+
}
376+
377+
public static String[] split(String original, String separator) {
378+
if (original == null) {
379+
return null;
275380
}
381+
return original.split(separator);
276382
}
277383
}

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
}

0 commit comments

Comments
 (0)