zipkin2.Span.tags()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(11.6k)|赞(0)|评价(0)|浏览(110)

本文整理了Java中zipkin2.Span.tags()方法的一些代码示例,展示了Span.tags()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Span.tags()方法的具体详情如下:
包路径:zipkin2.Span
类名称:Span
方法名:tags

Span.tags介绍

[英]Tags a span with context, usually to support query or aggregation.

For example, a tag key could be "http.path".
[中]用上下文标记跨度,通常用于支持查询或聚合。
例如,标记键可以是“http.path”。

代码示例

代码示例来源:origin: openzipkin/brave

private void addsErrorTagOnException(String path) throws Exception {
  Span span = reportsSpanOnException(path);
  assertThat(span.tags())
    .containsKey("error");
 }
}

代码示例来源:origin: openzipkin/brave

@Test public void addsErrorTagOnTransportException() throws Exception {
 Span span = checkReportsSpanOnTransportException();
 assertThat(span.tags())
   .containsKey("error");
}

代码示例来源:origin: openzipkin/brave

/**
 * This tests both that a root path ends up as "/" (slash) not "" (empty), as well that less
 * typical OPTIONS methods can be traced.
 */
@Test public void options() throws Exception {
 assertThat(call("OPTIONS", "/").isSuccessful())
   .isTrue();
 Span span = takeSpan();
 // verify normal tags
 assertThat(span.tags())
   .containsEntry("http.method", "OPTIONS")
   .containsEntry("http.path", "/");
 // Either the span name is the method, or it is a route expression
 String name = span.name();
 if (name != null && !"options".equals(name)) {
  assertThat(name).isEqualTo("options /");
 }
}

代码示例来源:origin: openzipkin/brave

@Test
public void httpPathTagExcludesQueryParams() throws Exception {
 get("/foo?z=2&yAA=1");
 Span span = takeSpan();
 assertThat(span.tags())
   .containsEntry("http.path", "/foo");
}

代码示例来源:origin: openzipkin/brave

/**
 * Shows that a framework can directly use the "brave.Span" rather than relying on the
 * current span.
 */
@Test public void canUseSpanAttribute() throws Exception {
 delegate = customHook;
 get("/foo");
 Span span = takeSpan();
 assertThat(span.tags())
   .containsEntry("foo", "bar");
}

代码示例来源:origin: openzipkin/brave

/** If http route is supported, then the span name should include it */
@Test public void notFound() throws Exception {
 assertThat(call("GET", "/foo/bark").code())
   .isEqualTo(404);
 Span span = takeSpan();
 // verify normal tags
 assertThat(span.tags())
   .hasSize(4)
   .containsEntry("http.method", "GET")
   .containsEntry("http.path", "/foo/bark")
   .containsEntry("http.status_code", "404")
   .containsKey("error"); // as 404 is an error
 // Either the span name is the method, or it is a route expression
 String name = span.name();
 if (name != null && !"get".equals(name)) {
  assertThat(name).isEqualTo("get not_found");
 }
}

代码示例来源:origin: openzipkin/brave

@Test
public void addsStatusCode_badRequest() throws Exception {
 try {
  get("/badrequest");
 } catch (RuntimeException e) {
  // some servers think 400 is an error
 }
 Span span = takeSpan();
 assertThat(span.tags())
   .containsEntry("http.status_code", "400")
   .containsEntry("error", "400");
}

代码示例来源:origin: openzipkin/brave

private void routeBasedRequestNameIncludesPathPrefix(String prefix) throws Exception {
 Response request1 = get(prefix + "/1?foo");
 Response request2 = get(prefix + "/2?bar");
 // get() doesn't check the response, check to make sure the server didn't 500
 assertThat(request1.isSuccessful()).isTrue();
 assertThat(request2.isSuccessful()).isTrue();
 // Reading the route parameter from the response ensures the test endpoint is correct
 assertThat(request1.body().string())
   .isEqualTo("1");
 assertThat(request2.body().string())
   .isEqualTo("2");
 Span span1 = takeSpan(), span2 = takeSpan();
 // verify that the path and url reflect the initial request (not a route expression)
 assertThat(span1.tags())
   .containsEntry("http.method", "GET")
   .containsEntry("http.path", prefix + "/1")
   .containsEntry("http.url", url(prefix + "/1?foo"));
 assertThat(span2.tags())
   .containsEntry("http.method", "GET")
   .containsEntry("http.path", prefix + "/2")
   .containsEntry("http.url", url(prefix + "/2?bar"));
 // We don't know the exact format of the http route as it is framework specific
 // However, we know that it should match both requests and include the common part of the path
 Set<String> routeBasedNames = new LinkedHashSet<>(Arrays.asList(span1.name(), span2.name()));
 assertThat(routeBasedNames).hasSize(1);
 assertThat(routeBasedNames.iterator().next())
   .startsWith("get " + prefix)
   .doesNotEndWith("/") // no trailing slashes
   .doesNotContain("//"); // no duplicate slashes
}

