Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,41 @@

public class TraceAttributes {

/**
* The location to put attributes
*/
public enum AttributeLocation {
SPAN,
BAGGAGE
}
/**
* The location to put attributes
*/
public enum AttributeLocation {
SPAN,
BAGGAGE
}

private Map<String, String> attributes;
private Map<String, String> attributes;

private AttributeLocation attributeLocation;

private AttributeLocation attributeLocation;
private Object otScope;

public Map<String, String> getAttributes() {
return attributes;
}
public Map<String, String> getAttributes() {
return attributes;
}

public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}
public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}

public AttributeLocation getAttributeLocation() {
return attributeLocation;
}
public AttributeLocation getAttributeLocation() {
return attributeLocation;
}

public void setAttributeLocation(AttributeLocation attributeLocation) {
this.attributeLocation = attributeLocation;
}
public void setAttributeLocation(AttributeLocation attributeLocation) {
this.attributeLocation = attributeLocation;
}

public Object getOtScope() {
return otScope;
}

public void setOtScope(Object otScope) {
this.otScope = otScope;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,19 @@ public void updateTraceAttributes(TraceAttributes traceAttributes) {
return;
}
TraceReporter traceReporter = extensions.getTraceReporter();
if (null == traceReporter) {
if (null == traceReporter || !traceReporter.isEnabled()) {
return;
}
switch (traceAttributes.getAttributeLocation()) {
case SPAN:
traceReporter.setSpanAttributes(traceAttributes.getAttributes());
break;
case BAGGAGE:
traceReporter.setBaggageAttributes(traceAttributes.getAttributes());
break;
default:
break;
case SPAN:
traceReporter.setSpanAttributes(traceAttributes.getAttributes());
break;
case BAGGAGE:
Object otScope = traceReporter.setBaggageAttributes(traceAttributes.getAttributes());
traceAttributes.setOtScope(otScope);
break;
default:
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package com.tencent.polaris.api.utils;

import com.tencent.polaris.logging.LoggerFactory;
import org.slf4j.Logger;

/**
* Utils for class.
*
* @author Haotian Zhang
*/
public class ClassUtils {

private static final Logger LOG = LoggerFactory.getLogger(RuleUtils.class);

/**
* Check if class is present.
*
* @param className class name
* @return true if present, false otherwise
*/
public static boolean isClassPresent(String className) {
try {
Class.forName(className);
return true;
} catch (ClassNotFoundException e) {
return false;
} catch (Throwable throwable) {
LOG.warn("Failed to check class present", throwable);
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

public class StringUtils {

public static final String EMPTY = "";

private static final String[] EMPTY_STRING_ARRAY = new String[0];

public static boolean isBlank(String str) {
Expand Down Expand Up @@ -271,7 +273,111 @@ public static String nullSafeToString(Object obj) {
return nullSafeToString((short[]) ((short[]) obj));
} else {
String str = obj.toString();
return str != null ? str : "";
return str != null ? str : EMPTY;
}
}

public static String substring(String str, int start) {
if (str == null) {
return null;
}

// handle negatives, which means last n characters
if (start < 0) {
start = str.length() + start; // remember start is negative
}

if (start < 0) {
start = 0;
}
if (start > str.length()) {
return EMPTY;
}

return str.substring(start);
}

public static String substring(String str, int start, int end) {
if (str == null) {
return null;
}

// handle negatives
if (end < 0) {
end = str.length() + end; // remember end is negative
}
if (start < 0) {
start = str.length() + start; // remember start is negative
}

// check length next
if (end > str.length()) {
end = str.length();
}

// if start is greater than end, return ""
if (start > end) {
return EMPTY;
}

if (start < 0) {
start = 0;
}
if (end < 0) {
end = 0;
}

return str.substring(start, end);
}

public static boolean startsWith(String str, String prefix) {
return startsWith(str, prefix, false);
}

public static boolean startsWithIgnoreCase(String str, String prefix) {
return startsWith(str, prefix, true);
}

private static boolean startsWith(String str, String prefix, boolean ignoreCase) {
if (str == null || prefix == null) {
return (str == null && prefix == null);
}
if (prefix.length() > str.length()) {
return false;
}
return str.regionMatches(ignoreCase, 0, prefix, 0, prefix.length());
}

public static boolean endsWith(String str, String suffix) {
return endsWith(str, suffix, false);
}

public static boolean endsWithIgnoreCase(String str, String suffix) {
return endsWith(str, suffix, true);
}

private static boolean endsWith(String str, String suffix, boolean ignoreCase) {
if (str == null || suffix == null) {
return (str == null && suffix == null);
}
if (suffix.length() > str.length()) {
return false;
}
int strOffset = str.length() - suffix.length();
return str.regionMatches(ignoreCase, strOffset, suffix, 0, suffix.length());
}

public static String upperCase(String str) {
if (str == null) {
return null;
}
return str.toUpperCase();
}

public static String[] split(String original, String separator) {
if (original == null) {
return null;
}
return original.split(separator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

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

import java.util.Map;

import com.tencent.polaris.api.plugin.Plugin;

import java.util.Map;

/**
* 【扩展点接口】上报调用链
*
Expand All @@ -29,15 +29,22 @@
*/
public interface TraceReporter extends Plugin {

/**
* set the attributes in trace span
* @param attributes span attributes
*/
void setSpanAttributes(Map<String, String> attributes);
/**
* if the reporter is enabled
*/
boolean isEnabled();

/**
* set the attributes in trace span
*
* @param attributes span attributes
*/
void setSpanAttributes(Map<String, String> attributes);

/**
* set the attributes in baggage span
* @param attributes baggage attributes
*/
void setBaggageAttributes(Map<String, String> attributes);
/**
* set the attributes in baggage span
*
* @param attributes baggage attributes
*/
Object setBaggageAttributes(Map<String, String> attributes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.tencent.polaris.api.plugin.common.PluginTypes;
import com.tencent.polaris.api.plugin.compose.Extensions;
import com.tencent.polaris.api.plugin.stat.TraceReporter;
import com.tencent.polaris.api.utils.ClassUtils;
import com.tencent.polaris.logging.LoggerFactory;
import com.tencent.polaris.logging.PolarisLogging;
import io.opentelemetry.api.baggage.Baggage;
Expand All @@ -35,45 +36,50 @@

public class OtelTraceReporter implements TraceReporter {

private static final Logger LOGGER = LoggerFactory.getLogger(PolarisLogging.class);
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisLogging.class);

@Override
public String getName() {
return TraceReporterConfig.DEFAULT_REPORTER_OTEL;
}
@Override
public String getName() {
return TraceReporterConfig.DEFAULT_REPORTER_OTEL;
}

@Override
public PluginType getType() {
return PluginTypes.TRACE_REPORTER.getBaseType();
}
@Override
public PluginType getType() {
return PluginTypes.TRACE_REPORTER.getBaseType();
}

@Override
public void init(InitContext ctx) throws PolarisException {
@Override
public void init(InitContext ctx) throws PolarisException {

}
}

@Override
public void postContextInit(Extensions ctx) throws PolarisException {
@Override
public void postContextInit(Extensions ctx) throws PolarisException {

}
}

@Override
public void destroy() {
@Override
public void destroy() {

}
}

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

@Override
public void setBaggageAttributes(Map<String, String> attributes) {
LOGGER.debug("OtelTraceReporter: setBaggageAttributes: {}", attributes);
BaggageBuilder builder = Baggage.current().toBuilder();
attributes.forEach(builder::put);
builder.build().makeCurrent();
}
@Override
public void setSpanAttributes(Map<String, String> attributes) {
LOGGER.debug("OtelTraceReporter: setSpanAttributes: {}", attributes);
Span span = Span.current();
attributes.forEach(span::setAttribute);
}

@Override
public Object setBaggageAttributes(Map<String, String> attributes) {
LOGGER.debug("OtelTraceReporter: setBaggageAttributes: {}", attributes);
BaggageBuilder builder = Baggage.current().toBuilder();
attributes.forEach(builder::put);
return builder.build().makeCurrent();
}
}
Loading