-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(worker): add tracing to bucket, when parent context is present (#9)
- Loading branch information
1 parent
2c45d52
commit 494bc2d
Showing
9 changed files
with
401 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
dist | ||
coverage | ||
.dev.vars* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.