You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2019/08/02 09:07:39 UTC

[skywalking] branch master updated: Add agent version in gRPC connection establish stage (#3205)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking.git


The following commit(s) were added to refs/heads/master by this push:
     new ff0ffb9  Add agent version in gRPC connection establish stage (#3205)
ff0ffb9 is described below

commit ff0ffb99ca3c97862637d14c39738aa0e7b7c75d
Author: 吴晟 Wu Sheng <wu...@foxmail.com>
AuthorDate: Fri Aug 2 17:07:32 2019 +0800

    Add agent version in gRPC connection establish stage (#3205)
---
 .../apm/agent/core/remote/AgentIDDecorator.java    | 87 ++++++++++++++++++++++
 .../apm/agent/core/remote/GRPCChannelManager.java  | 22 ++++--
 2 files changed, 104 insertions(+), 5 deletions(-)

diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/AgentIDDecorator.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/AgentIDDecorator.java
new file mode 100644
index 0000000..a0ccf84
--- /dev/null
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/AgentIDDecorator.java
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.skywalking.apm.agent.core.remote;
+
+import io.grpc.CallOptions;
+import io.grpc.Channel;
+import io.grpc.ClientCall;
+import io.grpc.ClientInterceptor;
+import io.grpc.ClientInterceptors;
+import io.grpc.ForwardingClientCall;
+import io.grpc.Metadata;
+import io.grpc.MethodDescriptor;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+import org.apache.skywalking.apm.agent.core.logging.api.ILog;
+import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
+
+/**
+ * Add agent version(Described in MANIFEST.MF) to the connection establish stage.
+ *
+ * @author wusheng
+ */
+public class AgentIDDecorator implements ChannelDecorator {
+    private static final ILog logger = LogManager.getLogger(AgentIDDecorator.class);
+    private static final Metadata.Key<String> AGENT_VERSION_HEAD_HEADER_NAME =
+        Metadata.Key.of("Agent-Version", Metadata.ASCII_STRING_MARSHALLER);
+    private String version = "UNKNOWN";
+
+    public AgentIDDecorator() {
+        try {
+            Enumeration<URL> resources = AgentIDDecorator.class.getClassLoader().getResources(JarFile.MANIFEST_NAME);
+            while (resources.hasMoreElements()) {
+                URL url = resources.nextElement();
+                InputStream is = url.openStream();
+                if (is != null) {
+                    Manifest manifest = new Manifest(is);
+                    Attributes mainAttribs = manifest.getMainAttributes();
+                    String projectName = mainAttribs.getValue("Implementation-Vendor-Id");
+                    if (projectName != null) {
+                        if (projectName.equals("org.apache.skywalking")) {
+                            version = mainAttribs.getValue("Implementation-Version");
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            logger.warn("Can't read version from MANIFEST.MF in the agent jar");
+        }
+    }
+
+    @Override public Channel build(Channel channel) {
+        return ClientInterceptors.intercept(channel, new ClientInterceptor() {
+            @Override
+            public <REQ, RESP> ClientCall<REQ, RESP> interceptCall(MethodDescriptor<REQ, RESP> method,
+                CallOptions options, Channel channel) {
+                return new ForwardingClientCall.SimpleForwardingClientCall<REQ, RESP>(channel.newCall(method, options)) {
+                    @Override
+                    public void start(Listener<RESP> responseListener, Metadata headers) {
+                        headers.put(AGENT_VERSION_HEAD_HEADER_NAME, version);
+
+                        super.start(responseListener, headers);
+                    }
+                };
+            }
+        });
+    }
+}
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/GRPCChannelManager.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/GRPCChannelManager.java
index f9213dd..952a115 100755
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/GRPCChannelManager.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/GRPCChannelManager.java
@@ -18,12 +18,23 @@
 
 package org.apache.skywalking.apm.agent.core.remote;
 
-import io.grpc.*;
-import java.util.*;
-import java.util.concurrent.*;
-import org.apache.skywalking.apm.agent.core.boot.*;
+import io.grpc.Channel;
+import io.grpc.Status;
+import io.grpc.StatusRuntimeException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import org.apache.skywalking.apm.agent.core.boot.BootService;
+import org.apache.skywalking.apm.agent.core.boot.DefaultImplementor;
+import org.apache.skywalking.apm.agent.core.boot.DefaultNamedThreadFactory;
 import org.apache.skywalking.apm.agent.core.conf.Config;
-import org.apache.skywalking.apm.agent.core.logging.api.*;
+import org.apache.skywalking.apm.agent.core.logging.api.ILog;
+import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
 import org.apache.skywalking.apm.util.RunnableWithExceptionProtection;
 
 /**
@@ -101,6 +112,7 @@ public class GRPCChannelManager implements BootService, Runnable {
                         managedChannel = GRPCChannel.newBuilder(ipAndPort[0], Integer.parseInt(ipAndPort[1]))
                             .addManagedChannelBuilder(new StandardChannelBuilder())
                             .addManagedChannelBuilder(new TLSChannelBuilder())
+                            .addChannelDecorator(new AgentIDDecorator())
                             .addChannelDecorator(new AuthenticationDecorator())
                             .build();