You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@skywalking.apache.org by wu...@apache.org on 2018/05/21 08:54:17 UTC
[incubator-skywalking] 01/01: Provide runtime context.
This is an automated email from the ASF dual-hosted git repository.
wusheng pushed a commit to branch runtime-context
in repository https://gitbox.apache.org/repos/asf/incubator-skywalking.git
commit e36d0e10818ef4b9a68cc4d1c8cfc250cd12e5f0
Author: wusheng <wu...@foxmail.com>
AuthorDate: Mon May 21 16:54:07 2018 +0800
Provide runtime context.
---
.../agent/core/context/AbstractTracerContext.java | 5 +++
.../apm/agent/core/context/ContextManager.java | 10 ++++-
.../agent/core/context/IgnoredTracerContext.java | 14 +++++++
.../apm/agent/core/context/RuntimeContext.java | 46 ++++++++++++++++++++++
.../apm/agent/core/context/TracingContext.java | 17 +++++++-
5 files changed, 89 insertions(+), 3 deletions(-)
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/AbstractTracerContext.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/AbstractTracerContext.java
index e3ffdac..adc6a3f 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/AbstractTracerContext.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/AbstractTracerContext.java
@@ -104,4 +104,9 @@ public interface AbstractTracerContext {
* @param span to finish
*/
void stopSpan(AbstractSpan span);
+
+ /**
+ * @return the runtime context from current tracing context.
+ */
+ RuntimeContext getRuntimeContext();
}
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java
index 61e1e9d..e1ce828 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java
@@ -194,7 +194,15 @@ public class ContextManager implements TracingContextListener, BootService, Igno
}
public static boolean isActive() {
- return CONTEXT.get() != null;
+ return get() != null;
+ }
+
+ public static RuntimeContext getRuntimeContext() {
+ if (isActive()) {
+ return get().getRuntimeContext();
+ } else {
+ throw new IllegalStateException("No active context");
+ }
}
}
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/IgnoredTracerContext.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/IgnoredTracerContext.java
index fd115e5..a52fd2e 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/IgnoredTracerContext.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/IgnoredTracerContext.java
@@ -37,6 +37,13 @@ public class IgnoredTracerContext implements AbstractTracerContext {
private int stackDepth;
+ /**
+ * Runtime context of the ignored context
+ *
+ * The context should work even no trace, in order to avoid the unexpected status.
+ */
+ private RuntimeContext runtimeContext;
+
public IgnoredTracerContext() {
this.stackDepth = 0;
}
@@ -95,6 +102,13 @@ public class IgnoredTracerContext implements AbstractTracerContext {
}
}
+ @Override public RuntimeContext getRuntimeContext() {
+ if (runtimeContext == null) {
+ runtimeContext = new RuntimeContext();
+ }
+ return runtimeContext;
+ }
+
public static class ListenerManager {
private static List<IgnoreTracerContextListener> LISTENERS = new LinkedList<IgnoreTracerContextListener>();
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/RuntimeContext.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/RuntimeContext.java
new file mode 100644
index 0000000..3bd79fd
--- /dev/null
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/RuntimeContext.java
@@ -0,0 +1,46 @@
+/*
+ * 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.context;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * RuntimeContext is alive during the tracing context.
+ * It will not be serialized to the collector, and always stays in the same context only.
+ *
+ * In most cases, it means it only stays in a single thread for context propagation.
+ *
+ * @author wusheng
+ */
+public class RuntimeContext {
+ private Map context = new ConcurrentHashMap(0);
+
+ public void put(Object key, Object value) {
+ context.put(key, value);
+ }
+
+ public Object get(Object key) {
+ return context.get(key);
+ }
+
+ public <T> T get(Object key, Class<T> type) {
+ return (T)context.get(key);
+ }
+}
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java
index 1bff777..e3d4f7c 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java
@@ -80,6 +80,11 @@ public class TracingContext implements AbstractTracerContext {
private int spanIdGenerator;
/**
+ * Runtime context of the tracing context
+ */
+ private RuntimeContext runtimeContext;
+
+ /**
* Initialize all fields with default value.
*/
TracingContext() {
@@ -94,7 +99,7 @@ public class TracingContext implements AbstractTracerContext {
* Inject the context into the given carrier, only when the active span is an exit one.
*
* @param carrier to carry the context for crossing process.
- * @throws IllegalStateException if the active span isn't an exit one.
+ * @throws IllegalStateException if the active span isn't an exit one.
* Ref to {@link AbstractTracerContext#inject(ContextCarrier)}
*/
@Override
@@ -316,7 +321,7 @@ public class TracingContext implements AbstractTracerContext {
* @param operationName most likely a service name of remote
* @param remotePeer the network id(ip:port, hostname:port or ip1:port1,ip2,port, etc.)
* @return the span represent an exit point of this segment.
- * @see ExitSpan
+ * @see ExitSpan
*/
@Override
public AbstractSpan createExitSpan(final String operationName, final String remotePeer) {
@@ -419,6 +424,14 @@ public class TracingContext implements AbstractTracerContext {
}
}
+ @Override
+ public RuntimeContext getRuntimeContext() {
+ if (runtimeContext == null) {
+ runtimeContext = new RuntimeContext();
+ }
+ return runtimeContext;
+ }
+
/**
* Finish this context, and notify all {@link TracingContextListener}s, managed by {@link
* TracingContext.ListenerManager}
--
To stop receiving notification emails like this one, please contact
wusheng@apache.org.