-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from Schniz/schniz/support-email-triggering
feat: add support for email triggers
- Loading branch information
Showing
7 changed files
with
117 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
'@microlabs/otel-cf-workers': minor | ||
--- | ||
|
||
add support for `email` handlers | ||
|
||
Example usage: | ||
|
||
```ts | ||
export default { | ||
async email(message, env, ctx) { | ||
// this is running in a trace! | ||
}, | ||
}; | ||
``` |
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
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
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,76 @@ | ||
import { setConfig, type Initialiser } from '../config' | ||
import { wrap } from '../wrap' | ||
import { exportSpans, proxyExecutionContext } from './common' | ||
import { context as api_context, Exception, SpanKind, type SpanOptions, trace } from '@opentelemetry/api' | ||
import { instrumentEnv } from './env' | ||
import { versionAttributes } from './version' | ||
import { | ||
ATTR_FAAS_TRIGGER, | ||
ATTR_MESSAGING_DESTINATION_NAME, | ||
ATTR_RPC_MESSAGE_ID, | ||
} from '@opentelemetry/semantic-conventions/incubating' | ||
|
||
type EmailHandler = EmailExportedHandler | ||
export type EmailHandlerArgs = Parameters<EmailHandler> | ||
|
||
export function createEmailHandler(emailFn: EmailHandler, initialiser: Initialiser): EmailHandler { | ||
const emailHandler: ProxyHandler<EmailHandler> = { | ||
async apply(target, _thisArg, argArray: Parameters<EmailHandler>): Promise<void> { | ||
const [message, orig_env, orig_ctx] = argArray | ||
const config = initialiser(orig_env as Record<string, unknown>, message) | ||
const env = instrumentEnv(orig_env as Record<string, unknown>) | ||
const { ctx, tracker } = proxyExecutionContext(orig_ctx) | ||
const context = setConfig(config) | ||
|
||
try { | ||
const args: EmailHandlerArgs = [message, env, ctx] | ||
return await api_context.with(context, executeEmailHandler, undefined, target, args) | ||
} catch (error) { | ||
throw error | ||
} finally { | ||
orig_ctx.waitUntil(exportSpans(tracker)) | ||
} | ||
}, | ||
} | ||
return wrap(emailFn, emailHandler) | ||
} | ||
|
||
/** | ||
* Converts the message headers into a record ready to be injected | ||
* as OpenTelemetry attributes | ||
* | ||
* @example | ||
* ```ts | ||
* const headers = new Headers({ "Subject": "Hello!", From: "hello@example.com" }) | ||
* headerAttributes({ headers }) | ||
* // => {"email.header.Subject": "Hello!", "email.header.From": "hello@example.com"} | ||
* ``` | ||
*/ | ||
function headerAttributes(message: { headers: Headers }): Record<string, unknown> { | ||
return Object.fromEntries([...message.headers].map(([key, value]) => [`email.header.${key}`, value] as const)) | ||
} | ||
|
||
async function executeEmailHandler(emailFn: EmailHandler, [message, env, ctx]: EmailHandlerArgs): Promise<void> { | ||
const tracer = trace.getTracer('emailHandler') | ||
const options = { | ||
attributes: { | ||
[ATTR_FAAS_TRIGGER]: 'other', | ||
[ATTR_RPC_MESSAGE_ID]: message.headers.get('Message-Id') ?? undefined, | ||
[ATTR_MESSAGING_DESTINATION_NAME]: message.to, | ||
}, | ||
kind: SpanKind.CONSUMER, | ||
} satisfies SpanOptions | ||
Object.assign(options.attributes!, headerAttributes(message), versionAttributes(env)) | ||
const promise = tracer.startActiveSpan(`emailHandler ${message.to}`, options, async (span) => { | ||
try { | ||
const result = await emailFn(message, env, ctx) | ||
span.end() | ||
return result | ||
} catch (error) { | ||
span.recordException(error as Exception) | ||
span.end() | ||
throw error | ||
} | ||
}) | ||
return promise | ||
} |
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
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
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,9 @@ | ||
import { defineConfig } from 'tsup' | ||
|
||
export default defineConfig({ | ||
entry: ['src/index.ts'], | ||
format: ['cjs', 'esm'], | ||
dts: true, | ||
clean: true, | ||
sourcemap: true, | ||
}) |