Java源码示例:io.opencensus.trace.Tracer
示例1
CensusTracingModule(
Tracer censusTracer, final BinaryFormat censusPropagationBinaryFormat) {
this.censusTracer = checkNotNull(censusTracer, "censusTracer");
checkNotNull(censusPropagationBinaryFormat, "censusPropagationBinaryFormat");
this.tracingHeader =
Metadata.Key.of("grpc-trace-bin", new Metadata.BinaryMarshaller<SpanContext>() {
@Override
public byte[] toBytes(SpanContext context) {
return censusPropagationBinaryFormat.toByteArray(context);
}
@Override
public SpanContext parseBytes(byte[] serialized) {
try {
return censusPropagationBinaryFormat.fromByteArray(serialized);
} catch (Exception e) {
logger.log(Level.FINE, "Failed to parse tracing header", e);
return SpanContext.INVALID;
}
}
});
}
示例2
static Object proceed(
ProceedingJoinPoint call, Tracer tracer, String spanName, String... annotations)
throws Throwable {
Scope scope = tracer.spanBuilder(spanName).startScopedSpan();
try {
for (String annotation : annotations) {
tracer.getCurrentSpan().addAnnotation(annotation);
}
return call.proceed();
} catch (Throwable t) {
Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>();
String message = t.getMessage();
attributes.put(
"message", AttributeValue.stringAttributeValue(message == null ? "null" : message));
attributes.put("type", AttributeValue.stringAttributeValue(t.getClass().toString()));
Span span = tracer.getCurrentSpan();
span.addAnnotation("error", attributes);
span.setStatus(Status.UNKNOWN);
throw t;
} finally {
scope.close();
}
}
示例3
/**
* Creates a {@link HttpServerHandler} with given parameters.
*
* @param tracer the Open Census tracing component.
* @param extractor the {@code HttpExtractor} used to extract information from the
* request/response.
* @param textFormat the {@code TextFormat} used in HTTP propagation.
* @param getter the getter used when extracting information from the {@code carrier}.
* @param publicEndpoint set to true for publicly accessible HTTP(S) server. If true then incoming
* tracecontext will be added as a link instead of as a parent.
* @since 0.19
*/
public HttpServerHandler(
Tracer tracer,
HttpExtractor<Q, P> extractor,
TextFormat textFormat,
TextFormat.Getter<C> getter,
Boolean publicEndpoint) {
super(extractor);
checkNotNull(tracer, "tracer");
checkNotNull(textFormat, "textFormat");
checkNotNull(getter, "getter");
checkNotNull(publicEndpoint, "publicEndpoint");
this.tracer = tracer;
this.textFormat = textFormat;
this.getter = getter;
this.publicEndpoint = publicEndpoint;
this.statsRecorder = Stats.getStatsRecorder();
this.tagger = Tags.getTagger();
}
示例4
/**
* Creates a {@link HttpClientHandler} with given parameters.
*
* @param tracer the Open Census tracing component.
* @param extractor the {@code HttpExtractor} used to extract information from the
* request/response.
* @param textFormat the {@code TextFormat} used in HTTP propagation.
* @param setter the setter used when injecting information to the {@code carrier}.
* @since 0.19
*/
public HttpClientHandler(
Tracer tracer,
HttpExtractor<Q, P> extractor,
TextFormat textFormat,
TextFormat.Setter<C> setter) {
super(extractor);
checkNotNull(setter, "setter");
checkNotNull(textFormat, "textFormat");
checkNotNull(tracer, "tracer");
this.setter = setter;
this.textFormat = textFormat;
this.tracer = tracer;
this.statsRecorder = Stats.getStatsRecorder();
this.tagger = Tags.getTagger();
}
示例5
CensusTracingModule(
Tracer censusTracer, final BinaryFormat censusPropagationBinaryFormat) {
this.censusTracer = checkNotNull(censusTracer, "censusTracer");
checkNotNull(censusPropagationBinaryFormat, "censusPropagationBinaryFormat");
this.tracingHeader =
Metadata.Key.of("grpc-trace-bin", new Metadata.BinaryMarshaller<SpanContext>() {
@Override
public byte[] toBytes(SpanContext context) {
return censusPropagationBinaryFormat.toByteArray(context);
}
@Override
public SpanContext parseBytes(byte[] serialized) {
try {
return censusPropagationBinaryFormat.fromByteArray(serialized);
} catch (Exception e) {
logger.log(Level.FINE, "Failed to parse tracing header", e);
return SpanContext.INVALID;
}
}
});
}
示例6
@SuppressWarnings("unchecked")
public static <T> T instrument(Class<T> iface, T delegate, Tracer tracer) {
return (T) Proxy.newProxyInstance(
iface.getClassLoader(),
new Class[]{iface},
new TracingProxy(delegate, tracer));
}
示例7
static Tracer getTracer(String implementation) {
if (implementation.equals("impl")) {
// We can return the global tracer here because if impl is linked the global tracer will be
// the impl one.
// TODO(bdrutu): Make everything not be a singleton (disruptor, etc.) and use a new
// TraceComponentImpl similar to TraceComponentImplLite.
return Tracing.getTracer();
} else if (implementation.equals("impl-lite")) {
return traceComponentImplLite.getTracer();
} else {
throw new RuntimeException("Invalid tracer implementation requested.");
}
}
示例8
@Setup
public void setup() {
Tracer tracer = BenchmarksUtil.getTracer(implementation);
linkedSpan =
tracer
.spanBuilderWithExplicitParent(SPAN_NAME, null)
.setSampler(sampled ? Samplers.alwaysSample() : Samplers.neverSample())
.startSpan();
span =
tracer
.spanBuilderWithExplicitParent(SPAN_NAME, null)
.setSampler(sampled ? Samplers.alwaysSample() : Samplers.neverSample())
.startSpan();
}
示例9
@Override
public void execute() throws Exception {
Tracer tracer = Tracing.getTracer();
SpanBuilder builder = tracer.spanBuilder("MapTaskExecutor.Span").setRecordEvents(true);
// Start the progress tracker before execution (which blocks until execution is finished).
try (Scope unused = builder.startScopedSpan();
AutoCloseable unused2 = progressTrackerCloseable(progressTracker)) {
tracer.getCurrentSpan().addAnnotation("About to execute");
super.execute();
tracer.getCurrentSpan().addAnnotation("Done with execute");
}
}
示例10
public OpenCensusTracerAdapter(Tracer ocTracer) {
this.ocTracer = ocTracer;
}
示例11
OpenCensusSpanBuilderAdapter(Tracer tracer, String opName) {
this.tracer = tracer;
this.opName = opName;
}
示例12
private TracingProxy(Object delegate, Tracer tracer) {
this.delegate = Objects.requireNonNull(delegate);
this.delegateName = delegate.getClass().getSimpleName();
this.tracer = tracer;
}
示例13
@Override
public Tracer getTracer() {
return traceComponentImplBase.getTracer();
}
示例14
public Tracer getTracer() {
return tracer;
}
示例15
@Override
public Tracer getTracer() {
return traceComponentImplBase.getTracer();
}
示例16
/**
* Creates a {@code CensusSpringAspect} with the given tracer.
*
* @param tracer the tracer responsible for building new spans
* @since 0.16.0
*/
public CensusSpringAspect(Tracer tracer) {
this.tracer = tracer;
}
示例17
/**
* Creates a {@code CensusSpringSqlAspect} with the given tracer.
*
* @param tracer the tracer responsible for building new spans
* @since 0.16.0
*/
public CensusSpringSqlAspect(Tracer tracer) {
this.tracer = tracer;
}
示例18
/**
* Returns the tracing component of OpenCensus.
*
* @return the tracing component of OpenCensus.
*/
public static Tracer getTracer() {
return tracer;
}