Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #10377] Migrating Client OpenTelemetry trace from dynamic proxies to delegate classes #11559

Open
wants to merge 5 commits into
base: summer-ospp#10377
Choose a base branch
from
Open
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 @@ -27,21 +27,20 @@
import com.alibaba.nacos.client.config.filter.impl.ConfigRequest;
import com.alibaba.nacos.client.config.filter.impl.ConfigResponse;
import com.alibaba.nacos.client.config.http.HttpAgent;
import com.alibaba.nacos.client.config.http.ServerHttpAgent;
import com.alibaba.nacos.client.config.impl.ClientWorker;
import com.alibaba.nacos.client.config.impl.LocalConfigInfoProcessor;
import com.alibaba.nacos.client.config.impl.LocalEncryptedDataKeyProcessor;
import com.alibaba.nacos.client.config.impl.ServerListManager;
import com.alibaba.nacos.client.config.utils.ContentUtils;
import com.alibaba.nacos.client.config.utils.ParamUtils;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.monitor.TraceDynamicProxy;
import com.alibaba.nacos.client.monitor.config.ClientWorkerTraceProxy;
import com.alibaba.nacos.common.constant.NacosSemanticAttributes;
import com.alibaba.nacos.client.monitor.config.ConfigTrace;
import com.alibaba.nacos.client.monitor.delegate.config.ClientWorkerTraceDelegate;
import com.alibaba.nacos.client.monitor.delegate.config.ServerHttpAgentTraceDelegate;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.ValidatorUtils;
import com.alibaba.nacos.common.constant.NacosSemanticAttributes;
import com.alibaba.nacos.common.utils.StringUtils;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanBuilder;
Expand Down Expand Up @@ -76,7 +75,7 @@ public class NacosConfigService implements ConfigService {
/**
* long polling.
*/
private final ClientWorkerTraceProxy worker;
private final ClientWorker worker;

private String namespace;

Expand All @@ -91,10 +90,9 @@ public NacosConfigService(Properties properties) throws NacosException {
ServerListManager serverListManager = new ServerListManager(clientProperties);
serverListManager.start();

this.worker = TraceDynamicProxy.getClientWorkerTraceProxy(
new ClientWorker(this.configFilterChainManager, serverListManager, clientProperties));
this.worker = new ClientWorkerTraceDelegate(this.configFilterChainManager, serverListManager, clientProperties);
// will be deleted in 2.0 later versions
agent = TraceDynamicProxy.getHttpAgentTraceProxy(new ServerHttpAgent(serverListManager));
agent = new ServerHttpAgentTraceDelegate(serverListManager);

}

Expand Down Expand Up @@ -224,7 +222,7 @@ private String getConfigInner(String tenant, String dataId, String group, long t
cr.setContent(content);

String encryptedDataKey = getSnapshotWithTrace(agent.getName(), dataId, group, tenant, true);

cr.setEncryptedDataKey(encryptedDataKey);
configFilterChainManager.doFilter(null, cr);
content = cr.getContent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private String getUrl(String serverAddr, String relativePath) {
return serverAddr + ContextPathUtil.normalizeContextPath(serverListMgr.getContentPath()) + relativePath;
}

private boolean isFail(HttpRestResult<String> result) {
protected boolean isFail(HttpRestResult<String> result) {
return result.getCode() == HttpURLConnection.HTTP_INTERNAL_ERROR
|| result.getCode() == HttpURLConnection.HTTP_BAD_GATEWAY
|| result.getCode() == HttpURLConnection.HTTP_UNAVAILABLE
Expand Down

Large diffs are not rendered by default.

1,151 changes: 0 additions & 1,151 deletions client/src/main/java/com/alibaba/nacos/client/monitor/TraceDynamicProxy.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* 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.alibaba.nacos.client.monitor.delegate;

import com.alibaba.nacos.api.config.remote.request.ClientConfigMetricRequest;
import com.alibaba.nacos.api.config.remote.request.ConfigChangeNotifyRequest;
import com.alibaba.nacos.api.naming.remote.request.NotifySubscriberRequest;
import com.alibaba.nacos.client.monitor.TraceMonitor;
import com.alibaba.nacos.common.constant.NacosSemanticAttributes;
import com.alibaba.nacos.common.remote.client.ServerRequestHandler;
import com.alibaba.nacos.common.utils.VersionUtils;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.context.Scope;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;

/**
* Wrap ServerRequestHandler for tracing.
*
* @author <a href="https://github.com/FAWC438">FAWC438</a>
*/
public class ServerRequestHandlerTraceDelegate {

/**
* Wrap ServerRequestHandler for tracing.
*
* @param handler ServerRequestHandler
* @return ServerRequestHandler
*/
public static ServerRequestHandler warp(ServerRequestHandler handler) {
return (request) -> {
String methodName = "requestReply";
String moduleName = request.getModule();

String spanName = TraceMonitor.getNacosClientRequestFromServerSpanName() + " / " + moduleName;
SpanBuilder spanBuilder = TraceMonitor.getTracer().spanBuilder(spanName);
// SpanKind.SERVER means incoming span rather than the span in the server side.
// See https://opentelemetry.io/docs/specs/otel/trace/api/#spankind
spanBuilder.setSpanKind(SpanKind.SERVER);
spanBuilder.setAttribute(NacosSemanticAttributes.CLIENT_VERSION, VersionUtils.getFullClientVersion());
spanBuilder.setAttribute(SemanticAttributes.CODE_NAMESPACE, handler.getClass().getName());
spanBuilder.setAttribute(SemanticAttributes.CODE_FUNCTION, methodName);
spanBuilder.setAttribute(NacosSemanticAttributes.RequestAttributes.REQUEST_ID, request.getRequestId());
spanBuilder.setAttribute(NacosSemanticAttributes.RequestAttributes.REQUEST_MODULE, moduleName);

Span span = spanBuilder.startSpan();
try (Scope ignored = span.makeCurrent()) {

if (span.isRecording()) {
if (request instanceof ConfigChangeNotifyRequest) {
ConfigChangeNotifyRequest configChangeNotifyRequest = (ConfigChangeNotifyRequest) request;
span.setAttribute(NacosSemanticAttributes.RequestAttributes.REQUEST_ID,
configChangeNotifyRequest.getRequestId());
span.setAttribute(NacosSemanticAttributes.RequestAttributes.REQUEST_DATA_ID,
configChangeNotifyRequest.getDataId());
span.setAttribute(NacosSemanticAttributes.RequestAttributes.REQUEST_GROUP,
configChangeNotifyRequest.getGroup());
span.setAttribute(NacosSemanticAttributes.RequestAttributes.REQUEST_TENANT,
configChangeNotifyRequest.getTenant());
span.setAttribute(NacosSemanticAttributes.RequestAttributes.REQUEST_TYPE,
ConfigChangeNotifyRequest.class.getSimpleName());

} else if (request instanceof ClientConfigMetricRequest) {
span.setAttribute(NacosSemanticAttributes.RequestAttributes.REQUEST_TYPE,
ClientConfigMetricRequest.class.getSimpleName());

} else if (request instanceof NotifySubscriberRequest) {
NotifySubscriberRequest notifySubscriberRequest = (NotifySubscriberRequest) request;
span.setAttribute(NacosSemanticAttributes.RequestAttributes.REQUEST_NAMESPACE,
notifySubscriberRequest.getNamespace());
span.setAttribute(NacosSemanticAttributes.RequestAttributes.REQUEST_SERVICE_NAME,
notifySubscriberRequest.getServiceName());
if (notifySubscriberRequest.getServiceInfo() != null) {
span.setAttribute(NacosSemanticAttributes.RequestAttributes.REQUEST_GROUP,
notifySubscriberRequest.getServiceInfo().getGroupName());
span.setAttribute(NacosSemanticAttributes.RequestAttributes.REQUEST_SERVICE_CLUSTER_NAME,
notifySubscriberRequest.getServiceInfo().getClusters());
}
span.setAttribute(NacosSemanticAttributes.RequestAttributes.REQUEST_TYPE,
NotifySubscriberRequest.class.getSimpleName());

}
// More request type can be added here.
}

return handler.requestReply(request);

} catch (Exception e) {
span.recordException(e);
span.setStatus(StatusCode.ERROR, e.getClass().getSimpleName());
throw e;
} finally {
span.end();
}

};
}
}
Loading