Skip to content

Commit

Permalink
[#8780] Apply ContentLength Util
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Apr 18, 2022
1 parent 514dd71 commit 571bf80
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.interceptor.SpanEventSimpleAroundInterceptorForPlugin;
import com.navercorp.pinpoint.common.util.ArrayUtils;
import com.navercorp.pinpoint.common.util.ContentLength;
import com.navercorp.pinpoint.plugin.fastjson.FastjsonConstants;

/**
Expand All @@ -29,6 +30,7 @@
* @since 2017/07/17
*/
public class ParseInterceptor extends SpanEventSimpleAroundInterceptorForPlugin {
private final ContentLength contentLength;

/**
* Instantiates a new Parse interceptor.
Expand All @@ -38,6 +40,14 @@ public class ParseInterceptor extends SpanEventSimpleAroundInterceptorForPlugin
*/
public ParseInterceptor(TraceContext traceContext, MethodDescriptor descriptor) {
super(traceContext, descriptor);
this.contentLength = newContentLength();
}

private ContentLength newContentLength() {
ContentLength.Builder builder = ContentLength.newBuilder();
builder.addContentType(String.class);
builder.addContentType(byte[].class);
return builder.build();
}

@Override
Expand All @@ -51,12 +61,9 @@ protected void doInAfterTrace(SpanEventRecorder recorder, Object target, Object[
recorder.recordException(throwable);

Object arg = ArrayUtils.get(args, 0);
if (arg != null) {
if (arg instanceof String) {
recorder.recordAttribute(FastjsonConstants.ANNOTATION_KEY_JSON_LENGTH, ((String) arg).length());
} else if (arg instanceof byte[]) {
recorder.recordAttribute(FastjsonConstants.ANNOTATION_KEY_JSON_LENGTH, ((byte[]) arg).length);
}
int length = contentLength.getLength(arg);
if (length != ContentLength.NOT_EXIST) {
recorder.recordAttribute(FastjsonConstants.ANNOTATION_KEY_JSON_LENGTH, length);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.common.util.ArrayUtils;
import com.navercorp.pinpoint.common.util.ContentLength;
import com.navercorp.pinpoint.plugin.fastjson.FastjsonConstants;

import java.io.IOException;
import java.io.InputStream;

/**
Expand All @@ -39,6 +39,7 @@ public class ParseObjectInterceptor implements AroundInterceptor {
private final TraceContext traceContext;
private final MethodDescriptor descriptor;
private final PLogger logger = PLoggerFactory.getLogger(getClass());
private final ContentLength contentLength;

/**
* Instantiates a new Parse object interceptor.
Expand All @@ -49,6 +50,16 @@ public class ParseObjectInterceptor implements AroundInterceptor {
public ParseObjectInterceptor(TraceContext traceContext, MethodDescriptor descriptor) {
this.traceContext = traceContext;
this.descriptor = descriptor;
this.contentLength = newContentLength();
}

private ContentLength newContentLength() {
ContentLength.Builder builder = ContentLength.newBuilder();
builder.addContentType(String.class);
builder.addContentType(byte[].class);
builder.addContentType(char[].class);
builder.addContentType(InputStream.class);
return builder.build();
}

@Override
Expand Down Expand Up @@ -89,19 +100,11 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
recorder.recordException(throwable);

Object arg = ArrayUtils.get(args, 0);
if (arg != null) {
if (arg instanceof String) {
recorder.recordAttribute(FastjsonConstants.ANNOTATION_KEY_JSON_LENGTH, ((String) arg).length());
} else if (arg instanceof byte[]) {
recorder.recordAttribute(FastjsonConstants.ANNOTATION_KEY_JSON_LENGTH, ((byte[]) arg).length);
} else if (arg instanceof char[]) {
recorder.recordAttribute(FastjsonConstants.ANNOTATION_KEY_JSON_LENGTH, ((char[]) arg).length);
} else if (arg instanceof InputStream) {
recorder.recordAttribute(FastjsonConstants.ANNOTATION_KEY_JSON_LENGTH, ((InputStream) arg).available());
}
int length = contentLength.getLength(arg);
if (length != ContentLength.NOT_EXIST) {
recorder.recordAttribute(FastjsonConstants.ANNOTATION_KEY_JSON_LENGTH, length);
}
} catch (IOException ignore) {
// ignore

} finally {
trace.traceBlockEnd();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected void doInAfterTrace(SpanEventRecorder recorder, Object target, Object[
recorder.recordApi(methodDescriptor);
recorder.recordException(throwable);

if (result instanceof Object) {
if (result != null) {
recorder.recordAttribute(FastjsonConstants.ANNOTATION_KEY_JSON_LENGTH, result.hashCode());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.interceptor.SpanEventSimpleAroundInterceptorForPlugin;
import com.navercorp.pinpoint.common.util.ArrayUtils;
import com.navercorp.pinpoint.common.util.ContentLength;
import com.navercorp.pinpoint.plugin.jackson.JacksonConstants;

import java.io.File;
Expand All @@ -29,8 +30,18 @@
*/
public class ReadValueInterceptor extends SpanEventSimpleAroundInterceptorForPlugin {

private final ContentLength contentLength;

public ReadValueInterceptor(TraceContext traceContext, MethodDescriptor descriptor) {
super(traceContext, descriptor);
this.contentLength = newContentLength();
}

private ContentLength newContentLength() {
ContentLength.Builder builder = ContentLength.newBuilder();
builder.addContentType(String.class);
builder.addContentType(byte[].class);
return builder.build();
}

@Override
Expand All @@ -44,14 +55,14 @@ protected void doInAfterTrace(SpanEventRecorder recorder, Object target, Object[
recorder.recordException(throwable);

Object arg = ArrayUtils.get(args, 0);

if (arg != null) {
if (arg instanceof String) {
recorder.recordAttribute(JacksonConstants.ANNOTATION_KEY_LENGTH_VALUE, ((String) arg).length());
} else if (arg instanceof byte[]) {
recorder.recordAttribute(JacksonConstants.ANNOTATION_KEY_LENGTH_VALUE, ((byte[]) arg).length);
} else if (arg instanceof File) {
recorder.recordAttribute(JacksonConstants.ANNOTATION_KEY_LENGTH_VALUE, ((File) arg).length());
if (arg instanceof File) {
// long length
long length = ((File) arg).length();
recorder.recordAttribute(JacksonConstants.ANNOTATION_KEY_LENGTH_VALUE, length);
} else {
int length = contentLength.getLength(arg);
if (length != ContentLength.NOT_EXIST) {
recorder.recordAttribute(JacksonConstants.ANNOTATION_KEY_LENGTH_VALUE, length);
}
}
}
Expand Down

0 comments on commit 571bf80

Please sign in to comment.