From 7f00da2708ccb4fee9865f1819468a88261c745f Mon Sep 17 00:00:00 2001 From: Andreas Thomas Date: Thu, 19 Jan 2023 10:47:43 +0100 Subject: [PATCH 1/4] feat: allow setting telemetry from outside --- pkg/http.ts | 51 ++++++++++++++++++++++++++++++++------------------- pkg/redis.ts | 14 ++++++++++++++ pkg/types.ts | 20 ++++++++++++++++++++ 3 files changed, 66 insertions(+), 19 deletions(-) diff --git a/pkg/http.ts b/pkg/http.ts index 6644d5c8..f0e27ae9 100644 --- a/pkg/http.ts +++ b/pkg/http.ts @@ -1,4 +1,5 @@ import { UpstashError } from "./error.ts"; +import { Telemetry } from "./types.ts"; export type UpstashRequest = { path?: string[]; @@ -80,25 +81,7 @@ export type HttpClientConfig = { options?: Options; retry?: RetryConfig; agent?: any; - telemetry?: { - /** - * Upstash-Telemetry-Sdk - * @example @upstash/redis@v1.1.1 - */ - sdk?: string; - - /** - * Upstash-Telemetry-Platform - * @example cloudflare - */ - platform?: string; - - /** - * Upstash-Telemetry-Runtime - * @example node@v18 - */ - runtime?: string; - }; + telemetry?: Telemetry; } & RequesterConfig; export class HttpClient implements Requester { @@ -157,6 +140,36 @@ export class HttpClient implements Requester { } } + public mergeTelemetry(telemetry: Telemetry): void { + function merge( + obj: Record, + key: string, + value?: string, + ): Record { + if (!value) { + return obj; + } + if (obj[key]) { + obj[key] = [obj[key], value].join(","); + } else { + obj[key] = value; + } + return obj; + } + + this.headers = merge( + this.headers, + "Upstash-Telemetry-Runtime", + telemetry.runtime, + ); + this.headers = merge( + this.headers, + "Upstash-Telemetry-Platform", + telemetry.platform, + ); + this.headers = merge(this.headers, "Upstash-Telemetry-Sdk", telemetry.sdk); + } + public async request( req: UpstashRequest, ): Promise> { diff --git a/pkg/redis.ts b/pkg/redis.ts index 450751f0..182f1a0d 100644 --- a/pkg/redis.ts +++ b/pkg/redis.ts @@ -130,6 +130,7 @@ import type { CommandArgs } from "./types.ts"; import { Script } from "./script.ts"; import { ZMScoreCommand } from "./commands/zmscore.ts"; import { ZDiffStoreCommand } from "./commands/zdiffstore.ts"; +import { Telemetry } from "./types.ts"; export type RedisOptions = { /** @@ -179,6 +180,19 @@ export class Redis { middleware(req, makeRequest) as any; }; + /** + * Technically this is not private, we can hide it from intellisense by doing this + */ + private addTelemetry = (telemetry: Telemetry) => { + try { + // @ts-ignore - The `Requester` interface does not know about this method but it will be there + // as long as the user uses the standard HttpClient + this.client.mergeTelemetry(telemetry); + } catch { + // ignore + } + }; + createScript(script: string): Script { return new Script(this, script); } diff --git a/pkg/types.ts b/pkg/types.ts index d372498b..64998f02 100644 --- a/pkg/types.ts +++ b/pkg/types.ts @@ -1,2 +1,22 @@ export type CommandArgs any> = ConstructorParameters[0]; + +export type Telemetry = { + /** + * Upstash-Telemetry-Sdk + * @example @upstash/redis@v1.1.1 + */ + sdk?: string; + + /** + * Upstash-Telemetry-Platform + * @example cloudflare + */ + platform?: string; + + /** + * Upstash-Telemetry-Runtime + * @example node@v18 + */ + runtime?: string; +}; From dbd52e73cf9b40e02c1ba0c22dbd15f0a5e2a01a Mon Sep 17 00:00:00 2001 From: Andreas Thomas Date: Thu, 19 Jan 2023 10:51:35 +0100 Subject: [PATCH 2/4] style: fmt --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1a39ca8d..aeeeec3a 100644 --- a/README.md +++ b/README.md @@ -83,8 +83,9 @@ console.log(data) ## Troubleshooting -We have a [dedicated page](https://docs.upstash.com/redis/sdks/javascriptsdk/troubleshooting) for common -problems. If you can't find a solution, please +We have a +[dedicated page](https://docs.upstash.com/redis/sdks/javascriptsdk/troubleshooting) +for common problems. If you can't find a solution, please [open an issue](https://github.com/upstash/upstash-redis/issues/new). ## Docs From abb41e7760438b376f10349b21d23a905beea022 Mon Sep 17 00:00:00 2001 From: Andreas Thomas Date: Thu, 19 Jan 2023 11:05:37 +0100 Subject: [PATCH 3/4] ci: remove _CLOUD secrets --- .github/workflows/tests.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 725111e5..b7ac99cf 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -689,9 +689,9 @@ jobs: - name: Inject variables working-directory: ./examples/fastly run: | - sed -i 's;;${{ secrets.UPSTASH_REDIS_REST_URL_CLOUD }};' fastly.toml - sed -i 's;;${{ secrets.UPSTASH_REDIS_REST_URL_CLOUD }};' src/index.js - sed -i 's;;${{ secrets.UPSTASH_REDIS_REST_TOKEN_CLOUD }};' src/index.js + sed -i 's;;${{ secrets.UPSTASH_REDIS_REST_URL }};' fastly.toml + sed -i 's;;${{ secrets.UPSTASH_REDIS_REST_URL }};' src/index.js + sed -i 's;;${{ secrets.UPSTASH_REDIS_REST_TOKEN }};' src/index.js - name: Deploy working-directory: ./examples/fastly From 439c7f3084464353f64eb9a2342834d38ca50525 Mon Sep 17 00:00:00 2001 From: Andreas Thomas Date: Thu, 19 Jan 2023 11:25:20 +0100 Subject: [PATCH 4/4] ci: update build command --- examples/fastly/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/fastly/package.json b/examples/fastly/package.json index db05ab50..608b0c06 100644 --- a/examples/fastly/package.json +++ b/examples/fastly/package.json @@ -16,7 +16,7 @@ }, "scripts": { "prebuild": "webpack", - "build": "js-compute-runtime --skip-pkg bin/index.js bin/main.wasm", + "build": "js-compute-runtime bin/index.js bin/main.wasm", "deploy": "npm run build && fastly compute deploy", "dev": "fastly compute serve --watch" }