当前位置: 首页>>代码示例>>Java>>正文


Java SpanContext类代码示例

本文整理汇总了Java中com.google.cloud.trace.core.SpanContext的典型用法代码示例。如果您正苦于以下问题:Java SpanContext类的具体用法?Java SpanContext怎么用?Java SpanContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


SpanContext类属于com.google.cloud.trace.core包,在下文中一共展示了SpanContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: doFilter

import com.google.cloud.trace.core.SpanContext; //导入依赖的package包/类
@Override
public void doFilter(
    ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
  HttpServletRequest httpRequest = (HttpServletRequest) request;

  String contextHeader = httpRequest.getHeader(SpanContextFactory.headerKey());
  SpanContext context;
  if (contextHeader != null) {
    context = spanContextFactory.fromHeader(contextHeader);
  } else {
    context = spanContextFactory.initialContext();
  }
  SpanContextHandle handle = spanContextHandler.attach(context);
  try {
    chain.doFilter(request, response);
  } finally {
    handle.detach();
  }
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-trace-java,代码行数:21,代码来源:RequestTraceContextFilter.java

示例2: generateStartSpan

import com.google.cloud.trace.core.SpanContext; //导入依赖的package包/类
/**
 * Converts a start span event into an API v1 trace message.
 *
 * @param projectId     a string that contains the Google Cloud Platform project identifier.
 * @param context       a span context that represents the event span.
 * @param parentContext a span context that represents the parent span of the event span.
 * @param spanKind      the span kind of the event span.
 * @param name          a string containing the name of the event span.
 * @param timestamp     a timestamp that represents the start time of the event span.
 * @return a trace message that represents the start span event.
 */
public Trace generateStartSpan(String projectId, SpanContext context,
    SpanContext parentContext, SpanKind spanKind, String name, Timestamp timestamp) {
  TraceSpan.Builder spanBuilder =
      TraceSpan.newBuilder()
          .setSpanId(context.getSpanId().getSpanId())
          .setKind(toSpanKindProto(spanKind))
          .setName(name)
          .setStartTime(toTimestamp(timestamp));

  if (parentContext.getTraceId().equals(context.getTraceId())
      && parentContext.getSpanId().isValid()) {
    spanBuilder.setParentSpanId(parentContext.getSpanId().getSpanId());
  }

  Trace.Builder traceBuilder =
      Trace.newBuilder()
          .setProjectId(projectId)
          .setTraceId(formatTraceId(context.getTraceId()))
          .addSpans(spanBuilder.build());

  return traceBuilder.build();
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-trace-java,代码行数:34,代码来源:TraceProducer.java

示例3: generateAnnotateSpan

import com.google.cloud.trace.core.SpanContext; //导入依赖的package包/类
/**
 * Converts a span label annotation event into an API v1 trace message.
 *
 * @param projectId a string that contains the Google Cloud Platform project identifier.
 * @param context   a span context that represents the event span.
 * @param labels    a labels containing the label annotations for the event span.
 * @return a trace message that represents the span label annotation event.
 */
public Trace generateAnnotateSpan(String projectId, SpanContext context, Labels labels) {
  TraceSpan.Builder spanBuilder =
      TraceSpan.newBuilder().setSpanId(context.getSpanId().getSpanId());

  for (Label label : labels.getLabels()) {
    spanBuilder.putLabels(label.getKey(), label.getValue());
  }

  Trace.Builder traceBuilder =
      Trace.newBuilder()
          .setProjectId(projectId)
          .setTraceId(formatTraceId(context.getTraceId()))
          .addSpans(spanBuilder.build());

  return traceBuilder.build();
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-trace-java,代码行数:25,代码来源:TraceProducer.java

示例4: getSpanContext

import com.google.cloud.trace.core.SpanContext; //导入依赖的package包/类
private static SpanContext getSpanContext(CloudTraceContext cloudTraceContext) {
  if (cloudTraceContext == null) {
    return new SpanContext(TraceId.invalid(), SpanId.invalid(), new TraceOptions());
  }

  try {
    // Extract the trace ID from the binary protobuf CloudTraceContext#traceId.
    TraceIdProto traceIdProto = TraceIdProto.parseFrom(cloudTraceContext.getTraceId());
    BigInteger traceIdHi = UnsignedLong.fromLongBits(traceIdProto.getHi()).bigIntegerValue();
    BigInteger traceIdLo = UnsignedLong.fromLongBits(traceIdProto.getLo()).bigIntegerValue();
    BigInteger traceId = traceIdHi.shiftLeft(64).or(traceIdLo);

    return new SpanContext(new TraceId(traceId), new SpanId(cloudTraceContext.getSpanId()),
        new TraceOptions((int) cloudTraceContext.getTraceMask()));
  } catch (InvalidProtocolBufferException e) {
    throw Throwables.propagate(e);
  }
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-trace-java,代码行数:19,代码来源:AppEngineSpanContextHandle.java

示例5: getCurrent

import com.google.cloud.trace.core.SpanContext; //导入依赖的package包/类
@Test
public void getCurrent() {
  CloudTraceContext cloudTraceContext = new CloudTraceContext(
      // Protobuf-encoded upper and lower 64 bits of the example trace ID
      // 7ae1c6346b9cf9a272cb6504b5a10dcc/123456789.
      new byte[] {(byte) 0x09, (byte) 0xa2, (byte) 0xf9, (byte) 0x9c, (byte) 0x6b, (byte) 0x34,
          (byte) 0xc6, (byte) 0xe1, (byte) 0x7a, (byte) 0x11, (byte) 0xcc, (byte) 0x0d,
          (byte) 0xa1, (byte) 0xb5, (byte) 0x04, (byte) 0x65, (byte) 0xcb, (byte) 0x72},
      123456789L,
      // Trace enabled.
      1L);
  when(mockSpan.getContext()).thenReturn(cloudTraceContext);
  SpanContextHandle handle = new AppEngineSpanContextHandle(mockSpan);

  SpanContext current = handle.getCurrentSpanContext();

  assertThat(current).isEqualTo(
      new SpanContext(new TraceId(new BigInteger("7ae1c6346b9cf9a272cb6504b5a10dcc", 16)),
          new SpanId(123456789L), new TraceOptions(1)));
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-trace-java,代码行数:21,代码来源:AppEngineSpanContextHandleTest.java

示例6: startSpanOptions

import com.google.cloud.trace.core.SpanContext; //导入依赖的package包/类
private SpanContext startSpanOptions(SpanContext parentContext, String name, Timestamp timestamp,
    SpanKind spanKind, Boolean enableTrace, Boolean enableStackTrace) {
  if (timestamp == null) {
    timestamp = timestampFactory.now();
  }
  if (spanKind == null) {
    spanKind = SpanKind.UNSPECIFIED;
  }
  SpanContext context;
  if (enableTrace != null || enableStackTrace != null) {
    TraceOptions options = parentContext.getTraceOptions();
    if (enableTrace != null) {
      options = options.overrideTraceEnabled(enableTrace);
    }
    if (enableStackTrace != null) {
      options = options.overrideStackTraceEnabled(enableStackTrace);
    }
    context = spanContextFactory.childContext(parentContext.overrideOptions(options));
  } else {
    context = spanContextFactory.childContext(parentContext);
  }
  for (TraceSink sink : sinks) {
    sink.startSpan(context, parentContext, spanKind, name, timestamp);
  }
  return context;
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-trace-java,代码行数:27,代码来源:SpanContextHandlerTracer.java

示例7: testStartSpanWithTraceOptionsTrue

import com.google.cloud.trace.core.SpanContext; //导入依赖的package包/类
@Test
public void testStartSpanWithTraceOptionsTrue() {
  SpanContext initial = new SpanContext(
      new TraceId(BigInteger.valueOf(1000)),
      new SpanId(2000),
      new TraceOptions());
  contextHandler.attach(initial);
  StartSpanOptions options = new StartSpanOptions()
      .setEnableTrace(true)
      .setEnableStackTrace(true);

  TraceContext traceContext = tracer.startSpan("foo", options);
  assertThat(sink.startSpanEvents).hasSize(1);
  TestTraceSink.StartSpanEvent startEvent1 = sink.startSpanEvents.get(0);

  SpanContext currentContext = traceContext.getHandle().getCurrentSpanContext();
  assertThat(currentContext).isEqualTo(contextHandler.current());
  assertThat(currentContext).isEqualTo(startEvent1.context);
  assertThat(currentContext.getTraceOptions().getTraceEnabled()).isEqualTo(true);
  assertThat(currentContext.getTraceOptions().getStackTraceEnabled()).isEqualTo(true);

  assertThat(startEvent1.name).isEqualTo("foo");
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-trace-java,代码行数:24,代码来源:SpanContextHandlerTracerTest.java

示例8: testStartSpanWithTraceOptionsFalse

import com.google.cloud.trace.core.SpanContext; //导入依赖的package包/类
@Test
public void testStartSpanWithTraceOptionsFalse() {
  SpanContext initial = new SpanContext(
      new TraceId(BigInteger.valueOf(1000)),
      new SpanId(2000),
      new TraceOptions(3));
  contextHandler.attach(initial);
  StartSpanOptions options = new StartSpanOptions()
      .setEnableTrace(false)
      .setEnableStackTrace(false);

  TraceContext traceContext = tracer.startSpan("foo", options);
  assertThat(sink.startSpanEvents).hasSize(1);
  TestTraceSink.StartSpanEvent startEvent1 = sink.startSpanEvents.get(0);

  SpanContext currentContext = traceContext.getHandle().getCurrentSpanContext();
  assertThat(currentContext).isEqualTo(contextHandler.current());
  assertThat(currentContext).isEqualTo(startEvent1.context);
  assertThat(currentContext.getTraceOptions().getTraceEnabled()).isEqualTo(false);
  assertThat(currentContext.getTraceOptions().getStackTraceEnabled()).isEqualTo(false);

  assertThat(startEvent1.name).isEqualTo("foo");
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-trace-java,代码行数:24,代码来源:SpanContextHandlerTracerTest.java

示例9: generateEndSpan

import com.google.cloud.trace.core.SpanContext; //导入依赖的package包/类
/**
 * Converts an end span event into an API v1 trace message.
 *
 * @param projectId a string that contains the Google Cloud Platform project identifier.
 * @param context   a span context that represents the event span.
 * @param timestamp a timestamp that represents the end time of the event span.
 * @return a trace message that represents the end span event.
 */
public Trace generateEndSpan(String projectId, SpanContext context, Timestamp timestamp) {
  TraceSpan.Builder spanBuilder =
      TraceSpan.newBuilder()
          .setSpanId(context.getSpanId().getSpanId())
          .setEndTime(toTimestamp(timestamp));

  Trace.Builder traceBuilder =
      Trace.newBuilder()
          .setProjectId(projectId)
          .setTraceId(formatTraceId(context.getTraceId()))
          .addSpans(spanBuilder.build());

  return traceBuilder.build();
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-trace-java,代码行数:23,代码来源:TraceProducer.java

示例10: startSpan

import com.google.cloud.trace.core.SpanContext; //导入依赖的package包/类
@Override
public void startSpan(SpanContext context, SpanContext parentContext,
    SpanKind spanKind, String name, Timestamp timestamp) {
  if (context.getTraceOptions().getTraceEnabled()) {
    Trace trace = traceProducer.generateStartSpan(
        projectId, context, parentContext, spanKind, name, timestamp);
    traceConsumer.receive(Traces.newBuilder().addTraces(trace).build());
  }
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-trace-java,代码行数:10,代码来源:TraceSinkV1.java

示例11: endSpan

import com.google.cloud.trace.core.SpanContext; //导入依赖的package包/类
@Override
public void endSpan(SpanContext context, Timestamp timestamp) {
  if (context.getTraceOptions().getTraceEnabled()) {
    Trace trace = traceProducer.generateEndSpan(projectId, context, timestamp);
    traceConsumer.receive(Traces.newBuilder().addTraces(trace).build());
  }
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-trace-java,代码行数:8,代码来源:TraceSinkV1.java

示例12: annotateSpan

import com.google.cloud.trace.core.SpanContext; //导入依赖的package包/类
@Override
public void annotateSpan(SpanContext context, Labels labels) {
  if (context.getTraceOptions().getTraceEnabled()) {
    Trace trace = traceProducer.generateAnnotateSpan(projectId, context, labels);
    traceConsumer.receive(Traces.newBuilder().addTraces(trace).build());
  }
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-trace-java,代码行数:8,代码来源:TraceSinkV1.java

示例13: setStackTrace

import com.google.cloud.trace.core.SpanContext; //导入依赖的package包/类
@Override
public void setStackTrace(SpanContext context, StackTrace stackTrace) {
  if (context.getTraceOptions().getTraceEnabled()) {
    Trace trace = traceProducer.generateSetStackTrace(projectId, context, stackTrace);
    traceConsumer.receive(Traces.newBuilder().addTraces(trace).build());
  }
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-trace-java,代码行数:8,代码来源:TraceSinkV1.java

示例14: getCurrent_noContext

import com.google.cloud.trace.core.SpanContext; //导入依赖的package包/类
@Test
public void getCurrent_noContext() {
  when(mockSpan.getContext()).thenReturn(null);
  SpanContextHandle handle = new AppEngineSpanContextHandle(mockSpan);

  SpanContext current = handle.getCurrentSpanContext();

  assertThat(current).isEqualTo(
      new SpanContext(TraceId.invalid(), SpanId.invalid(), new TraceOptions()));
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-trace-java,代码行数:11,代码来源:AppEngineSpanContextHandleTest.java

示例15: startSpan

import com.google.cloud.trace.core.SpanContext; //导入依赖的package包/类
@Override
public TraceContext startSpan(String name, StartSpanOptions options) {
  SpanContext parent = contextHandler.current();
  SpanContext child = startSpanOptions(parent, name, options.getTimestamp(), options.getSpanKind(),
      options.getEnableTrace(), options.getEnableStackTrace());
  SpanContextHandle handle = contextHandler.attach(child);
  TraceContext traceContext = new TraceContext(handle);
  annotateSpan(traceContext, agentLabel);
  return traceContext;
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-trace-java,代码行数:11,代码来源:SpanContextHandlerTracer.java


注:本文中的com.google.cloud.trace.core.SpanContext类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。