Skip to content
Merged
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 @@ -42,9 +42,11 @@ public class ContextCarrier implements Serializable {
private String addressUsedAtClient;

private CorrelationContext correlationContext = new CorrelationContext();
private ExtensionContext extensionContext = new ExtensionContext();

public CarrierItem items() {
SW8CorrelationCarrierItem sw8CorrelationCarrierItem = new SW8CorrelationCarrierItem(correlationContext, null);
SW8ExtensionCarrierItem sw8ExtensionCarrierItem = new SW8ExtensionCarrierItem(extensionContext, null);
SW8CorrelationCarrierItem sw8CorrelationCarrierItem = new SW8CorrelationCarrierItem(correlationContext, sw8ExtensionCarrierItem);
SW8CarrierItem sw8CarrierItem = new SW8CarrierItem(this, sw8CorrelationCarrierItem);
return new CarrierItemHead(sw8CarrierItem);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@ public class ContextSnapshot {
private String parentEndpoint;

private CorrelationContext correlationContext;
private ExtensionContext extensionContext;

ContextSnapshot(String traceSegmentId,
int spanId,
DistributedTraceId primaryTraceId,
String parentEndpoint,
CorrelationContext correlationContext) {
CorrelationContext correlationContext,
ExtensionContext extensionContext) {
this.traceSegmentId = traceSegmentId;
this.spanId = spanId;
this.traceId = primaryTraceId;
this.parentEndpoint = parentEndpoint;
this.correlationContext = correlationContext.clone();
this.extensionContext = extensionContext.clone();
}

public boolean isFromCurrent() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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 org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;

import java.util.Objects;

/**
* Extension context, It provides the interaction capabilities between the agents
* deployed in upstream and downstream services.
*/
public class ExtensionContext {

/**
* Tracing Mode. If true means represents all spans generated in this context should skip analysis.
*/
private boolean skipAnalysis;

/**
* Serialize this {@link ExtensionContext} to a {@link String}
*
* @return the serialization string.
*/
String serialize() {
return skipAnalysis ? "1" : "0";
}

/**
* Deserialize data from {@link String}
*/
void deserialize(String value) {
this.skipAnalysis = Objects.equals(value, "1");
}

/**
* Prepare for the cross-process propagation.
*/
void inject(ContextCarrier carrier) {
carrier.getExtensionContext().skipAnalysis = this.skipAnalysis;
}

/**
* Extra the {@link ContextCarrier#getExtensionContext()} into this context.
*/
void extract(ContextCarrier carrier) {
this.skipAnalysis = carrier.getExtensionContext().skipAnalysis;
}

/**
* Handle the tracing span.
*/
void handle(AbstractSpan span) {
if (this.skipAnalysis) {
span.skipAnalysis();
}
}

/**
* Clone the context data, work for capture to cross-thread.
*/
public ExtensionContext clone() {
final ExtensionContext context = new ExtensionContext();
context.skipAnalysis = this.skipAnalysis;
return context;
}

void continued(ContextSnapshot snapshot) {
this.skipAnalysis = snapshot.getExtensionContext().skipAnalysis;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ExtensionContext that = (ExtensionContext) o;
return skipAnalysis == that.skipAnalysis;
}

@Override
public int hashCode() {
return Objects.hash(skipAnalysis);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ public class IgnoredTracerContext implements AbstractTracerContext {
private static final NoopSpan NOOP_SPAN = new NoopSpan();

private final CorrelationContext correlationContext;
private final ExtensionContext extensionContext;

private int stackDepth;

public IgnoredTracerContext() {
this.stackDepth = 0;
this.correlationContext = new CorrelationContext();
this.extensionContext = new ExtensionContext();
}

@Override
Expand All @@ -53,7 +55,7 @@ public void extract(ContextCarrier carrier) {

@Override
public ContextSnapshot capture() {
return new ContextSnapshot(null, -1, null, null, correlationContext);
return new ContextSnapshot(null, -1, null, null, correlationContext, extensionContext);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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;

public class SW8ExtensionCarrierItem extends CarrierItem {
public static final String HEADER_NAME = "sw8-x";
private final ExtensionContext extensionContext;

public SW8ExtensionCarrierItem(ExtensionContext extensionContext, CarrierItem next) {
super(HEADER_NAME, extensionContext.serialize(), next);
this.extensionContext = extensionContext;
}

@Override
public void setHeadValue(String headValue) {
this.extensionContext.deserialize(headValue);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public class TracingContext implements AbstractTracerContext {
private final ProfileStatusReference profileStatus;

private final CorrelationContext correlationContext;
private final ExtensionContext extensionContext;

/**
* Initialize all fields with default value.
Expand All @@ -134,6 +135,7 @@ public class TracingContext implements AbstractTracerContext {
this, segment.getTraceSegmentId(), firstOPName);

this.correlationContext = new CorrelationContext();
this.extensionContext = new ExtensionContext();
}

/**
Expand Down Expand Up @@ -177,6 +179,7 @@ public void inject(AbstractSpan exitSpan, ContextCarrier carrier) {
carrier.setAddressUsedAtClient(peer);

this.correlationContext.inject(carrier);
this.extensionContext.inject(carrier);
}

/**
Expand All @@ -195,6 +198,8 @@ public void extract(ContextCarrier carrier) {
}

this.correlationContext.extract(carrier);
this.extensionContext.extract(carrier);
this.extensionContext.handle(span);
}

/**
Expand All @@ -209,7 +214,8 @@ public ContextSnapshot capture() {
activeSpan().getSpanId(),
getPrimaryTraceId(),
first().getOperationName(),
this.correlationContext
this.correlationContext,
this.extensionContext
);

return snapshot;
Expand All @@ -228,6 +234,8 @@ public void continued(ContextSnapshot snapshot) {
this.activeSpan().ref(segmentRef);
this.segment.relatedGlobalTraces(snapshot.getTraceId());
this.correlationContext.continued(snapshot);
this.extensionContext.continued(snapshot);
this.extensionContext.handle(this.activeSpan());
}
}

Expand Down Expand Up @@ -529,6 +537,7 @@ private AbstractSpan push(AbstractSpan span) {
firstSpan = span;
}
activeSpanStack.addLast(span);
this.extensionContext.handle(span);
return span;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,9 @@ public interface AbstractSpan extends AsyncSpan {
* @return true if the span's owner(tracing context main thread) is been profiled.
*/
boolean isProfiling();

/**
* Should skip analysis in the backend.
*/
void skipAnalysis();
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public abstract class AbstractTracingSpan implements AbstractSpan {
*/
protected List<TraceSegmentRef> refs;

/**
* Tracing Mode. If true means represents all spans generated in this context should skip analysis.
*/
protected boolean skipAnalysis;

protected AbstractTracingSpan(int spanId, int parentSpanId, String operationName, TracingContext owner) {
this.operationName = operationName;
this.spanId = spanId;
Expand Down Expand Up @@ -246,6 +251,7 @@ public SpanObject.Builder transform() {
spanBuilder.setStartTime(startTime);
spanBuilder.setEndTime(endTime);
spanBuilder.setOperationName(operationName);
spanBuilder.setSkipAnalysis(skipAnalysis);
if (isEntry()) {
spanBuilder.setSpanType(SpanType.Entry);
} else if (isExit()) {
Expand Down Expand Up @@ -317,4 +323,9 @@ public AbstractSpan asyncFinish() {
public boolean isProfiling() {
return this.owner.profileStatus().isProfiling();
}

@Override
public void skipAnalysis() {
this.skipAnalysis = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public boolean isProfiling() {
return false;
}

@Override
public void skipAnalysis() {
}

@Override
public AbstractSpan prepareForAsync() {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public void testDeserializeV3Header() {
next.setHeadValue("1-My40LjU=-MS4yLjM=-4-c2VydmljZQ==-aW5zdGFuY2U=-L2FwcA==-MTI3LjAuMC4xOjgwODA=");
} else if (next.getHeadKey().equals(SW8CorrelationCarrierItem.HEADER_NAME)) {
next.setHeadValue("dGVzdA==:dHJ1ZQ==");
} else if (next.getHeadKey().equals(SW8ExtensionCarrierItem.HEADER_NAME)) {
next.setHeadValue("1");
} else {
throw new IllegalArgumentException("Unknown Header: " + next.getHeadKey());
}
Expand All @@ -61,6 +63,8 @@ public void testSerializeV3Header() {

contextCarrier.getCorrelationContext().put("test", "true");

contextCarrier.getExtensionContext().deserialize("1");

CarrierItem next = contextCarrier.items();
while (next.hasNext()) {
next = next.next();
Expand All @@ -73,6 +77,8 @@ public void testSerializeV3Header() {
* "test:true"
*/
Assert.assertEquals("dGVzdA==:dHJ1ZQ==", next.getHeadValue());
} else if (next.getHeadKey().equals(SW8ExtensionCarrierItem.HEADER_NAME)) {
Assert.assertEquals("1", next.getHeadValue());
} else {
throw new IllegalArgumentException("Unknown Header: " + next.getHeadKey());
}
Expand All @@ -85,6 +91,8 @@ public void testSerializeV3Header() {
Assert.assertEquals("1-My40LjU=-MS4yLjM=-4-c2VydmljZQ==-aW5zdGFuY2U=-L2FwcA==-MTI3LjAuMC4xOjgwODA=", next.getHeadValue());
} else if (next.getHeadKey().equals(SW8CorrelationCarrierItem.HEADER_NAME)) {
Assert.assertEquals("dGVzdA==:dHJ1ZQ==", next.getHeadValue());
} else if (next.getHeadKey().equals(SW8ExtensionCarrierItem.HEADER_NAME)) {
Assert.assertEquals("1", next.getHeadValue());
} else {
throw new IllegalArgumentException("Unknown Header: " + next.getHeadKey());
}
Expand All @@ -109,16 +117,20 @@ public void testV2HeaderAccurate() {
contextCarrier.setParentEndpoint("/app");

contextCarrier.getCorrelationContext().put("test", "true");
contextCarrier.getExtensionContext().deserialize("1");

CarrierItem next = contextCarrier.items();
String sw6HeaderValue = null;
String correlationHeaderValue = null;
String extensionHeaderValue = null;
while (next.hasNext()) {
next = next.next();
if (next.getHeadKey().equals(SW8CarrierItem.HEADER_NAME)) {
sw6HeaderValue = next.getHeadValue();
} else if (next.getHeadKey().equals(SW8CorrelationCarrierItem.HEADER_NAME)) {
correlationHeaderValue = next.getHeadValue();
} else if (next.getHeadKey().equals(SW8ExtensionCarrierItem.HEADER_NAME)) {
extensionHeaderValue = next.getHeadValue();
} else {
throw new IllegalArgumentException("Unknown Header: " + next.getHeadKey());
}
Expand All @@ -132,6 +144,8 @@ public void testV2HeaderAccurate() {
next.setHeadValue(sw6HeaderValue);
} else if (next.getHeadKey().equals(SW8CorrelationCarrierItem.HEADER_NAME)) {
next.setHeadValue(correlationHeaderValue);
} else if (next.getHeadKey().equals(SW8ExtensionCarrierItem.HEADER_NAME)) {
next.setHeadValue(extensionHeaderValue);
} else {
throw new IllegalArgumentException("Unknown Header: " + next.getHeadKey());
}
Expand All @@ -146,5 +160,6 @@ public void testV2HeaderAccurate() {
Assert.assertEquals(contextCarrier.getParentServiceInstance(), contextCarrier2.getParentServiceInstance());
Assert.assertEquals(contextCarrier.getParentEndpoint(), contextCarrier2.getParentEndpoint());
Assert.assertEquals(contextCarrier.getCorrelationContext(), contextCarrier2.getCorrelationContext());
Assert.assertEquals(contextCarrier.getExtensionContext(), contextCarrier2.getExtensionContext());
}
}
Loading