-
Notifications
You must be signed in to change notification settings - Fork 28
/
SpanContext.scala
285 lines (253 loc) · 7.93 KB
/
SpanContext.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Copyright 2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.typelevel.otel4s.trace
import cats.Hash
import cats.Show
import cats.syntax.show._
import scodec.bits.ByteVector
/** A span context contains the state that must propagate to child spans and
* across process boundaries.
*
* It contains the identifiers (a `trace_id` and `span_id`) associated with the
* span and a set of flags (currently only whether the context is sampled or
* not), as well as the remote flag.
*
* @see
* [[https://opentelemetry.io/docs/specs/otel/trace/api/#spancontext]]
*/
sealed trait SpanContext {
/** Returns the trace identifier associated with this [[SpanContext]] as
* 16-byte vector.
*/
def traceId: ByteVector
/** Returns the trace identifier associated with this [[SpanContext]] as 32
* character lowercase hex String.
*/
def traceIdHex: String
/** Returns the span identifier associated with this [[SpanContext]] as 8-byte
* vector.
*/
def spanId: ByteVector
/** Returns the span identifier associated with this [[SpanContext]] as 16
* character lowercase hex String.
*/
def spanIdHex: String
/** Returns details about the trace associated with this [[SpanContext]] as an
* 8-bit field.
*/
def traceFlags: TraceFlags
/** Returns the trace state associated with this [[SpanContext]].
*/
def traceState: TraceState
/** Return `true` if this [[SpanContext]] is sampled.
*/
final def isSampled: Boolean =
traceFlags.isSampled
/** Returns `true` if this [[SpanContext]] is valid.
*/
def isValid: Boolean
/** Returns `true` if this [[SpanContext]] was propagated from a remote
* parent.
*/
def isRemote: Boolean
override final def hashCode(): Int =
Hash[SpanContext].hash(this)
override final def equals(obj: Any): Boolean =
obj match {
case other: SpanContext => Hash[SpanContext].eqv(this, other)
case _ => false
}
override final def toString: String =
Show[SpanContext].show(this)
}
object SpanContext {
object TraceId {
val Bytes: Int = 16
val HexLength: Int = Bytes * 2
val InvalidHex: String = "0" * HexLength
val Invalid: ByteVector = ByteVector.fromValidHex(InvalidHex)
def fromLongs(hi: Long, lo: Long): ByteVector =
ByteVector.fromLong(hi, 8) ++ ByteVector.fromLong(lo, 8)
/** Creates trace id from the hex string.
*
* Returns `None` when the input isn't a valid hex or the id is invalid.
*/
def fromHex(hex: String): Option[ByteVector] =
ByteVector.fromHex(hex).filter(bv => isValid(bv))
/** Checks whether a trace id has correct length and is not the invalid id.
*/
def isValid(id: ByteVector): Boolean =
(id.length == Bytes) && (id != Invalid)
}
object SpanId {
val Bytes: Int = 8
val HexLength: Int = Bytes * 2
val InvalidHex: String = "0" * HexLength
val Invalid: ByteVector = ByteVector.fromValidHex(InvalidHex)
def fromLong(value: Long): ByteVector =
ByteVector.fromLong(value, 8)
/** Creates span id from the hex string.
*
* Returns `None` when the input isn't a valid hex or the id is invalid.
*/
def fromHex(hex: String): Option[ByteVector] =
ByteVector.fromHex(hex).filter(bv => isValid(bv))
/** Checks whether a span id has correct length and is not the invalid id.
*/
def isValid(id: ByteVector): Boolean =
(id.length == Bytes) && (id != Invalid)
}
val invalid: SpanContext =
Impl(
traceId = TraceId.Invalid,
traceIdHex = TraceId.Invalid.toHex,
spanId = SpanId.Invalid,
spanIdHex = SpanId.Invalid.toHex,
traceFlags = TraceFlags.Default,
traceState = TraceState.empty,
isRemote = false,
isValid = false
)
/** Creates a new [[SpanContext]] with the given identifiers and options.
*
* If the `traceId` or the `spanId` are invalid (ie. do not conform to the
* requirements for hexadecimal ids of the appropriate lengths), both will be
* replaced with the standard "invalid" versions (i.e. all '0's).
*
* @param traceId
* the trace identifier of the span context
*
* @param spanId
* the span identifier of the span context
*
* @param traceFlags
* the trace flags of the span context
*
* @param traceState
* the trace state of the span context
*
* @param remote
* whether the span is propagated from the remote parent or not
*/
def apply(
traceId: ByteVector,
spanId: ByteVector,
traceFlags: TraceFlags,
traceState: TraceState,
remote: Boolean
): SpanContext =
if (TraceId.isValid(traceId) && SpanId.isValid(spanId)) {
createInternal(
traceId = traceId,
spanId = spanId,
traceFlags = traceFlags,
traceState = traceState,
remote = remote,
isValid = true
)
} else {
createInternal(
traceId = TraceId.Invalid,
spanId = SpanId.Invalid,
traceFlags = traceFlags,
traceState = traceState,
remote = remote,
isValid = false
)
}
implicit val spanContextHash: Hash[SpanContext] = {
implicit val byteVectorHash: Hash[ByteVector] = Hash.fromUniversalHashCode
Hash.by { ctx =>
(
ctx.traceId,
ctx.spanId,
ctx.traceFlags,
ctx.traceState,
ctx.isValid,
ctx.isRemote
)
}
}
implicit val spanContextShow: Show[SpanContext] =
Show.show { ctx =>
show"SpanContext{" +
show"traceId=${ctx.traceIdHex}, " +
show"spanId=${ctx.spanIdHex}, " +
show"traceFlags=${ctx.traceFlags}, " +
show"traceState=${ctx.traceState}, " +
show"remote=${ctx.isRemote}, " +
show"valid=${ctx.isValid}}"
}
/** Creates a new [[SpanContext]] with the given identifiers and options.
*
* If the id validation isn't skipped and the `traceId` or the `spanId` are
* invalid (ie. do not conform to the requirements for hexadecimal ids of the
* appropriate lengths), both will be replaced with the standard "invalid"
* versions (i.e. all '0's).
*
* '''Note''': the method is for the internal use only. It is not supposed to
* be publicly available.
*
* @see
* [[apply]]
*
* @param traceId
* the trace identifier of the span context
*
* @param spanId
* the span identifier of the span context
*
* @param traceFlags
* the trace flags of the span context
*
* @param traceState
* the trace state of the span context
*
* @param remote
* whether the span is propagated from the remote parent or not
*
* @param isValid
* whether the span is valid or not
*/
private[otel4s] def createInternal(
traceId: ByteVector,
spanId: ByteVector,
traceFlags: TraceFlags,
traceState: TraceState,
remote: Boolean,
isValid: Boolean
): SpanContext =
Impl(
traceId = traceId,
traceIdHex = traceId.toHex,
spanId = spanId,
spanIdHex = spanId.toHex,
traceFlags = traceFlags,
traceState = traceState,
isRemote = remote,
isValid = isValid
)
private final case class Impl(
traceId: ByteVector,
traceIdHex: String,
spanId: ByteVector,
spanIdHex: String,
traceFlags: TraceFlags,
traceState: TraceState,
isRemote: Boolean,
isValid: Boolean
) extends SpanContext
}