Skip to content

Commit

Permalink
[#7742] export trace info for agent-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
yjqg6666 committed Sep 13, 2022
1 parent d745085 commit e7b1184
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2022 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.navercorp.pinpoint.sdk.v1.trace.info;

/**
* @author yjqg6666
* @since 2020-05-15 14:43:39
*/
@SuppressWarnings("unused")
public class DefaultTraceInfo implements TraceInfo {

private String transactionId;

private long spanId;

public DefaultTraceInfo(String txId, long spanId) {
setTransactionId(txId);
setSpanId(spanId);
}

public DefaultTraceInfo() {
}

@Override
public String getTransactionId() {
return transactionId;
}

@Override
public void setTransactionId(String txId) {
if (txId == null) {
throw new NullPointerException("TransactionId can NOT be null");
}
this.transactionId = txId;
}

@Override
public long getSpanId() {
return spanId;
}

@Override
public void setSpanId(long spanId) {
this.spanId = spanId;
}

@Override
public String toString() {
return "DefaultTraceInfo{" +
"transactionId='" + transactionId + '\'' +
", spanId=" + spanId +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2022 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.navercorp.pinpoint.sdk.v1.trace.info;

/**
* @author yjqg6666
*/
@SuppressWarnings("unused")
public interface TraceInfo {

/**
* get transaction id
*
* @return txId
*/
String getTransactionId();

/**
* set transaction id
*
* @param txId txId
*/
void setTransactionId(String txId);

/**
* get span id
*
* @return span id
*/
long getSpanId();

/**
* set span id
*
* @param spanId span id
*/
void setSpanId(long spanId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2022 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.navercorp.pinpoint.sdk.v1.trace.info;

/**
* @author yjqg6666
* @since 2020-05-15 14:25:50
*/
@SuppressWarnings("unused")
public class TraceInfoHolder {

private static final ThreadLocal<TraceInfo> HOLDER = new ThreadLocal<>();

public static String getTransactionId() {
TraceInfo traceInfo = getTraceInfo();
return traceInfo == null ? null : traceInfo.getTransactionId();
}

public static long getSpanId() {
TraceInfo traceInfo = getTraceInfo();
return traceInfo == null ? 0 : traceInfo.getSpanId();
}

public static TraceInfo getTraceInfo() {
return HOLDER.get();
}

public static void setTraceInfo(TraceInfo traceInfo) {
if (traceInfo == null) {
throw new NullPointerException("TraceInfo can NOT be null");
}
if (traceInfo.getTransactionId() == null) {
throw new NullPointerException("TraceInfo.transactionId can NOT be null");
}
HOLDER.set(traceInfo);
}

public static void setTraceInfo(String txId, long spanId) {
HOLDER.set(new DefaultTraceInfo(txId, spanId));
}

public static void clearTraceInfo() {
HOLDER.remove();
}

public static String toInfoString() {
return "DefaultTraceInfoHolder{data=" + getTraceInfo() + "}";
}
}
4 changes: 4 additions & 0 deletions agent/src/main/resources/profiles/local/pinpoint.config
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ profiler.instrument.matcher.super.cache.entry.size=4
# Lambda expressions.
profiler.lambda.expressions.support=true

# Export trace info to application
#profiler.export.trace.info=false


# Proxy HTTP headers.
# Please see (https://github.com/naver/pinpoint/blob/master/doc/proxy-http-header.md) for more information.
profiler.proxy.http.header.enable=true
Expand Down
3 changes: 3 additions & 0 deletions agent/src/main/resources/profiles/release/pinpoint.config
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ profiler.instrument.matcher.super.cache.entry.size=4
# Lambda expressions.
profiler.lambda.expressions.support=true

# Export trace info to application
#profiler.export.trace.info=false

# Proxy HTTP headers.
# Please see (https://github.com/naver/pinpoint/blob/master/doc/proxy-http-header.md) for more information.
profiler.proxy.http.header.enable=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import com.navercorp.pinpoint.bootstrap.context.SpanRecorder;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.context.TraceId;
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.bootstrap.plugin.http.HttpStatusCodeRecorder;
import com.navercorp.pinpoint.bootstrap.plugin.proxy.ProxyRequestRecorder;
import com.navercorp.pinpoint.bootstrap.plugin.request.method.ServletSyncMethodDescriptor;
import com.navercorp.pinpoint.bootstrap.plugin.request.util.ParameterRecorder;
import com.navercorp.pinpoint.bootstrap.plugin.request.util.TraceInfoExportHelper;
import com.navercorp.pinpoint.bootstrap.plugin.uri.UriStatRecorder;
import com.navercorp.pinpoint.common.trace.ServiceType;

Expand All @@ -42,6 +44,8 @@ public class ServletRequestListener<REQ> {
private final PLogger logger = PLoggerFactory.getLogger(this.getClass());
private final boolean isDebug = logger.isDebugEnabled();
private final boolean isTrace = logger.isTraceEnabled();
private final boolean exportTraceInfo;
private static final String CONFIG_KEY_EXPORT_TRACE = "profiler.export.trace.info";

private final TraceContext traceContext;
private final ServiceType serviceType;
Expand Down Expand Up @@ -79,7 +83,7 @@ public ServletRequestListener(final ServiceType serviceType,
this.proxyRequestRecorder = Objects.requireNonNull(proxyRequestRecorder, "proxyRequestRecorder");

this.excludeUrlFilter = Objects.requireNonNull(excludeUrlFilter, "excludeUrlFilter");

this.parameterRecorder = Objects.requireNonNull(parameterRecorder, "parameterRecorder");


Expand All @@ -91,6 +95,8 @@ public ServletRequestListener(final ServiceType serviceType,

this.recordStatusCode = recordStatusCode;

this.exportTraceInfo = this.traceContext.getProfilerConfig().readBoolean(CONFIG_KEY_EXPORT_TRACE, false);

this.traceContext.cacheApi(SERVLET_SYNC_METHOD_DESCRIPTOR);
}

Expand All @@ -113,6 +119,13 @@ public void initialized(REQ request, final ServiceType serviceType, final Method
return;
}

if (exportTraceInfo) {
TraceId traceId = trace.getTraceId();
if (traceId != null) {
TraceInfoExportHelper.exportTraceInfo(request, traceId.getTransactionId(), traceId.getSpanId());
}
}

final SpanEventRecorder recorder = trace.traceBlockBegin();
recorder.recordServiceType(serviceType);
recorder.recordApi(methodDescriptor);
Expand Down Expand Up @@ -166,6 +179,11 @@ public void destroyed(REQ request, final Throwable throwable, final int statusCo
return;
}

// clear trace info which is put in initialized method
if (exportTraceInfo) {
TraceInfoExportHelper.clearExportedTraceInfo(request);
}

try {
final SpanEventRecorder recorder = trace.currentSpanEventRecorder();
recorder.recordException(throwable);
Expand Down Expand Up @@ -194,4 +212,4 @@ public static boolean isNotFailedStatus(int statusCode) {
return false;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2022 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.navercorp.pinpoint.bootstrap.plugin.request.util;

/**
* @author yjqg6666
*/
@SuppressWarnings("unused")
class ClassLoaderUtils {

public static Class<?> loadClassFromAppObject(Object appLoadedObject, String className) {
ClassLoader appClassLoader = getAppClassLoader(appLoadedObject);
return loadClassFromClassLoader(appClassLoader, className);
}

public static Class<?> loadClassFromClassLoader(ClassLoader appClassLoader, String className) {
if (appClassLoader == null || className == null) {
return null;
}
try {
return Class.forName(className, false, appClassLoader);
} catch (Throwable t) {
//t.printStackTrace();
//do nothing even no logging for no introduced dependency
}
return null;
}

public static ClassLoader getAppClassLoader(Object appLoadedObject) {
if (appLoadedObject == null) {
return null;
}
Class<?> targetClass = appLoadedObject.getClass();
if (targetClass == null) {
return null;
}
return targetClass.getClassLoader();
}
}
Loading

0 comments on commit e7b1184

Please sign in to comment.