forked from evanderkoogh/otel-cf-workers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
span.ts
193 lines (170 loc) · 4.67 KB
/
span.ts
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
import {
SpanContext,
Link,
SpanKind,
TimeInput,
Exception,
Attributes,
HrTime,
Span,
SpanStatus,
SpanStatusCode,
AttributeValue,
} from '@opentelemetry/api'
import {
hrTimeDuration,
InstrumentationLibrary,
isAttributeKey,
isAttributeValue,
isTimeInput,
sanitizeAttributes,
} from '@opentelemetry/core'
import { IResource } from '@opentelemetry/resources'
import { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base'
import { SemanticAttributes } from '@opentelemetry/semantic-conventions'
type OnSpanEnd = (span: Span) => void
interface SpanInit {
attributes: unknown
name: string
onEnd: OnSpanEnd
resource: IResource
spanContext: SpanContext
links?: Link[]
parentSpanId?: string
spanKind?: SpanKind
startTime?: TimeInput
}
function transformExceptionAttributes(exception: Exception): Attributes {
const attributes: Attributes = {}
if (typeof exception === 'string') {
attributes[SemanticAttributes.EXCEPTION_MESSAGE] = exception
} else {
if (exception.code) {
attributes[SemanticAttributes.EXCEPTION_TYPE] = exception.code.toString()
} else if (exception.name) {
attributes[SemanticAttributes.EXCEPTION_TYPE] = exception.name
}
if (exception.message) {
attributes[SemanticAttributes.EXCEPTION_MESSAGE] = exception.message
}
if (exception.stack) {
attributes[SemanticAttributes.EXCEPTION_STACKTRACE] = exception.stack
}
}
return attributes
}
function millisToHr(millis: number): HrTime {
return [Math.trunc(millis / 1000), (millis % 1000) * 1e6]
}
function getHrTime(input?: TimeInput): HrTime {
const now = Date.now()
if (!input) {
return millisToHr(now)
} else if (input instanceof Date) {
return millisToHr(input.getTime())
} else if (typeof input === 'number') {
//TODO: do something with performance.now something
return millisToHr(input)
} else if (Array.isArray(input)) {
return input
}
const v: never = input
throw new Error(`unreachable value: ${JSON.stringify(v)}`)
}
export class SpanImpl implements Span, ReadableSpan {
name: string
private readonly _spanContext: SpanContext
private readonly onEnd: OnSpanEnd
readonly parentSpanId?: string
readonly kind: SpanKind
readonly attributes: Attributes
status: SpanStatus = {
code: SpanStatusCode.UNSET,
}
endTime: HrTime = [0, 0]
private _duration: HrTime = [0, 0]
readonly startTime: HrTime
readonly events: TimedEvent[] = []
readonly links: Link[]
readonly resource: IResource
instrumentationLibrary: InstrumentationLibrary = { name: '@microlabs/otel-cf-workers' }
private _ended: boolean = false
private _droppedAttributesCount: number = 0
private _droppedEventsCount: number = 0
private _droppedLinksCount: number = 0
constructor(init: SpanInit) {
this.name = init.name
this._spanContext = init.spanContext
this.parentSpanId = init.parentSpanId
this.kind = init.spanKind || SpanKind.INTERNAL
this.attributes = sanitizeAttributes(init.attributes)
this.startTime = getHrTime(init.startTime)
this.links = init.links || []
this.resource = init.resource
this.onEnd = init.onEnd
}
spanContext(): SpanContext {
return this._spanContext
}
setAttribute(key: string, value?: AttributeValue): this {
if (isAttributeKey(key) && isAttributeValue(value)) {
this.attributes[key] = value
}
return this
}
setAttributes(attributes: Attributes): this {
for (const [key, value] of Object.entries(attributes)) {
this.setAttribute(key, value)
}
return this
}
addEvent(name: string, attributesOrStartTime?: Attributes | TimeInput, startTime?: TimeInput): this {
if (isTimeInput(attributesOrStartTime)) {
startTime = attributesOrStartTime
attributesOrStartTime = undefined
}
const attributes = sanitizeAttributes(attributesOrStartTime)
const time = getHrTime(startTime)
this.events.push({ name, attributes, time })
return this
}
setStatus(status: SpanStatus): this {
this.status = status
return this
}
updateName(name: string): this {
this.name = name
return this
}
end(endTime?: TimeInput): void {
if (this._ended) {
return
}
this._ended = true
this.endTime = getHrTime(endTime)
this._duration = hrTimeDuration(this.startTime, this.endTime)
this.onEnd(this)
}
isRecording(): boolean {
return !this._ended
}
recordException(exception: Exception, time?: TimeInput): void {
const attributes = transformExceptionAttributes(exception)
this.addEvent('exception', attributes, time)
}
get duration(): HrTime {
return this._duration
}
get ended(): boolean {
return this._ended
}
get droppedAttributesCount(): number {
return this._droppedAttributesCount
}
get droppedEventsCount(): number {
return this._droppedEventsCount
}
get droppedLinksCount(): number {
return this._droppedLinksCount
}
}