org.cattleframework.cloud.strategy.skywalking.SkyWalkingStrategySpan Maven / Gradle / Ivy
/*
* Copyright (C) 2018 the original author or authors.
*
* 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 org.cattleframework.cloud.strategy.skywalking;
import java.util.Map;
import org.apache.skywalking.apm.toolkit.trace.TraceContext;
import org.cattleframework.cloud.strategy.constants.StrategyConstants;
import org.cattleframework.cloud.strategy.monitor.StrategySpan;
import org.cattleframework.cloud.strategy.monitor.StrategyTracerContext;
import io.opentracing.Span;
import io.opentracing.SpanContext;
/**
* SkyWalking策略跨度
*
* @author orange
*
*/
public class SkyWalkingStrategySpan implements Span {
private Span span;
private String traceId;
private String spanId;
public SkyWalkingStrategySpan(Span span) {
this.span = span;
this.traceId = createTraceId();
this.spanId = createSpanId();
}
@Override
public SpanContext context() {
return span.context();
}
@Override
public Span setTag(String key, String value) {
return span.setTag(key, value);
}
@Override
public Span setTag(String key, boolean value) {
return span.setTag(key, value);
}
@Override
public Span setTag(String key, Number value) {
return span.setTag(key, value);
}
@Override
public Span log(Map fields) {
return span.log(fields);
}
@Override
public Span log(long timestampMicroseconds, Map fields) {
return span.log(timestampMicroseconds, fields);
}
@Override
public Span log(String event) {
return span.log(event);
}
@Override
public Span log(long timestampMicroseconds, String event) {
return span.log(timestampMicroseconds, event);
}
@Override
public Span setBaggageItem(String key, String value) {
return span.setBaggageItem(key, value);
}
@Override
public String getBaggageItem(String key) {
return span.getBaggageItem(key);
}
@Override
public Span setOperationName(String operationName) {
return span.setOperationName(operationName);
}
@SuppressWarnings("deprecation")
@Override
public Span log(String eventName, Object payload) {
return span.log(eventName, payload);
}
@SuppressWarnings("deprecation")
@Override
public Span log(long timestampMicroseconds, String eventName, Object payload) {
return span.log(timestampMicroseconds, eventName, payload);
}
@Override
public void finish() {
span.finish();
}
@Override
public void finish(long finishMicros) {
span.finish(finishMicros);
}
public String toTraceId() {
return traceId;
}
public String toSpanId() {
return spanId;
}
private String createTraceId() {
Object span = StrategyTracerContext.getContext().getSpan();
if (span != null) {
// 该方式适用在WebFlux模式下
StrategySpan strategySpan = (StrategySpan) span;
return strategySpan.getTraceId();
} else {
// 该方式适用在WebMvc模式下
return TraceContext.traceId();
}
}
private String createSpanId() {
return StrategyConstants.IGNORED;
}
}