Skip to content

Commit

Permalink
feat(worker): add tracing to bucket, when parent context is present (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahhoward authored Jan 7, 2025
1 parent 2c45d52 commit 494bc2d
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
dist
coverage
.dev.vars*
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "src/worker.js",
"type": "module",
"scripts": {
"build": "esbuild src/worker.js --bundle --format=esm --outfile=dist/worker.mjs",
"build": "esbuild src/worker.js --bundle --format=esm --external:node:buffer --external:node:events --external:node:async_hooks --outfile=dist/worker.mjs",
"lint": "standard",
"test": "npm run build && entail"
},
Expand All @@ -25,8 +25,12 @@
"uint8arrays": "^5.1.0"
},
"dependencies": {
"@microlabs/otel-cf-workers": "1.0.0-rc.49",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/sdk-trace-base": "^1.30.0",
"@web3-storage/gateway-lib": "^5.0.1",
"@web3-storage/public-bucket": "^1.2.0"
"@web3-storage/public-bucket": "^1.2.0",
"typescript": "^5.7.2"
},
"repository": {
"type": "git",
Expand Down
202 changes: 202 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions src/tracebucket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// eslint-disable-next-line
import * as BucketAPI from '@web3-storage/public-bucket'
import { trace, context, SpanStatusCode } from '@opentelemetry/api'

/**
* @template {unknown[]} A
* @template {*} T
* @template {*} This
* @param {string} spanName
* @param {(this: This, ...args: A) => Promise<T>} fn
* @param {This} [thisParam]
*/
const withSimpleSpan = (spanName, fn, thisParam) =>
/**
* @param {A} args
*/
async (...args) => {
const tracer = trace.getTracer('public-r2-bucket')
const span = tracer.startSpan(spanName)
const ctx = trace.setSpan(context.active(), span)

try {
const result = await context.with(ctx, fn, thisParam, ...args)
span.setStatus({ code: SpanStatusCode.OK })
span.end()
return result
} catch (err) {
if (err instanceof Error) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: err.message
})
} else {
span.setStatus({
code: SpanStatusCode.ERROR
})
}
span.end()
throw err
}
}

/** @implements {BucketAPI.Bucket} */
export class TraceBucket {
#bucket

/**
*
* @param {BucketAPI.Bucket} bucket
*/
constructor (bucket) {
this.#bucket = bucket
}

/** @param {string} key */
head (key) {
return withSimpleSpan('bucket.head', this.#bucket.head, this.#bucket)(key)
}

/**
* @param {string} key
* @param {BucketAPI.GetOptions} [options]
*/
get (key, options) {
return withSimpleSpan('bucket.get', this.#bucket.get, this.#bucket)(key, options)
}
}
5 changes: 4 additions & 1 deletion src/worker.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { R2Bucket } from '@cloudflare/workers-types'
import { Environment as MiddlewareEnvironment } from '@web3-storage/gateway-lib'

export { R2Bucket }

export interface Environment {
export interface Environment extends MiddlewareEnvironment {
BUCKET: R2Bucket
FF_TELEMETRY_ENABLED: string
HONEYCOMB_API_KEY: string
}

export declare function handler (request: Request, env: Environment): Promise<Response>
Loading

0 comments on commit 494bc2d

Please sign in to comment.