Search in sources :

Example 1 with CloudTraceContext

use of com.google.apphosting.api.CloudTraceContext in project instrumentation-java by census-instrumentation.

the class AppEngineCloudTraceContextUtils method toCloudTraceContext.

/**
 * Converts {@code SpanContext} to AppEngine {@code CloudTraceContext}.
 *
 * @param spanContext the {@code SpanContext}.
 * @return the converted AppEngine {@code CloudTraceContext}.
 * @since 0.14
 */
public static CloudTraceContext toCloudTraceContext(SpanContext spanContext) {
    checkNotNull(spanContext, "spanContext");
    ByteBuffer traceIdBuf = ByteBuffer.wrap(spanContext.getTraceId().getBytes());
    TraceIdProto traceIdProto = TraceIdProto.newBuilder().setHi(traceIdBuf.getLong()).setLo(traceIdBuf.getLong()).build();
    ByteBuffer spanIdBuf = ByteBuffer.wrap(spanContext.getSpanId().getBytes());
    return new CloudTraceContext(traceIdProto.toByteArray(), spanIdBuf.getLong(), spanContext.getTraceOptions().isSampled() ? 1L : 0L);
}
Also used : ByteBuffer(java.nio.ByteBuffer) CloudTraceContext(com.google.apphosting.api.CloudTraceContext)

Example 2 with CloudTraceContext

use of com.google.apphosting.api.CloudTraceContext in project instrumentation-java by census-instrumentation.

the class AppEngineCloudTraceContextUtilsTest method toFromSampledCloudTraceContext.

@Test
public void toFromSampledCloudTraceContext() {
    CloudTraceContext cloudTraceContext = new CloudTraceContext(// fae1c6346b9cf9a272cb6504b5a10dcc/123456789.
    new byte[] { (byte) 0x09, (byte) 0xa2, (byte) 0xf9, (byte) 0x9c, (byte) 0x6b, (byte) 0x34, (byte) 0xc6, (byte) 0xe1, (byte) 0xfa, (byte) 0x11, (byte) 0xcc, (byte) 0x0d, (byte) 0xa1, (byte) 0xb5, (byte) 0x04, (byte) 0x65, (byte) 0xcb, (byte) 0x72 }, Long.MIN_VALUE, // Trace enabled.
    1L);
    SpanContext spanContext = AppEngineCloudTraceContextUtils.fromCloudTraceContext(cloudTraceContext);
    assertThat(spanContext).isEqualTo(SpanContext.create(TraceId.fromLowerBase16("fae1c6346b9cf9a272cb6504b5a10dcc"), SpanId.fromLowerBase16("8000000000000000"), TraceOptions.builder().setIsSampled(true).build()));
    // CloudTraceContext does not implement equals, so need to check every argument.
    CloudTraceContext newCloudTraceContext = AppEngineCloudTraceContextUtils.toCloudTraceContext(spanContext);
    assertThat(newCloudTraceContext.getTraceId()).isEqualTo(cloudTraceContext.getTraceId());
    assertThat(newCloudTraceContext.getSpanId()).isEqualTo(cloudTraceContext.getSpanId());
    assertThat(newCloudTraceContext.getTraceMask()).isEqualTo(cloudTraceContext.getTraceMask());
}
Also used : SpanContext(io.opencensus.trace.SpanContext) CloudTraceContext(com.google.apphosting.api.CloudTraceContext) Test(org.junit.Test)

Example 3 with CloudTraceContext

use of com.google.apphosting.api.CloudTraceContext in project instrumentation-java by census-instrumentation.

the class AppEngineCloudTraceContextUtilsTest method toFromNotSampledCloudTraceContext.

@Test
public void toFromNotSampledCloudTraceContext() {
    CloudTraceContext cloudTraceContext = new CloudTraceContext(// fae1c6346b9cf9a272cb6504b5a10dcc/123456789.
    new byte[] { (byte) 0x09, (byte) 0xa2, (byte) 0xf9, (byte) 0x9c, (byte) 0x6b, (byte) 0x34, (byte) 0xc6, (byte) 0xe1, (byte) 0xfa, (byte) 0x11, (byte) 0xcc, (byte) 0x0d, (byte) 0xa1, (byte) 0xb5, (byte) 0x04, (byte) 0x65, (byte) 0xcb, (byte) 0x72 }, Long.MIN_VALUE, // Trace disabled.
    0L);
    SpanContext spanContext = AppEngineCloudTraceContextUtils.fromCloudTraceContext(cloudTraceContext);
    assertThat(spanContext).isEqualTo(SpanContext.create(TraceId.fromLowerBase16("fae1c6346b9cf9a272cb6504b5a10dcc"), SpanId.fromLowerBase16("8000000000000000"), TraceOptions.builder().setIsSampled(false).build()));
    // CloudTraceContext does not implement equals, so need to check every argument.
    assertThat(cloudTraceContextEquals(AppEngineCloudTraceContextUtils.toCloudTraceContext(spanContext), cloudTraceContext)).isTrue();
}
Also used : SpanContext(io.opencensus.trace.SpanContext) CloudTraceContext(com.google.apphosting.api.CloudTraceContext) Test(org.junit.Test)

Aggregations

CloudTraceContext (com.google.apphosting.api.CloudTraceContext)3 SpanContext (io.opencensus.trace.SpanContext)2 Test (org.junit.Test)2 ByteBuffer (java.nio.ByteBuffer)1