-
Notifications
You must be signed in to change notification settings - Fork 8
/
EmbraceSpanImpl.kt
194 lines (167 loc) · 7.57 KB
/
EmbraceSpanImpl.kt
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
package io.embrace.android.embracesdk.internal.spans
import io.embrace.android.embracesdk.arch.schema.EmbraceAttributeKey
import io.embrace.android.embracesdk.arch.schema.FixedAttribute
import io.embrace.android.embracesdk.internal.clock.millisToNanos
import io.embrace.android.embracesdk.internal.clock.nanosToMillis
import io.embrace.android.embracesdk.internal.clock.normalizeTimestampAsMillis
import io.embrace.android.embracesdk.internal.payload.Span
import io.embrace.android.embracesdk.internal.payload.toNewPayload
import io.embrace.android.embracesdk.spans.EmbraceSpan
import io.embrace.android.embracesdk.spans.EmbraceSpanEvent
import io.embrace.android.embracesdk.spans.EmbraceSpanEvent.Companion.inputsValid
import io.embrace.android.embracesdk.spans.ErrorCode
import io.embrace.android.embracesdk.spans.PersistableEmbraceSpan
import io.opentelemetry.api.common.Attributes
import io.opentelemetry.sdk.common.Clock
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicReference
internal class EmbraceSpanImpl(
private val spanBuilder: EmbraceSpanBuilder,
private val openTelemetryClock: Clock,
private val spanRepository: SpanRepository
) : PersistableEmbraceSpan {
private val startedSpan: AtomicReference<io.opentelemetry.api.trace.Span?> = AtomicReference(null)
private var spanStartTimeMs: Long? = null
private var spanEndTimeMs: Long? = null
private var status = Span.Status.UNSET
private val events = ConcurrentLinkedQueue<EmbraceSpanEvent>()
private val schemaAttributes = spanBuilder.fixedAttributes.associate {
it.toEmbraceKeyValuePair()
}.toMutableMap()
private val attributes = ConcurrentHashMap<String, String>()
// size for ConcurrentLinkedQueues is not a constant operation, so it could be subject to race conditions
// do the bookkeeping separately so we don't have to worry about this
private val eventCount = AtomicInteger(0)
override val parent: EmbraceSpan? = spanBuilder.parent
override val traceId: String?
get() = startedSpan.get()?.spanContext?.traceId
override val spanId: String?
get() = startedSpan.get()?.spanContext?.spanId
override val isRecording: Boolean
get() = startedSpan.get()?.isRecording == true
override fun start(startTimeMs: Long?): Boolean {
return if (startedSpan.get() != null) {
false
} else {
var successful: Boolean
val attemptedStartTimeMs = startTimeMs ?: openTelemetryClock.now().nanosToMillis()
synchronized(startedSpan) {
startedSpan.set(spanBuilder.startSpan(attemptedStartTimeMs))
successful = startedSpan.get() != null
}
if (successful) {
spanStartTimeMs = attemptedStartTimeMs
spanRepository.trackStartedSpan(this)
}
return successful
}
}
override fun stop(errorCode: ErrorCode?, endTimeMs: Long?): Boolean {
return if (!isRecording) {
false
} else {
var successful = false
val attemptedEndTimeMs = endTimeMs ?: openTelemetryClock.now().nanosToMillis()
synchronized(startedSpan) {
startedSpan.get()?.let { spanToStop ->
allAttributes().forEach { attribute ->
spanToStop.setAttribute(attribute.key, attribute.value)
}
events.forEach { event ->
val eventAttributes = if (event.attributes.isNotEmpty()) {
Attributes.builder().fromMap(event.attributes).build()
} else {
Attributes.empty()
}
spanToStop.addEvent(
event.name,
eventAttributes,
event.timestampNanos,
TimeUnit.NANOSECONDS
)
}
spanToStop.endSpan(errorCode, attemptedEndTimeMs)
successful = !isRecording
}
}
if (successful) {
status = if (errorCode != null) {
Span.Status.ERROR
} else {
Span.Status.OK
}
spanEndTimeMs = attemptedEndTimeMs
spanId?.let { spanRepository.trackedSpanStopped(it) }
}
return successful
}
}
override fun addEvent(name: String, timestampMs: Long?, attributes: Map<String, String>?): Boolean {
if (eventCount.get() < MAX_EVENT_COUNT && inputsValid(name, attributes)) {
val newEvent = EmbraceSpanEvent.create(
name = name,
timestampMs = timestampMs?.normalizeTimestampAsMillis() ?: openTelemetryClock.now().nanosToMillis(),
attributes = attributes
)
synchronized(eventCount) {
if (eventCount.get() < MAX_EVENT_COUNT && isRecording) {
events.add(newEvent)
eventCount.incrementAndGet()
return true
}
}
}
return false
}
override fun addAttribute(key: String, value: String): Boolean {
if (attributes.size < MAX_ATTRIBUTE_COUNT && attributeValid(key, value)) {
synchronized(attributes) {
if (attributes.size < MAX_ATTRIBUTE_COUNT && isRecording) {
attributes[key] = value
return true
}
}
}
return false
}
override fun snapshot(): Span? {
return if (canSnapshot()) {
Span(
traceId = traceId,
spanId = spanId,
parentSpanId = parent?.spanId,
name = spanBuilder.spanName,
startTimeUnixNano = spanStartTimeMs?.millisToNanos(),
endTimeUnixNano = spanEndTimeMs?.millisToNanos(),
status = status,
events = events.map(EmbraceSpanEvent::toNewPayload),
attributes = allAttributes().toNewPayload()
)
} else {
null
}
}
override fun hasEmbraceAttribute(fixedAttribute: FixedAttribute): Boolean =
allAttributes().hasFixedAttribute(fixedAttribute)
override fun getAttribute(key: EmbraceAttributeKey): String? = allAttributes()[key.name]
override fun removeCustomAttribute(key: String): Boolean = attributes.remove(key) != null
private fun allAttributes(): Map<String, String> = attributes + schemaAttributes
private fun canSnapshot(): Boolean = spanId != null && spanStartTimeMs != null
internal fun wrappedSpan(): io.opentelemetry.api.trace.Span? = startedSpan.get()
companion object {
internal const val MAX_NAME_LENGTH = 50
internal const val MAX_EVENT_COUNT = 10
internal const val MAX_ATTRIBUTE_COUNT = 50
internal const val MAX_ATTRIBUTE_KEY_LENGTH = 50
internal const val MAX_ATTRIBUTE_VALUE_LENGTH = 200
internal fun attributeValid(key: String, value: String) =
key.length <= MAX_ATTRIBUTE_KEY_LENGTH && value.length <= MAX_ATTRIBUTE_VALUE_LENGTH
internal fun EmbraceSpan.setFixedAttribute(fixedAttribute: FixedAttribute): EmbraceSpan {
addAttribute(fixedAttribute.key.name, fixedAttribute.value)
return this
}
}
}