io.opentracing.Scope.close()方法的使用及代码示例

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

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

Scope.close介绍

[英]Mark the end of the active period for the current thread and Scope, updating the ScopeManager#active() in the process.

NOTE: Calling #close more than once on a single Scope instance leads to undefined behavior.
[中]

代码示例

代码示例来源:origin: qunarcorp/qmq

private void closeTrace() {
  if (traceScope == null) return;
  traceScope.close();
}

代码示例来源:origin: qunarcorp/qmq

@Override
  public void postOnMessage(Message message, Throwable e, Map<String, Object> filterContext) {
    Object o = filterContext.get(TRACE_OBJECT);
    if (!(o instanceof Span)) return;
    Scope scope = tracer.scopeManager().activate((Span) o, true);
    scope.close();
  }
}

代码示例来源:origin: stagemonitor/stagemonitor

@Override
public void close(MessageContext context) {
  if (!shouldExecute(context)) {
    return;
  }
  tracingPlugin.getTracer().scopeManager().active().close();
}

代码示例来源:origin: stagemonitor/stagemonitor

public void monitorStop() {
  final Scope activeScope = tracingPlugin.getTracer().scopeManager().active();
  if (activeScope != null) {
    final Span currentSpan = activeScope.span();
    final SpanContextInformation info = SpanContextInformation.get(currentSpan);
    if (info != null) {
      long overhead2 = System.nanoTime();
      trackOverhead(info.getOverhead1(), overhead2);
    }
    activeScope.close();
  }
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void testDontMonitorClientRootSpans() throws Exception {
  MockTracer tracer = new MockTracer();
  new AbstractExternalRequest(tracer) {
    @Override
    protected String getType() {
      return "jdbc";
    }
  }.createScope().close();
  assertThat(tracer.finishedSpans().get(0).tags().get(Tags.SAMPLING_PRIORITY.getKey())).isEqualTo(0);
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void testHonorDoNotTrack() throws Exception {
  when(servletPlugin.isHonorDoNotTrackHeader()).thenReturn(true);
  final MockHttpServletRequest request = new MockHttpServletRequest();
  request.addHeader("dnt", "1");
  Scope activeScope = new MonitoredHttpRequest(request, mock(StatusExposingByteCountingServletResponse.class),
      mock(FilterChain.class), configuration, mock(ExecutorService.class)).createScope();
  SpanWrapper span = SpanContextInformation.getCurrent().getSpanWrapper();
  assertThat(span.getNumberTag(Tags.SAMPLING_PRIORITY.getKey())).isEqualTo(0);
  activeScope.close();
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void testNoDoNotTrackHeader() throws Exception {
  when(servletPlugin.isHonorDoNotTrackHeader()).thenReturn(true);
  final MockHttpServletRequest request = new MockHttpServletRequest();
  Scope activeScope = new MonitoredHttpRequest(request, mock(StatusExposingByteCountingServletResponse.class),
      mock(FilterChain.class), configuration, mock(ExecutorService.class)).createScope();
  SpanWrapper span = SpanContextInformation.getCurrent().getSpanWrapper();
  assertThat(span.getNumberTag(Tags.SAMPLING_PRIORITY.getKey())).isNotEqualTo(0);
  activeScope.close();
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void testDontHonorDoNotTrack() throws Exception {
  when(servletPlugin.isHonorDoNotTrackHeader()).thenReturn(false);
  final MockHttpServletRequest request = new MockHttpServletRequest();
  request.addHeader("dnt", "1");
  Scope activeScope = new MonitoredHttpRequest(request, mock(StatusExposingByteCountingServletResponse.class),
      mock(FilterChain.class), configuration, mock(ExecutorService.class)).createScope();
  SpanWrapper span = SpanContextInformation.getCurrent().getSpanWrapper();
  assertThat(span.getNumberTag(Tags.SAMPLING_PRIORITY.getKey())).isNotEqualTo(0);
  activeScope.close();
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void testDoNotTrackDisabled() throws Exception {
  when(servletPlugin.isHonorDoNotTrackHeader()).thenReturn(true);
  final MockHttpServletRequest request = new MockHttpServletRequest();
  request.addHeader("dnt", "0");
  Scope activeScope = new MonitoredHttpRequest(request, mock(StatusExposingByteCountingServletResponse.class),
      mock(FilterChain.class), configuration, mock(ExecutorService.class)).createScope();
  SpanWrapper span = SpanContextInformation.getCurrent().getSpanWrapper();
  assertThat(span.getNumberTag(Tags.SAMPLING_PRIORITY.getKey())).isNotEqualTo(0);
  activeScope.close();
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void testParseUserAgent() throws Exception {
  doReturn(true).when(servletPlugin).isParseUserAgent();
  final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test.js");
  request.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
  final MonitoredHttpRequest monitoredHttpRequest = createMonitoredHttpRequest(request);
  monitoredHttpRequest.createScope().close();
  assertEquals(1, tracer.finishedSpans().size());
  final MockSpan mockSpan = tracer.finishedSpans().get(0);
  assertThat(mockSpan.tags()).containsEntry("user_agent.browser", "Chrome");
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void testReferringSameHostSite() throws Exception {
  final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test.js");
  request.addHeader("Referer", "https://www.myapp.com:8080/categories");
  request.setServerName("www.myapp.com");
  final MonitoredHttpRequest monitoredHttpRequest = createMonitoredHttpRequest(request);
  monitoredHttpRequest.createScope().close();
  assertEquals(1, tracer.finishedSpans().size());
  final MockSpan mockSpan = tracer.finishedSpans().get(0);
  assertNull(mockSpan.tags().get("http.referring_site"));
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void testReferringSite() throws Exception {
  final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test.js");
  request.addHeader("Referer", "https://www.github.com/stagemonitor/stagemonitor");
  final MonitoredHttpRequest monitoredHttpRequest = createMonitoredHttpRequest(request);
  monitoredHttpRequest.createScope().close();
  assertEquals(1, tracer.finishedSpans().size());
  final MockSpan mockSpan = tracer.finishedSpans().get(0);
  assertEquals("www.github.com", mockSpan.tags().get("http.referring_site"));
}

代码示例来源:origin: stagemonitor/stagemonitor

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
  final Scope scope = new ExternalHttpRequest(tracingPlugin.getTracer(), request.getMethod().toString(), removeQuery(request.getURI()), request.getURI().getHost(), request.getURI().getPort()).createScope();
  try {
    Profiler.start(request.getMethod().toString() + " " + request.getURI() + " ");
    tracingPlugin.getTracer().inject(scope.span().context(), Format.Builtin.HTTP_HEADERS, new SpringHttpRequestInjectAdapter(request));
    return execution.execute(request, body);
  } finally {
    Profiler.stop();
    scope.close();
  }
}

代码示例来源:origin: qunarcorp/qmq

static void ackWithTrace(PulledMessage message, Throwable throwable) {
    Scope scope = GlobalTracer.get()
        .buildSpan("Qmq.Consume.Ack")
        .withTag("subject", message.getSubject())
        .withTag("messageId", message.getMessageId())
        .withTag("consumerGroup", message.getStringProperty(BaseMessage.keys.qmq_consumerGroupName))
        .startActive(true);
    try {
      ack(message, throwable);
    } catch (Exception e) {
      scope.span().log("ack_failed");
      LOGGER.error("ack exception.", e);
      Metrics.counter("qmq_pull_ackError", SUBJECT_ARRAY, new String[]{message.getSubject()}).inc();
    } finally {
      if (scope != null) {
        scope.close();
      }
    }
  }
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void testNumberFormatExceptionCreateSpan() throws Exception {
  final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test.js");
  request.addHeader("spanid", "<script>alert(1);</script>");
  request.addHeader("traceid", "42");
  final MonitoredHttpRequest monitoredHttpRequest = createMonitoredHttpRequest(request);
  monitoredHttpRequest.createScope().close();
  assertEquals(1, tracer.finishedSpans().size());
  final MockSpan mockSpan = tracer.finishedSpans().get(0);
  assertEquals("/test.js", mockSpan.tags().get(Tags.HTTP_URL.getKey()));
  assertEquals("GET *.js", mockSpan.operationName());
  assertEquals("GET", mockSpan.tags().get("method"));
  assertFalse(mockSpan.tags().containsKey(Tags.ERROR.getKey()));
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void testCreateSpan() throws Exception {
  final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test.js");
  request.addParameter("foo", "bar");
  request.addParameter("bla", "blubb");
  request.addParameter("pwd", "secret");
  request.addParameter("creditCard", "123456789");
  request.addHeader("Cookie", "foobar");
  request.addHeader("Accept", "application/json");
  final MonitoredHttpRequest monitoredHttpRequest = createMonitoredHttpRequest(request);
  monitoredHttpRequest.createScope().close();
  assertEquals(1, tracer.finishedSpans().size());
  final MockSpan mockSpan = tracer.finishedSpans().get(0);
  assertEquals("/test.js", mockSpan.tags().get(Tags.HTTP_URL.getKey()));
  assertEquals("GET *.js", mockSpan.operationName());
  assertEquals("GET", mockSpan.tags().get("method"));
  assertEquals("application/json", mockSpan.tags().get(SpanUtils.HTTP_HEADERS_PREFIX + "accept"));
  assertFalse(mockSpan.tags().containsKey(SpanUtils.HTTP_HEADERS_PREFIX + "cookie"));
  assertFalse(mockSpan.tags().containsKey(SpanUtils.HTTP_HEADERS_PREFIX + "Cookie"));
  assertEquals("bar", mockSpan.tags().get(SpanUtils.PARAMETERS_PREFIX + "foo"));
  assertEquals("blubb", mockSpan.tags().get(SpanUtils.PARAMETERS_PREFIX + "bla"));
  assertEquals("XXXX", mockSpan.tags().get(SpanUtils.PARAMETERS_PREFIX + "pwd"));
  assertEquals("XXXX", mockSpan.tags().get(SpanUtils.PARAMETERS_PREFIX + "creditCard"));
  assertFalse(mockSpan.tags().containsKey(Tags.ERROR.getKey()));
}

代码示例来源:origin: io.opentracing.contrib/opentracing-concurrent

@Override
 public void run() {
  Scope scope = span == null ? null : tracer.scopeManager().activate(span, false);
  try {
   delegate.run();
  } finally {
   if (scope != null) {
    scope.close();
   }
  }
 }
}

代码示例来源:origin: io.opentracing.contrib/opentracing-jdbc

@Override
public int[] executeBatch() throws SQLException {
 Scope scope = buildScopeForBatch();
 try {
  return statement.executeBatch();
 } catch (Exception e) {
  JdbcTracingUtils.onError(e, scope.span());
  throw e;
 } finally {
  scope.close();
 }
}

代码示例来源:origin: com.couchbase.client/java-client

@Override
  public void call() {
    if (parent != null) {
      environment.tracer().scopeManager()
        .activate(parent, true)
        .close();
    }
  }
};

代码示例来源:origin: teiid/teiid

@Test public void testSpanContextInjection() {
  MockTracer tracer = new MockTracer();
  assertNull(GlobalTracerInjector.getSpanContext(tracer));
  Scope ignored = tracer.buildSpan("x").startActive(true);
  try {
    assertEquals("{\"spanid\":\"2\",\"traceid\":\"1\"}", GlobalTracerInjector.getSpanContext(tracer));
  } finally {
    ignored.close();
  }
}

相关文章