• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016-17, 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.trace;
18 
19 import java.util.Arrays;
20 import javax.annotation.Nullable;
21 import javax.annotation.concurrent.Immutable;
22 
23 /**
24  * A class that represents a span context. A span context contains the state that must propagate to
25  * child {@link Span}s and across process boundaries. It contains the identifiers (a {@link TraceId
26  * trace_id} and {@link SpanId span_id}) associated with the {@link Span} and a set of {@link
27  * TraceOptions options}.
28  *
29  * @since 0.5
30  */
31 @Immutable
32 public final class SpanContext {
33   private static final Tracestate TRACESTATE_DEFAULT = Tracestate.builder().build();
34   private final TraceId traceId;
35   private final SpanId spanId;
36   private final TraceOptions traceOptions;
37   private final Tracestate tracestate;
38 
39   /**
40    * The invalid {@code SpanContext}.
41    *
42    * @since 0.5
43    */
44   public static final SpanContext INVALID =
45       new SpanContext(TraceId.INVALID, SpanId.INVALID, TraceOptions.DEFAULT, TRACESTATE_DEFAULT);
46 
47   /**
48    * Creates a new {@code SpanContext} with the given identifiers and options.
49    *
50    * @param traceId the trace identifier of the span context.
51    * @param spanId the span identifier of the span context.
52    * @param traceOptions the trace options for the span context.
53    * @return a new {@code SpanContext} with the given identifiers and options.
54    * @deprecated use {@link #create(TraceId, SpanId, TraceOptions, Tracestate)}.
55    */
56   @Deprecated
create(TraceId traceId, SpanId spanId, TraceOptions traceOptions)57   public static SpanContext create(TraceId traceId, SpanId spanId, TraceOptions traceOptions) {
58     return create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT);
59   }
60 
61   /**
62    * Creates a new {@code SpanContext} with the given identifiers and options.
63    *
64    * @param traceId the trace identifier of the span context.
65    * @param spanId the span identifier of the span context.
66    * @param traceOptions the trace options for the span context.
67    * @param tracestate the trace state for the span context.
68    * @return a new {@code SpanContext} with the given identifiers and options.
69    * @since 0.16
70    */
create( TraceId traceId, SpanId spanId, TraceOptions traceOptions, Tracestate tracestate)71   public static SpanContext create(
72       TraceId traceId, SpanId spanId, TraceOptions traceOptions, Tracestate tracestate) {
73     return new SpanContext(traceId, spanId, traceOptions, tracestate);
74   }
75 
76   /**
77    * Returns the trace identifier associated with this {@code SpanContext}.
78    *
79    * @return the trace identifier associated with this {@code SpanContext}.
80    * @since 0.5
81    */
getTraceId()82   public TraceId getTraceId() {
83     return traceId;
84   }
85 
86   /**
87    * Returns the span identifier associated with this {@code SpanContext}.
88    *
89    * @return the span identifier associated with this {@code SpanContext}.
90    * @since 0.5
91    */
getSpanId()92   public SpanId getSpanId() {
93     return spanId;
94   }
95 
96   /**
97    * Returns the {@code TraceOptions} associated with this {@code SpanContext}.
98    *
99    * @return the {@code TraceOptions} associated with this {@code SpanContext}.
100    * @since 0.5
101    */
getTraceOptions()102   public TraceOptions getTraceOptions() {
103     return traceOptions;
104   }
105 
106   /**
107    * Returns the {@code Tracestate} associated with this {@code SpanContext}.
108    *
109    * @return the {@code Tracestate} associated with this {@code SpanContext}.
110    * @since 0.5
111    */
getTracestate()112   public Tracestate getTracestate() {
113     return tracestate;
114   }
115 
116   /**
117    * Returns true if this {@code SpanContext} is valid.
118    *
119    * @return true if this {@code SpanContext} is valid.
120    * @since 0.5
121    */
isValid()122   public boolean isValid() {
123     return traceId.isValid() && spanId.isValid();
124   }
125 
126   @Override
equals(@ullable Object obj)127   public boolean equals(@Nullable Object obj) {
128     if (obj == this) {
129       return true;
130     }
131 
132     if (!(obj instanceof SpanContext)) {
133       return false;
134     }
135 
136     SpanContext that = (SpanContext) obj;
137     return traceId.equals(that.traceId)
138         && spanId.equals(that.spanId)
139         && traceOptions.equals(that.traceOptions);
140   }
141 
142   @Override
hashCode()143   public int hashCode() {
144     return Arrays.hashCode(new Object[] {traceId, spanId, traceOptions});
145   }
146 
147   @Override
toString()148   public String toString() {
149     return "SpanContext{traceId="
150         + traceId
151         + ", spanId="
152         + spanId
153         + ", traceOptions="
154         + traceOptions
155         + "}";
156   }
157 
SpanContext( TraceId traceId, SpanId spanId, TraceOptions traceOptions, Tracestate tracestate)158   private SpanContext(
159       TraceId traceId, SpanId spanId, TraceOptions traceOptions, Tracestate tracestate) {
160     this.traceId = traceId;
161     this.spanId = spanId;
162     this.traceOptions = traceOptions;
163     this.tracestate = tracestate;
164   }
165 }
166