代码示例来源:origin: openzipkin/brave

@Test public void httpPathTagExcludesQueryParams() throws Exception {
 String path = "/foo?z=2&yAA=1";
 server.enqueue(new MockResponse());
 get(client, path);
 Span span = takeSpan();
 assertThat(span.tags())
   .containsEntry("http.path", "/foo");
}

代码示例来源:origin: openzipkin/brave

@Test public void addsStatusCodeWhenNotOk() throws Exception {
 server.enqueue(new MockResponse().setResponseCode(400));
 try {
  get(client, "/foo");
 } catch (Exception e) {
  // some clients think 400 is an error
 }
 Span span = takeSpan();
 assertThat(span.tags())
   .containsEntry("http.status_code", "400")
   .containsEntry("error", "400");
}

代码示例来源:origin: openzipkin/brave

@Test public void redirect() throws Exception {
 Tracer tracer = httpTracing.tracing().tracer();
 server.enqueue(new MockResponse().setResponseCode(302)
   .addHeader("Location: " + url("/bar")));
 server.enqueue(new MockResponse().setResponseCode(404)); // hehe to a bad location!
 ScopedSpan parent = tracer.startScopedSpan("test");
 try {
  get(client, "/foo");
 } catch (RuntimeException e) {
  // some think 404 is an exception
 } finally {
  parent.finish();
 }
 Span client1 = takeSpan();
 Span client2 = takeSpan();
 assertThat(Arrays.asList(client1.tags().get("http.path"), client2.tags().get("http.path")))
   .contains("/foo", "/bar");
 assertThat(takeSpan().kind()).isNull(); // local
}

代码示例来源:origin: line/armeria

@Test(timeout = 20000)
public void shouldSubmitSpanWhenRequestIsSampled() throws Exception {
  final SpanCollectingReporter reporter = testServiceInvocation(1.0f);
  // check span name
  final Span span = reporter.spans().take();
  assertThat(span.name()).isEqualTo(TEST_METHOD);
  // check kind
  assertThat(span.kind()).isSameAs(Kind.SERVER);
  // only one span should be submitted
  assertThat(reporter.spans().poll(1, TimeUnit.SECONDS)).isNull();
  // check # of annotations (we add wire annotations)
  assertThat(span.annotations()).hasSize(2);
  // check tags
  assertThat(span.tags()).containsAllEntriesOf(ImmutableMap.of(
      "http.host", "foo.com",
      "http.method", "POST",
      "http.path", "/hello/trustin",
      "http.status_code", "200",
      "http.url", "none+h2c://foo.com/hello/trustin"));
  // check service name
  assertThat(span.localServiceName()).isEqualTo(TEST_SERVICE);
}

代码示例来源:origin: openzipkin/brave

.isEqualTo("remote-service");
assertThat(span.tags())
  .containsEntry("http.url", url(uri))
  .containsEntry("context.visible", "true")

代码示例来源:origin: line/armeria

@Test(timeout = 20000)
public void shouldSubmitSpanWhenSampled() throws Exception {
  final SpanCollectingReporter reporter = new SpanCollectingReporter();
  final Tracing tracing = Tracing.newBuilder()
                  .localServiceName(TEST_SERVICE)
                  .spanReporter(reporter)
                  .sampler(Sampler.create(1.0f))
                  .build();
  testRemoteInvocation(tracing, null);
  // check span name
  final Span span = reporter.spans().take();
  assertThat(span.name()).isEqualTo(TEST_SPAN);
  // check kind
  assertThat(span.kind()).isSameAs(Kind.CLIENT);
  // only one span should be submitted
  assertThat(reporter.spans().poll(1, TimeUnit.SECONDS)).isNull();
  // check # of annotations (we add wire annotations)
  assertThat(span.annotations()).hasSize(2);
  // check tags
  assertThat(span.tags()).containsAllEntriesOf(ImmutableMap.of(
      "http.host", "foo.com",
      "http.method", "POST",
      "http.path", "/hello/armeria",
      "http.status_code", "200",
      "http.url", "none+h2c://foo.com/hello/armeria"));
  // check service name
  assertThat(span.localServiceName()).isEqualTo(TEST_SERVICE);
  // check remote service name
  assertThat(span.remoteServiceName()).isEqualTo("foo.com");
}

