public void createGroupId(String groupId) { ExtraFieldPropagation.set(GROUP_ID_FIELD_NAME, groupId); }
public void createAppList(String appList) { ExtraFieldPropagation.set(TX_APP_LIST, appList); }
public void createManagerKey(String managerKey) { ExtraFieldPropagation.set(TX_MANAGER_FIELD_NAME, managerKey); }
@GET @Produces("text/plain; charset=UTF-8") public String get() { // noop if not configured ExtraFieldPropagation.set("country-code", "FO"); return "hello world"; } }
@RequestMapping("/tracedextra") public ResponseEntity<String> tracedextra() throws IOException { ExtraFieldPropagation.set("country-code", "FO"); return ResponseEntity.ok("hello world"); }
@GET @Path("/tracedextra") public String tracedextra() { // noop if not configured ExtraFieldPropagation.set("country-code", "FO"); return "hello world"; }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { // noop if not configured ExtraFieldPropagation.set("country-code", "FO"); resp.addHeader("Content-Type", "text/plain; charset=UTF-8"); resp.getWriter().println("hello world"); } }
/** * Sets the current value of the field with the specified key, or drops if not a configured * field. * * <p>Prefer {@link #set(TraceContext, String, String)} if you have a reference to a span. */ public static void set(String name, String value) { TraceContext context = currentTraceContext(); if (context != null) set(context, name, value); }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (!(msg instanceof HttpRequest)) { ctx.fireChannelRead(msg); return; } String uri = ((HttpRequest) msg).uri(); if ("/unsampled".equals(uri)) { ctx.channel().attr(URI_ATTRIBUTE).set(uri); unsampled.channelRead(ctx, msg); } else if ("/traced".equals(uri)) { ctx.channel().attr(URI_ATTRIBUTE).set(uri); traced.channelRead(ctx, msg); } else if ("/tracedextra".equals(uri)) { ctx.channel().attr(URI_ATTRIBUTE).set(uri); ExtraFieldPropagation.set("country-code", "FO"); tracedExtra.channelRead(ctx, msg); } else if ("/traced128".equals(uri)) { ctx.channel().attr(URI_ATTRIBUTE).set(uri); traced128.channelRead(ctx, msg); } else { ctx.fireChannelRead(msg); } }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.addHeader("Content-Type", "text/plain; charset=UTF-8"); // regardless of how we got here, if we are "api" we just return a string if (req.getRequestURI().endsWith("/api")) { resp.getWriter().println(new Date().toString()); } else { // noop if extra field propagation is not configured ExtraFieldPropagation.set("country-code", "FO"); Request request = new Request.Builder().url(new HttpUrl.Builder() .scheme("http") .host("127.0.0.1") .port(PORT) .encodedPath(req.getRequestURI() + "/api").build()).build(); // If we are tracing, we'll have a scoped call factory available Call.Factory localCallFactory = (Call.Factory) req.getAttribute(Call.Factory.class.getName()); if (localCallFactory == null) localCallFactory = callFactory; resp.getWriter().println(localCallFactory.newCall(request).execute().body().string()); } } }
@Override public void init() { Spark.before(sparkTracing.before()); Spark.get("/tracedextra", (Request request, Response response) -> { ExtraFieldPropagation.set("country-code", "FO"); return "hello world"; }); Spark.afterAfter(sparkTracing.afterAfter()); } }
@Test public void propagatesExtra_newTrace() throws Exception { Tracer tracer = httpTracing.tracing().tracer(); server.enqueue(new MockResponse()); ScopedSpan parent = tracer.startScopedSpan("test"); try { ExtraFieldPropagation.set(parent.context(), EXTRA_KEY, "joey"); get(client, "/foo"); } finally { parent.finish(); } assertThat(server.takeRequest().getHeader(EXTRA_KEY)) .isEqualTo("joey"); // we report one in-process and one RPC client span assertThat(Arrays.asList(takeSpan(), takeSpan())) .extracting(Span::kind) .containsOnly(null, Span.Kind.CLIENT); }
@Test public void propagatesExtra_unsampledTrace() throws Exception { Tracer tracer = httpTracing.tracing().tracer().withSampler(Sampler.NEVER_SAMPLE); server.enqueue(new MockResponse()); ScopedSpan parent = tracer.startScopedSpan("test"); try { ExtraFieldPropagation.set(parent.context(), EXTRA_KEY, "joey"); get(client, "/foo"); } finally { parent.finish(); } assertThat(server.takeRequest().getHeader(EXTRA_KEY)) .isEqualTo("joey"); }
/** * Sets the current value of the field with the specified key, or drops if not a configured * field. * * <p>Prefer {@link #set(TraceContext, String, String)} if you have a reference to a span. */ public static void set(String name, String value) { TraceContext context = currentTraceContext(); if (context != null) set(context, name, value); }
/** This is a NOOP unless {@link ExtraFieldPropagation} is in use */ @Override public BraveSpan setBaggageItem(String key, String value) { ExtraFieldPropagation.set(delegate.context(), key, value); return this; }
@Test public void propagatesExtra_newTrace() throws Exception { Tracer tracer = httpTracing.tracing().tracer(); server.enqueue(new MockResponse()); ScopedSpan parent = tracer.startScopedSpan("test"); try { ExtraFieldPropagation.set(parent.context(), EXTRA_KEY, "joey"); get(client, "/foo"); } finally { parent.finish(); } assertThat(server.takeRequest().getHeader(EXTRA_KEY)) .isEqualTo("joey"); // we report one in-process and one RPC client span assertThat(Arrays.asList(takeSpan(), takeSpan())) .extracting(Span::kind) .containsOnly(null, Span.Kind.CLIENT); }
@Test public void propagatesExtra_unsampledTrace() throws Exception { Tracer tracer = httpTracing.tracing().tracer().withSampler(Sampler.NEVER_SAMPLE); server.enqueue(new MockResponse()); ScopedSpan parent = tracer.startScopedSpan("test"); try { ExtraFieldPropagation.set(parent.context(), EXTRA_KEY, "joey"); get(client, "/foo"); } finally { parent.finish(); } assertThat(server.takeRequest().getHeader(EXTRA_KEY)) .isEqualTo("joey"); }