Skip to content

Commit

Permalink
[#4271] Refactor ClientRequestRecorder
Browse files Browse the repository at this point in the history
 - Add ClientRequestAdaptor
 - Extract CookieRecorder
 - Extract EntityRecorder
  • Loading branch information
emeroad committed Jul 25, 2018
1 parent 1050883 commit bae3a4d
Show file tree
Hide file tree
Showing 53 changed files with 1,355 additions and 501 deletions.
Expand Up @@ -20,5 +20,5 @@
* @author Woonduk Kang(emeroad)
*/
public interface ClientHeaderAdaptor<T> {
void setHeader(T request, String name, String value);
void setHeader(T header, String name, String value);
}
@@ -0,0 +1,38 @@
/*
* Copyright 2018 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;

/**
* @author Woonduk Kang(emeroad)
*/
public interface ClientRequestAdaptor<T> {

/**
* The DestinationId is logical name of the destination.
* <p>
*
* @return If the value does not exist, it should return "Unknown".
*/
String getDestinationId(T request);

/**
* URL
*
* @return If the value does not exist, it should return null.
*/
String getUrl(T request);
}
Expand Up @@ -5,7 +5,7 @@
* 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
* 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,
Expand All @@ -16,43 +16,41 @@

package com.navercorp.pinpoint.bootstrap.plugin.request;

import com.navercorp.pinpoint.bootstrap.config.DumpType;
import com.navercorp.pinpoint.bootstrap.config.HttpDumpConfig;
import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder;
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.bootstrap.util.InterceptorUtils;
import com.navercorp.pinpoint.common.trace.AnnotationKey;
import com.navercorp.pinpoint.common.util.StringUtils;
import com.navercorp.pinpoint.common.util.Assert;

/**
* @author jaehong.kim
*/
public class ClientRequestRecorder {
public class ClientRequestRecorder<T> {
private static final String DEFAULT_DESTINATION_ID = "Unknown";

private final PLogger logger = PLoggerFactory.getLogger(this.getClass());
private final boolean isDebug = logger.isDebugEnabled();

private final boolean param;
private final HttpDumpConfig httpDumpConfig;
private final ClientRequestAdaptor<T> clientRequestAdaptor;

public ClientRequestRecorder(final boolean param, final HttpDumpConfig httpDumpConfig) {
public ClientRequestRecorder(final boolean param, ClientRequestAdaptor<T> clientRequestAdaptor) {
this.param = param;
this.httpDumpConfig = httpDumpConfig;
this.clientRequestAdaptor = Assert.requireNonNull(clientRequestAdaptor, "clientRequestAdaptor must not be null");
}

// Records the client's request information.
public void record(final SpanEventRecorder recorder, final ClientRequestWrapper clientRequestWrapper, final Throwable throwable) {
if (recorder == null || clientRequestWrapper == null) {
public void record(final SpanEventRecorder recorder, final T clientRequest, final Throwable throwable) {
if (recorder == null || clientRequest == null) {
return;
}

final String destinationId = clientRequestWrapper.getDestinationId();
final String destinationId = clientRequestAdaptor.getDestinationId(clientRequest);
if (destinationId != null) {
recorder.recordDestinationId(destinationId);
if (isDebug) {
logger.debug("Record destinationId={}", clientRequestWrapper.getDestinationId());
logger.debug("Record destinationId={}", destinationId);
}
} else {
// Set default value
Expand All @@ -62,54 +60,18 @@ public void record(final SpanEventRecorder recorder, final ClientRequestWrapper
}
}

final String url = clientRequestWrapper.getUrl();
final String url = clientRequestAdaptor.getUrl(clientRequest);
if (url != null) {
final String httpUrl = InterceptorUtils.getHttpUrl(clientRequestWrapper.getUrl(), this.param);
final String httpUrl = InterceptorUtils.getHttpUrl(url, this.param);
recorder.recordAttribute(AnnotationKey.HTTP_URL, httpUrl);
if (isDebug) {
logger.debug("Record url={}", httpUrl);
}
}

final boolean isException = InterceptorUtils.isThrowable(throwable);
if (this.httpDumpConfig.isDumpCookie()) {
if (DumpType.ALWAYS == this.httpDumpConfig.getCookieDumpType()) {
recordCookie(recorder, clientRequestWrapper);
} else if (DumpType.EXCEPTION == this.httpDumpConfig.getCookieDumpType() && isException) {
recordCookie(recorder, clientRequestWrapper);
}
}

if (this.httpDumpConfig.isDumpEntity()) {
if (DumpType.ALWAYS == this.httpDumpConfig.getEntityDumpType()) {
recordEntity(recorder, clientRequestWrapper);
} else if (DumpType.EXCEPTION == this.httpDumpConfig.getEntityDumpType() && isException) {
recordEntity(recorder, clientRequestWrapper);
}
}
}

private void recordCookie(final SpanEventRecorder recorder, final ClientRequestWrapper clientRequestWrapper) {
if (this.httpDumpConfig.getCookieSampler().isSampling()) {
final String cookieValue = clientRequestWrapper.getCookieValue();
if (cookieValue != null) {
recorder.recordAttribute(AnnotationKey.HTTP_COOKIE, StringUtils.abbreviate(cookieValue, this.httpDumpConfig.getCookieDumpSize()));
if (isDebug) {
logger.debug("Record cookie={}", cookieValue);
}
}
}
}

private void recordEntity(final SpanEventRecorder recorder, final ClientRequestWrapper clientRequestWrapper) {
if (this.httpDumpConfig.getEntitySampler().isSampling()) {
final String entityValue = clientRequestWrapper.getEntityValue();
if (entityValue != null) {
recorder.recordAttribute(AnnotationKey.HTTP_PARAM_ENTITY, StringUtils.abbreviate(entityValue, this.httpDumpConfig.getEntityDumpSize()));
if (isDebug) {
logger.debug("Record entity={}", entityValue);
}
}
}
}

}
Expand Up @@ -36,17 +36,4 @@ public interface ClientRequestWrapper {
*/
String getUrl();

/**
* Entity
*
* @return If the value does not exist, it should return null.
*/
String getEntityValue();

/**
* Cookie
*
* @return If the value does not exist, it should return null.
*/
String getCookieValue();
}
@@ -0,0 +1,35 @@
/*
* Copyright 2018 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;

/**
* @author Woonduk Kang(emeroad)
*/
public class ClientRequestWrapperAdaptor implements ClientRequestAdaptor<ClientRequestWrapper> {

public static final ClientRequestAdaptor<ClientRequestWrapper> INSTANCE = new ClientRequestWrapperAdaptor();

@Override
public String getDestinationId(ClientRequestWrapper request) {
return request.getDestinationId();
}

@Override
public String getUrl(ClientRequestWrapper request) {
return request.getUrl();
}
}
Expand Up @@ -16,6 +16,8 @@

package com.navercorp.pinpoint.bootstrap.plugin.request;

import com.navercorp.pinpoint.bootstrap.plugin.request.TraceHeader;
import com.navercorp.pinpoint.bootstrap.plugin.request.TraceHeaderState;
import com.navercorp.pinpoint.common.util.Assert;

/**
Expand Down
Expand Up @@ -142,7 +142,7 @@ public void destroyed(T request, final Throwable throwable, final int statusCode
recorder.recordException(throwable);
this.httpStatusCodeRecorder.record(trace.getSpanRecorder(), statusCode);
// Must be executed in destroyed()
this.parameterRecorder.recordParameter(recorder, request);
this.parameterRecorder.record(recorder, request, throwable);
} finally {
trace.traceBlockEnd();
this.traceContext.removeTraceObject();
Expand Down
@@ -0,0 +1,29 @@
/*
* Copyright 2018 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 Woonduk Kang(emeroad)
*/
public interface CookieExtractor<T> {
/**
* Cookie
*
* @return If the value does not exist, it should return null.
*/
String getCookie(T request);
}
@@ -0,0 +1,26 @@
/*
* Copyright 2018 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;

import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder;

/**
* @author Woonduk Kang(emeroad)
*/
public interface CookieRecorder<T> {
void record(final SpanEventRecorder recorder, final T cookie, final Throwable throwable);
}
@@ -0,0 +1,36 @@
/*
* Copyright 2018 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;

import com.navercorp.pinpoint.bootstrap.config.HttpDumpConfig;
import com.navercorp.pinpoint.common.util.Assert;

/**
* @author Woonduk Kang(emeroad)
*/
public class CookieRecorderFactory {


public static <T> CookieRecorder<T> newCookieRecorder(HttpDumpConfig httpDumpConfig, CookieExtractor<T> extractor) {
Assert.requireNonNull(httpDumpConfig, "httpDumpConfig must not be null");

if (!httpDumpConfig.isDumpCookie()) {
return new DisableCookieRecorder<T>();
}
return new DefaultCookieRecorder<T>(httpDumpConfig, extractor);
}
}

0 comments on commit bae3a4d

Please sign in to comment.