/** Returns the value of the field with the specified key or null if not available */ public static String get(TraceContext context, String name, Class<? extends PropagationFields> type) { if (context == null) throw new NullPointerException("context == null"); if (name == null) throw new NullPointerException("name == null"); PropagationFields fields = context.findExtra(type); return fields != null ? fields.get(name) : null; }
/** Replaces the value of the field with the specified key, ignoring if not a permitted field */ public static void put(TraceContext context, String name, String value, Class<? extends PropagationFields> type) { if (context == null) throw new NullPointerException("context == null"); if (name == null) throw new NullPointerException("name == null"); if (value == null) throw new NullPointerException("value == null"); PropagationFields fields = context.findExtra(type); if (fields == null) return; fields.put(name, value); } }
/** * Returns an {@linkplain #extra() extra} of the given type if present or null if not. * * <p>Note: it is the responsibility of {@link Propagation.Factory#decorate(TraceContext)} * to consolidate extra fields. If it doesn't, there could be multiple instance of a given type * and this can break logic. */ public @Nullable <T> T findExtra(Class<T> type) { return findExtra(type, extra); }
/** Returns a mapping of any fields in the trace context. */ public static Map<String, String> getAll(TraceContext context) { if (context == null) throw new NullPointerException("context == null"); PropagationFields fields = context.findExtra(Extra.class); return fields != null ? fields.toMap() : Collections.emptyMap(); }
@Override public void inject(TraceContext traceContext, C carrier) { delegate.inject(traceContext, carrier); Extra extra = traceContext.findExtra(Extra.class); if (extra == null) return; inject(extra, carrier); }
/** Returns a mapping of any fields in the extraction result. */ public static Map<String, String> getAll(TraceContextOrSamplingFlags extracted) { if (extracted == null) throw new NullPointerException("extracted == null"); TraceContext extractedContext = extracted.context(); if (extractedContext != null) return getAll(extractedContext); PropagationFields fields = TraceContext.findExtra(Extra.class, extracted.extra()); return fields != null ? fields.toMap() : Collections.emptyMap(); }
@Override public void inject(TraceContext traceContext, C carrier) { if (carrier instanceof Metadata) { byte[] serialized = TraceContextBinaryFormat.toBytes(traceContext); ((Metadata) carrier).put(GRPC_TRACE_BIN, serialized); Tags tags = traceContext.findExtra(Tags.class); if (tags != null) ((Metadata) carrier).put(GRPC_TAGS_BIN, tags.toMap()); } delegate.inject(traceContext, carrier); } }
@Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(final ServerCall<ReqT, RespT> call, final Metadata headers, final ServerCallHandler<ReqT, RespT> next) { TraceContextOrSamplingFlags extracted = extractor.extract(headers); Span span = extracted.context() != null ? tracer.joinSpan(extracted.context()) : tracer.nextSpan(extracted); // If grpc propagation is enabled, make sure we refresh the server method if (grpcPropagationFormatEnabled) { Tags tags = span.context().findExtra(Tags.class); if (tags != null) tags.put(RPC_METHOD, call.getMethodDescriptor().getFullMethodName()); } span.kind(Span.Kind.SERVER); parser.onStart(call, headers, span.customizer()); // startCall invokes user interceptors, so we place the span in scope here ServerCall.Listener<ReqT> result; SpanInScope scope = tracer.withSpanInScope(span); try { // retrolambda can't resolve this try/finally result = next.startCall(new TracingServerCall<>(span, call, parser), headers); } catch (RuntimeException | Error e) { span.error(e); span.finish(); throw e; } finally { scope.close(); } // This ensures the server implementation can see the span in scope return new ScopingServerCallListener<>(tracer, span, result, parser); }
/** Replaces the value of the field with the specified key, ignoring if not a permitted field */ public static void put(TraceContext context, String name, String value, Class<? extends PropagationFields> type) { if (context == null) throw new NullPointerException("context == null"); if (name == null) throw new NullPointerException("name == null"); if (value == null) throw new NullPointerException("value == null"); PropagationFields fields = context.findExtra(type); if (fields == null) return; fields.put(name, value); } }
/** Returns the value of the field with the specified key or null if not available */ public static String get(TraceContext context, String name, Class<? extends PropagationFields> type) { if (context == null) throw new NullPointerException("context == null"); if (name == null) throw new NullPointerException("name == null"); PropagationFields fields = context.findExtra(type); return fields != null ? fields.get(name) : null; }
/** Returns a mapping of any fields in the trace context. */ public static Map<String, String> getAll(TraceContext context) { if (context == null) throw new NullPointerException("context == null"); PropagationFields fields = context.findExtra(Extra.class); return fields != null ? fields.toMap() : Collections.emptyMap(); }
/** * Returns an {@linkplain #extra() extra} of the given type if present or null if not. * * <p>Note: it is the responsibility of {@link Propagation.Factory#decorate(TraceContext)} * to consolidate extra fields. If it doesn't, there could be multiple instance of a given type * and this can break logic. */ public @Nullable <T> T findExtra(Class<T> type) { return findExtra(type, extra); }
@Override public void inject(TraceContext traceContext, C carrier) { delegate.inject(traceContext, carrier); Extra extra = traceContext.findExtra(Extra.class); if (extra == null) return; inject(extra, carrier); }
/** Returns a mapping of any fields in the extraction result. */ public static Map<String, String> getAll(TraceContextOrSamplingFlags extracted) { if (extracted == null) throw new NullPointerException("extracted == null"); TraceContext extractedContext = extracted.context(); if (extractedContext != null) return getAll(extractedContext); PropagationFields fields = TraceContext.findExtra(Extra.class, extracted.extra()); return fields != null ? fields.toMap() : Collections.emptyMap(); }
@Override public void inject(TraceContext traceContext, C carrier) { if (carrier instanceof Metadata) { byte[] serialized = TraceContextBinaryFormat.toBytes(traceContext); ((Metadata) carrier).put(GRPC_TRACE_BIN, serialized); Tags tags = traceContext.findExtra(Tags.class); if (tags != null) ((Metadata) carrier).put(GRPC_TAGS_BIN, tags.toMap()); } delegate.inject(traceContext, carrier); } }
@Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(final ServerCall<ReqT, RespT> call, final Metadata headers, final ServerCallHandler<ReqT, RespT> next) { TraceContextOrSamplingFlags extracted = extractor.extract(headers); Span span = extracted.context() != null ? tracer.joinSpan(extracted.context()) : tracer.nextSpan(extracted); // If grpc propagation is enabled, make sure we refresh the server method if (grpcPropagationFormatEnabled) { Tags tags = span.context().findExtra(Tags.class); if (tags != null) tags.put(RPC_METHOD, call.getMethodDescriptor().getFullMethodName()); } span.kind(Span.Kind.SERVER); parser.onStart(call, headers, span.customizer()); // startCall invokes user interceptors, so we place the span in scope here ServerCall.Listener<ReqT> result; SpanInScope scope = tracer.withSpanInScope(span); try { // retrolambda can't resolve this try/finally result = next.startCall(new TracingServerCall<>(span, call, parser), headers); } catch (RuntimeException | Error e) { span.error(e); span.finish(); throw e; } finally { scope.close(); } // This ensures the server implementation can see the span in scope return new ScopingServerCallListener<>(tracer, span, result, parser); }