Skip to content

Commit 4120611

Browse files
authored
Merge branch 'main' into fix-durable-object
2 parents 0f24200 + 9883d45 commit 4120611

File tree

8 files changed

+70
-20
lines changed

8 files changed

+70
-20
lines changed

.changeset/huge-phones-work.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@microlabs/otel-cf-workers': patch
3+
---
4+
5+
Make sure spans are ended on network errors

.changeset/new-coins-run.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@microlabs/otel-cf-workers': patch
3+
---
4+
5+
Forcefully end spans that aren't ended by custom instrumentation when flushing

.changeset/pre.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626
"healthy-dolphins-yawn",
2727
"heavy-carrots-care",
2828
"hot-dodos-burn",
29+
"huge-phones-work",
2930
"kind-ladybugs-swim",
3031
"little-goats-occur",
3132
"lovely-pets-impress",
3233
"lucky-apples-clean",
3334
"mighty-timers-float",
3435
"nervous-kids-guess",
3536
"nervous-poets-matter",
37+
"new-coins-run",
3638
"new-gifts-repair",
3739
"new-horses-burn",
3840
"new-lions-collect",
@@ -46,8 +48,11 @@
4648
"proud-bugs-teach",
4749
"rotten-radios-hope",
4850
"rude-bottles-jog",
51+
"shaky-lizards-help",
4952
"silent-flies-sell",
53+
"smart-candies-grab",
5054
"smooth-vans-know",
55+
"spotty-women-sleep",
5156
"strange-moose-fetch",
5257
"strong-monkeys-film",
5358
"tasty-ghosts-tickle",

.changeset/shaky-lizards-help.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@microlabs/otel-cf-workers': patch
3+
---
4+
5+
Add user-agent header to the SpanExporter

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
# @microlabs/otel-cf-workers
22

3+
## 1.0.0-rc.51
4+
5+
### Patch Changes
6+
7+
- d20322d: Make sure spans are ended on network errors
8+
- 4b89506: Forcefully end spans that aren't ended by custom instrumentation when flushing
9+
- 169d92a: Add user-agent header to the SpanExporter
10+
11+
## 1.0.0-rc.50
12+
13+
### Minor Changes
14+
15+
- 19af336: Implement a complete rework of the internals to be more predictable and make it easier to implement more instrumentation on top of it.
16+
17+
### Patch Changes
18+
19+
- fcab308: Upgrade dependencies
20+
321
## 1.0.0-rc.49
422

523
### Minor Changes

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "@microlabs/otel-cf-workers",
3-
"version": "1.0.0-rc.49",
3+
"version": "1.0.0-rc.51",
44
"module": "./dist/index.js",
55
"type": "module",
66
"types": "./dist/index.d.ts",
77
"exports": "./dist/index.js",
8+
89
"scripts": {
910
"clean": "rimraf ./dist versions.json",
1011
"format": "prettier --ignore-unknown --write .",

src/instrumentation/fetch.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
Attributes,
88
Context,
99
Span,
10+
Exception,
11+
SpanStatusCode,
1012
} from '@opentelemetry/api'
1113
import { getActiveConfig } from '../config.js'
1214
import { wrap } from '../wrap.js'
@@ -180,21 +182,28 @@ export function instrumentClientFetch(
180182
const method = request.method.toUpperCase()
181183
const spanName = typeof attrs?.['name'] === 'string' ? attrs?.['name'] : `fetch ${method} ${host}`
182184
const promise = tracer.startActiveSpan(spanName, options, async (span) => {
183-
const includeTraceContext =
184-
typeof config.includeTraceContext === 'function'
185-
? config.includeTraceContext(request)
186-
: config.includeTraceContext
187-
if (includeTraceContext ?? true) {
188-
propagation.inject(api_context.active(), request.headers, {
189-
set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)),
190-
})
185+
try {
186+
const includeTraceContext =
187+
typeof config.includeTraceContext === 'function'
188+
? config.includeTraceContext(request)
189+
: config.includeTraceContext
190+
if (includeTraceContext ?? true) {
191+
propagation.inject(api_context.active(), request.headers, {
192+
set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)),
193+
})
194+
}
195+
span.setAttributes(gatherRequestAttributes(request))
196+
if (request.cf) span.setAttributes(gatherOutgoingCfAttributes(request.cf))
197+
const response = await Reflect.apply(target, thisArg, [request])
198+
span.setAttributes(gatherResponseAttributes(response))
199+
return response
200+
} catch (error: unknown) {
201+
span.recordException(error as Exception)
202+
span.setStatus({ code: SpanStatusCode.ERROR })
203+
throw error
204+
} finally {
205+
span.end()
191206
}
192-
span.setAttributes(gatherRequestAttributes(request))
193-
if (request.cf) span.setAttributes(gatherOutgoingCfAttributes(request.cf))
194-
const response = await Reflect.apply(target, thisArg, [request])
195-
span.setAttributes(gatherResponseAttributes(response))
196-
span.end()
197-
return response
198207
})
199208
return promise
200209
},

src/spanprocessor.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ class TraceState {
5353

5454
async flush(): Promise<void> {
5555
if (this.unexportedSpans.length > 0) {
56-
this.sample()
57-
const finishedSpans = this.unexportedSpans.filter((span) => !this.isSpanInProgress(span))
58-
this.unexportedSpans = this.unexportedSpans.filter((span) => this.isSpanInProgress(span))
59-
if (finishedSpans.length > 0) {
60-
this.exportPromises.push(this.exportSpans(finishedSpans))
56+
const unfinishedSpans = this.unexportedSpans.filter((span) => this.isSpanInProgress(span)) as unknown as Span[]
57+
for (const span of unfinishedSpans) {
58+
console.log(`Span ${span.spanContext().spanId} was not ended properly`)
59+
span.end()
6160
}
61+
this.sample()
62+
this.exportPromises.push(this.exportSpans(this.unexportedSpans))
63+
this.unexportedSpans = []
6264
}
6365
if (this.exportPromises.length > 0) {
6466
await Promise.allSettled(this.exportPromises)

0 commit comments

Comments
 (0)