代码示例来源:origin: line/armeria

@Test(timeout = 20000)
public void shouldSubmitSpanWithCustomRemoteName() throws Exception {
  final SpanCollectingReporter reporter = new SpanCollectingReporter();
  final Tracing tracing = Tracing.newBuilder()
                  .localServiceName(TEST_SERVICE)
                  .spanReporter(reporter)
                  .sampler(Sampler.create(1.0f))
                  .build();
  testRemoteInvocation(tracing, "fooService");
  // check span name
  final Span span = reporter.spans().take();
  // check tags
  assertThat(span.tags()).containsAllEntriesOf(ImmutableMap.of(
      "http.host", "foo.com",
      "http.method", "POST",
      "http.path", "/hello/armeria",
      "http.status_code", "200",
      "http.url", "none+h2c://foo.com/hello/armeria"));
  // check service name
  assertThat(span.localServiceName()).isEqualTo(TEST_SERVICE);
  // check remote service name, lower-cased
  assertThat(span.remoteServiceName()).isEqualTo("fooservice");
}

代码示例来源:origin: openzipkin/brave

@Test
public void supportsPortableCustomization() throws Exception {
 httpTracing = httpTracing.toBuilder().serverParser(new HttpServerParser() {
  @Override
  public <Req> void request(HttpAdapter<Req, ?> adapter, Req req, SpanCustomizer customizer) {
   customizer.tag("http.url", adapter.url(req)); // just the path is logged by default
   customizer.tag("context.visible", String.valueOf(currentTraceContext.get() != null));
   customizer.tag("request_customizer.is_span", (customizer instanceof brave.Span) + "");
  }
  @Override
  public <Resp> void response(HttpAdapter<?, Resp> adapter, Resp res, Throwable error,
    SpanCustomizer customizer) {
   super.response(adapter, res, error, customizer);
   customizer.tag("response_customizer.is_span", (customizer instanceof brave.Span) + "");
  }
 }).build();
 init();
 String uri = "/foo?z=2&yAA=1";
 get(uri);
 Span span = takeSpan();
 assertThat(span.tags())
   .containsEntry("http.url", url(uri))
   .containsEntry("context.visible", "true")
   .containsEntry("request_customizer.is_span", "false")
   .containsEntry("response_customizer.is_span", "false");
}

代码示例来源:origin: io.zipkin.brave/brave-instrumentation-http-tests

private void addsErrorTagOnException(String path) throws Exception {
  Span span = reportsSpanOnException(path);
  assertThat(span.tags())
    .containsKey("error");
 }
}

代码示例来源:origin: io.zipkin.brave/brave-http-tests

@Test
public void httpUrlTagIncludesQueryParams() throws Exception {
 String path = "/foo?z=2&yAA=1";
 server.enqueue(new MockResponse());
 get(client, path);
 assertThat(collectedSpans())
   .flatExtracting(s -> s.tags().entrySet())
   .contains(entry("http.url", server.url(path).toString()));
}

代码示例来源:origin: io.zipkin.brave/brave-instrumentation-http-tests

@Test public void httpPathTagExcludesQueryParams() throws Exception {
 String path = "/foo?z=2&yAA=1";
 server.enqueue(new MockResponse());
 get(client, path);
 Span span = takeSpan();
 assertThat(span.tags())
   .containsEntry("http.path", "/foo");
}

代码示例来源:origin: apache/cxf

@Test
public void testThatCurrentSpanIsAnnotatedWithKeyValue() {
  final SpanId spanId = fromRandom();
  final Response r = withTrace(createWebClient("/bookstore/book/1"), spanId).get();
  assertEquals(Status.OK.getStatusCode(), r.getStatus());
  assertThat(TestSpanReporter.getAllSpans().size(), equalTo(1));
  assertThat(TestSpanReporter.getAllSpans().get(0).name(), equalTo("get /bookstore/book/1"));
  assertThat(TestSpanReporter.getAllSpans().get(0).tags(), hasEntry("book-id", "1"));
  assertThatTraceIsPresent(r, spanId);
}

相关文章