Skip to content

Commit c534220

Browse files
authored
Add Telemetry (#49)
* fix: pass all params to context in failureFunction * feat: add workflow telemetry * fix: rm process from hono * fix: add disable telemetry option and change platform to telemetry
1 parent 76f674a commit c534220

21 files changed

+610
-175
lines changed

platforms/astro.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { APIContext, APIRoute } from "astro";
22

3-
import type { PublicServeOptions, WorkflowContext } from "../src";
3+
import { PublicServeOptions, WorkflowContext } from "../src";
44
import { serveBase } from "../src/serve";
5+
import { SDK_TELEMETRY } from "../src/constants";
56

67
export function serve<TInitialPayload = unknown>(
78
routeFunction: (
@@ -13,6 +14,13 @@ export function serve<TInitialPayload = unknown>(
1314
const POST: APIRoute = (apiContext) => {
1415
const { handler } = serveBase<TInitialPayload>(
1516
(workflowContext) => routeFunction(workflowContext, apiContext),
17+
{
18+
sdk: SDK_TELEMETRY,
19+
framework: "astro",
20+
runtime: process.versions.bun
21+
? `bun@${process.versions.bun}/node@${process.version}`
22+
: `node@${process.version}`,
23+
},
1624
options
1725
);
1826

platforms/cloudflare.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { PublicServeOptions, RouteFunction } from "../src";
2+
import { SDK_TELEMETRY } from "../src/constants";
23
import { serveBase } from "../src/serve";
34

45
export type WorkflowBindings = {
@@ -62,10 +63,17 @@ export const serve = <TInitialPayload = unknown>(
6263
): { fetch: (...args: PagesHandlerArgs | WorkersHandlerArgs) => Promise<Response> } => {
6364
const fetch = async (...args: PagesHandlerArgs | WorkersHandlerArgs) => {
6465
const { request, env } = getArgs(args);
65-
const { handler: serveHandler } = serveBase(routeFunction, {
66-
env,
67-
...options,
68-
});
66+
const { handler: serveHandler } = serveBase(
67+
routeFunction,
68+
{
69+
sdk: SDK_TELEMETRY,
70+
framework: "cloudflare",
71+
},
72+
{
73+
env,
74+
...options,
75+
}
76+
);
6977
return await serveHandler(request);
7078
};
7179
return { fetch };

platforms/express.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { WorkflowServeOptions, RouteFunction } from "../src";
2+
import { SDK_TELEMETRY } from "../src/constants";
23
import { serveBase } from "../src/serve";
34
import {
45
Request as ExpressRequest,
@@ -43,7 +44,14 @@ export function serve<TInitialPayload = unknown>(
4344

4445
// create handler
4546
const { handler: serveHandler } = serveBase<TInitialPayload>(
46-
(workflowContext) => routeFunction(workflowContext),
47+
routeFunction,
48+
{
49+
sdk: SDK_TELEMETRY,
50+
framework: "express",
51+
runtime: process.versions.bun
52+
? `bun@${process.versions.bun}/node@${process.version}`
53+
: `node@${process.version}`,
54+
},
4755
{
4856
...options,
4957
useJSONContent: true,

platforms/h3.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defineEventHandler, readRawBody } from "h3";
33
import type { PublicServeOptions, RouteFunction } from "../src";
44
import { serveBase } from "../src/serve";
55
import type { IncomingHttpHeaders } from "node:http";
6+
import { SDK_TELEMETRY } from "../src/constants";
67

78
function transformHeaders(headers: IncomingHttpHeaders): [string, string][] {
89
const formattedHeaders = Object.entries(headers).map(([key, value]) => [
@@ -37,7 +38,17 @@ export const serve = <TInitialPayload = unknown>(
3738
method: "POST",
3839
});
3940

40-
const { handler: serveHandler } = serveBase<TInitialPayload>(routeFunction, options);
41+
const { handler: serveHandler } = serveBase<TInitialPayload>(
42+
routeFunction,
43+
{
44+
sdk: SDK_TELEMETRY,
45+
framework: "h3",
46+
runtime: process.versions.bun
47+
? `bun@${process.versions.bun}/node@${process.version}`
48+
: `node@${process.version}`,
49+
},
50+
options
51+
);
4152
return await serveHandler(request);
4253
});
4354

platforms/hono.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Context } from "hono";
22
import type { PublicServeOptions, RouteFunction } from "../src";
33
import { serveBase } from "../src/serve";
44
import { Variables } from "hono/types";
5+
import { SDK_TELEMETRY } from "../src/constants";
56

67
export type WorkflowBindings = {
78
QSTASH_TOKEN: string;
@@ -32,12 +33,19 @@ export const serve = <
3233
const environment = context.env;
3334
const request = context.req.raw;
3435

35-
const { handler: serveHandler } = serveBase(routeFunction, {
36-
// when hono is used without cf workers, it sends a DebugHTTPServer
37-
// object in `context.env`. don't pass env if this is the case:
38-
env: "QSTASH_TOKEN" in environment ? environment : undefined,
39-
...options,
40-
});
36+
const { handler: serveHandler } = serveBase(
37+
routeFunction,
38+
{
39+
sdk: SDK_TELEMETRY,
40+
framework: "hono",
41+
},
42+
{
43+
// when hono is used without cf workers, it sends a DebugHTTPServer
44+
// object in `context.env`. don't pass env if this is the case:
45+
env: "QSTASH_TOKEN" in environment ? environment : undefined,
46+
...options,
47+
}
48+
);
4149
return await serveHandler(request);
4250
};
4351
return handler;

platforms/nextjs.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { NextApiHandler, NextApiRequest, NextApiResponse } from "next";
44

55
import type { RouteFunction, PublicServeOptions } from "../src";
66
import { serveBase } from "../src/serve";
7+
import { SDK_TELEMETRY } from "../src/constants";
78

89
/**
910
* Serve method to serve a Upstash Workflow in a Nextjs project
@@ -20,6 +21,11 @@ export const serve = <TInitialPayload = unknown>(
2021
): { POST: (request: Request) => Promise<Response> } => {
2122
const { handler: serveHandler } = serveBase<TInitialPayload, Request, Response>(
2223
routeFunction,
24+
{
25+
sdk: SDK_TELEMETRY,
26+
framework: "nextjs",
27+
runtime: `node@${process.version}`,
28+
},
2329
options
2430
);
2531

@@ -34,7 +40,17 @@ export const servePagesRouter = <TInitialPayload = unknown>(
3440
routeFunction: RouteFunction<TInitialPayload>,
3541
options?: PublicServeOptions<TInitialPayload>
3642
): { handler: NextApiHandler } => {
37-
const { handler: serveHandler } = serveBase(routeFunction, options);
43+
const { handler: serveHandler } = serveBase(
44+
routeFunction,
45+
{
46+
sdk: SDK_TELEMETRY,
47+
framework: "nextjs-pages",
48+
runtime: process.versions.bun
49+
? `bun@${process.versions.bun}/node@${process.version}`
50+
: `node@${process.version}`,
51+
},
52+
options
53+
);
3854

3955
const handler = async (request_: NextApiRequest, res: NextApiResponse) => {
4056
if (request_.method?.toUpperCase() !== "POST") {

platforms/solidjs.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { APIEvent } from "@solidjs/start/server";
22

33
import type { PublicServeOptions, RouteFunction } from "../src";
44
import { serveBase } from "../src/serve";
5+
import { SDK_TELEMETRY } from "../src/constants";
56

67
/**
78
* Serve method to serve a Upstash Workflow in a Nextjs project
@@ -28,7 +29,17 @@ export const serve = <TInitialPayload = unknown>(
2829
}
2930

3031
// create serve handler
31-
const { handler: serveHandler } = serveBase<TInitialPayload>(routeFunction, options);
32+
const { handler: serveHandler } = serveBase<TInitialPayload>(
33+
routeFunction,
34+
{
35+
sdk: SDK_TELEMETRY,
36+
framework: "solidjs",
37+
runtime: process.versions.bun
38+
? `bun@${process.versions.bun}/node@${process.version}`
39+
: `node@${process.version}`,
40+
},
41+
options
42+
);
3243

3344
return await serveHandler(event.request);
3445
};

platforms/svelte.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { RequestHandler } from "@sveltejs/kit";
22

33
import type { PublicServeOptions, RouteFunction } from "../src";
44
import { serveBase } from "../src/serve";
5+
import { SDK_TELEMETRY } from "../src/constants";
56

67
/**
78
* Serve method to serve a Upstash Workflow in a Nextjs project
@@ -19,10 +20,17 @@ export const serve = <TInitialPayload = unknown>(
1920
}
2021
): { POST: RequestHandler } => {
2122
const handler: RequestHandler = async ({ request }) => {
22-
const { handler: serveHandler } = serveBase<TInitialPayload>(routeFunction, {
23-
...options,
24-
useJSONContent: true,
25-
});
23+
const { handler: serveHandler } = serveBase<TInitialPayload>(
24+
routeFunction,
25+
{
26+
sdk: SDK_TELEMETRY,
27+
framework: "svelte",
28+
},
29+
{
30+
...options,
31+
useJSONContent: true,
32+
}
33+
);
2634
return await serveHandler(request);
2735
};
2836

src/client/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { makeGetWaitersRequest, makeNotifyRequest } from "./utils";
44
import { getWorkflowRunId } from "../utils";
55
import { triggerFirstInvocation } from "../workflow-requests";
66
import { WorkflowContext } from "../context";
7-
import { DEFAULT_RETRIES } from "../constants";
87

98
type ClientConfig = ConstructorParameters<typeof QStashClient>[0];
109

@@ -214,8 +213,13 @@ export class Client {
214213
steps: [],
215214
url,
216215
workflowRunId: finalWorkflowRunId,
216+
retries,
217+
telemetry: undefined, // can't know workflow telemetry here
218+
});
219+
const result = await triggerFirstInvocation({
220+
workflowContext: context,
221+
telemetry: undefined, // can't know workflow telemetry here
217222
});
218-
const result = await triggerFirstInvocation(context, retries ?? DEFAULT_RETRIES);
219223
if (result.isOk()) {
220224
return { workflowRunId: finalWorkflowRunId };
221225
} else {

src/constants.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Telemetry } from "./types";
2+
13
export const WORKFLOW_ID_HEADER = "Upstash-Workflow-RunId";
24
export const WORKFLOW_INIT_HEADER = "Upstash-Workflow-Init";
35
export const WORKFLOW_URL_HEADER = "Upstash-Workflow-Url";
@@ -12,3 +14,15 @@ export const DEFAULT_CONTENT_TYPE = "application/json";
1214
export const NO_CONCURRENCY = 1;
1315
export const NOT_SET = "not-set";
1416
export const DEFAULT_RETRIES = 3;
17+
18+
export const VERSION = "v0.2.3";
19+
export const SDK_TELEMETRY = `@upstash/workflow@${VERSION}`;
20+
21+
export const TELEMETRY_HEADER_SDK = "Upstash-Telemetry-Sdk" as const;
22+
export const TELEMETRY_HEADER_FRAMEWORK = "Upstash-Telemetry-Framework" as const;
23+
export const TELEMETRY_HEADER_RUNTIME = "Upstash-Telemetry-Runtime" as const;
24+
25+
export const MOCK_TELEMETRY: Telemetry = {
26+
framework: "mock",
27+
sdk: "mock",
28+
};

0 commit comments

Comments
 (0)