1 /*
2  * Copyright 2018, OpenCensus Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.opencensus.contrib.spring.sleuth.v1x;
18 
19 import io.opencensus.common.ExperimentalApi;
20 import io.opencensus.trace.Annotation;
21 import io.opencensus.trace.AttributeValue;
22 import io.opencensus.trace.EndSpanOptions;
23 import io.opencensus.trace.Link;
24 import io.opencensus.trace.Span;
25 import io.opencensus.trace.SpanContext;
26 import io.opencensus.trace.SpanId;
27 import io.opencensus.trace.TraceId;
28 import io.opencensus.trace.TraceOptions;
29 import java.nio.ByteBuffer;
30 import java.util.EnumSet;
31 import java.util.Map;
32 
33 /**
34  * Implementaion of Span that is created from a Sleuth Span.
35  *
36  * @since 0.16
37  */
38 @ExperimentalApi
39 public class OpenCensusSleuthSpan extends Span {
40 
41   private static final EnumSet<Options> recordOptions = EnumSet.of(Options.RECORD_EVENTS);
42   private static final EnumSet<Options> notRecordOptions = EnumSet.noneOf(Options.class);
43 
44   private static final TraceOptions sampledOptions =
45       TraceOptions.builder().setIsSampled(true).build();
46   private static final TraceOptions notSampledOptions =
47       TraceOptions.builder().setIsSampled(false).build();
48 
OpenCensusSleuthSpan(org.springframework.cloud.sleuth.Span span)49   OpenCensusSleuthSpan(org.springframework.cloud.sleuth.Span span) {
50     super(
51         fromSleuthSpan(span),
52         Boolean.TRUE.equals(span.isExportable()) ? recordOptions : notRecordOptions);
53   }
54 
55   @Override
addAnnotation(String s, Map<String, AttributeValue> map)56   public void addAnnotation(String s, Map<String, AttributeValue> map) {}
57 
58   @Override
addAnnotation(Annotation annotation)59   public void addAnnotation(Annotation annotation) {}
60 
61   @Override
addLink(Link link)62   public void addLink(Link link) {}
63 
64   @Override
end(EndSpanOptions endSpanOptions)65   public void end(EndSpanOptions endSpanOptions) {}
66 
67   // TODO: upgrade to new SpanContext.create() once it has been released.
68   @SuppressWarnings("deprecation")
fromSleuthSpan(org.springframework.cloud.sleuth.Span span)69   private static SpanContext fromSleuthSpan(org.springframework.cloud.sleuth.Span span) {
70     return SpanContext.create(
71         TraceId.fromBytes(
72             ByteBuffer.allocate(TraceId.SIZE)
73                 .putLong(span.getTraceIdHigh())
74                 .putLong(span.getTraceId())
75                 .array()),
76         SpanId.fromBytes(ByteBuffer.allocate(SpanId.SIZE).putLong(span.getSpanId()).array()),
77         Boolean.TRUE.equals(span.isExportable()) ? sampledOptions : notSampledOptions);
78   }
79 }
80