From 8895aca4ac4b6a7430f4e15b010a71fd1735c231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20Olguncu?= <21091016+ogzhanolguncu@users.noreply.github.com> Date: Fri, 22 Mar 2024 22:39:36 +0300 Subject: [PATCH] Opt-in per-line logging (#958) * Opt-in per-line logging * ci: increase timeout for tests * feat: add latencyLogging to CF --- bun.lockb | Bin 60912 -> 62409 bytes package.json | 31 +++++++++---------------------- pkg/commands/command.ts | 17 +++++++++++++++++ pkg/http.ts | 1 + pkg/redis.test.ts | 11 +++++++++++ pkg/types.ts | 2 +- platforms/cloudflare.ts | 27 ++++++++------------------- platforms/nodejs.ts | 34 ++++++++++------------------------ 8 files changed, 57 insertions(+), 66 deletions(-) diff --git a/bun.lockb b/bun.lockb index f1a4c6ee024a89b0b34a3137d72df9298b0154b6..af801cbd1eaf211b227944b6ce27b0f3a43a21e7 100755 GIT binary patch delta 10610 zcmeHNd013Ow!gOlQ>*SzPM?3@o#o(4S}od&tDST+kJ7wzO>J|GoKeMnDXb*<}Z&AU9r3Ns@JK5>!ZIC zh(g*x8>U%?e0_h3)4x~>x5*Lx+j!r{5L&n)UF32w`PA@7FJX#9^8|DbfSV z@~s(yP=fMoXi-UeVQNvPu-{Q>xWHPNTAW!VG!FuF#hwWgcE96{AHYkVcMelfLXt|r! zXpKwFfn0m)zP^a@Tm5CxNkTh50!-JpR**GG}Mz z6&F2Y8>9h{ZJ{qN3d+mR$P~)**iu2bjZ7ZhcOkj84D0mioPQo19af|kW-%2`+w`gF z`Gs7y4xD}7rpTTZt85wbZ88Otr&o2SEHyWW8!Diyn&6~l%rHAW1vAgx?t)~VR+yTO zsStdzVmLnqlFc{*$@(!U$Ae}q3X(_PiV@;+11ZVQJN^L}x~(|mr>u-0A$j1#P>YSt zuolYI&ciTnZ3L{~)-p>o(^FyFg$_#n(^PNgU0x5y>#HEOI6G)&X4xJa-wMgK*Ff@+ zFM(tm=0b97bF!`J*=WsLgmxhp1t?aG>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R$lP4eDBj0(84|*Z6`{dVa$E-M3`{i12-e7K8pKOY1 zGFi5ec->y-=&5beD^rqAiRJ#^>;Lz&|IKpW4UWE*ANA7yrQ<^OeJ?n74>Vl??~PU( zLbLsU=#zNykKlpeZ>BDsVZV3O`})7mtel-(7(I#_UfZ#M zc-E<}>OPo(i~ux{InH^y@{QCd@;n7nPy!pia;QlCIJm|@O zD~>JQ_9!_1>eoJUbjR_w_|wmW;1F29>sQw|wJeN2x(~cB_^Hm{zHwg>oqP+Nr(tH{ z%=tbm!(V>mJ!IglS6{bb)caT7a5U{ajEO>+eU0WGEuy!N-b1sF?ewXxEib6buXLAJ fw|{>u)JXov# = { * @default true */ automaticDeserialization?: boolean; + latencyLogging?: boolean; }; /** * Command offers default (de)serialization and the exec method to all commands. @@ -55,6 +56,22 @@ export class Command { : (x) => x as unknown as TData; this.command = command.map((c) => this.serialize(c)); + + if (opts?.latencyLogging) { + const originalExec = this.exec.bind(this); + this.exec = async (client: Requester): Promise => { + const start = performance.now(); + const result = await originalExec(client); + const end = performance.now(); + const loggerResult = (end - start).toFixed(2); + console.log( + `Latency for \x1b[38;2;19;185;39m${this.command[0] + .toString() + .toUpperCase()}\x1b[0m: \x1b[38;2;0;255;255m${loggerResult} ms\x1b[0m`, + ); + return result; + }; + } } /** diff --git a/pkg/http.ts b/pkg/http.ts index 45daa71b..66ea9d83 100644 --- a/pkg/http.ts +++ b/pkg/http.ts @@ -170,6 +170,7 @@ export class HttpClient implements Requester { public async request(req: UpstashRequest): Promise> { const requestOptions: RequestInit & { backend?: string; agent?: any } = { + //@ts-expect-error this should throw due to bun regression cache: this.options.cache, method: "POST", headers: this.headers, diff --git a/pkg/redis.test.ts b/pkg/redis.test.ts index f7bd9a0c..3562b6d8 100644 --- a/pkg/redis.test.ts +++ b/pkg/redis.test.ts @@ -237,3 +237,14 @@ test("disable base64 encoding", () => { expect(res).toEqual(value); }); }); + +describe("tests with latency logging", () => { + test("test should return OK with latency logs", async () => { + const redis = new Redis(client, { latencyLogging: true }); + const key = newKey(); + const value = "OK"; + await redis.set(key, value); + const res = await redis.get(key); + expect(res).toEqual(value); + }); +}); diff --git a/pkg/types.ts b/pkg/types.ts index 18295b57..e30d35f8 100644 --- a/pkg/types.ts +++ b/pkg/types.ts @@ -28,6 +28,6 @@ export type RedisOptions = { * @default true */ automaticDeserialization?: boolean; - + latencyLogging?: boolean; enableTelemetry?: boolean; }; diff --git a/platforms/cloudflare.ts b/platforms/cloudflare.ts index 5e740901..c71e1a6e 100644 --- a/platforms/cloudflare.ts +++ b/platforms/cloudflare.ts @@ -47,23 +47,11 @@ export class Redis extends core.Redis { * ``` */ constructor(config: RedisConfigCloudflare, env?: Env) { - if ( - config.url.startsWith(" ") || - config.url.endsWith(" ") || - /\r|\n/.test(config.url) - ) { - console.warn( - "The redis url contains whitespace or newline, which can cause errors!" - ); + if (config.url.startsWith(" ") || config.url.endsWith(" ") || /\r|\n/.test(config.url)) { + console.warn("The redis url contains whitespace or newline, which can cause errors!"); } - if ( - config.token.startsWith(" ") || - config.token.endsWith(" ") || - /\r|\n/.test(config.token) - ) { - console.warn( - "The redis token contains whitespace or newline, which can cause errors!" - ); + if (config.token.startsWith(" ") || config.token.endsWith(" ") || /\r|\n/.test(config.token)) { + console.warn("The redis token contains whitespace or newline, which can cause errors!"); } const client = new HttpClient({ @@ -77,6 +65,7 @@ export class Redis extends core.Redis { super(client, { enableTelemetry: !env?.UPSTASH_DISABLE_TELEMETRY, automaticDeserialization: config.automaticDeserialization, + latencyLogging: config.latencyLogging, }); // This is only added of the user has not disabled telemetry this.addTelemetry({ @@ -102,7 +91,7 @@ export class Redis extends core.Redis { UPSTASH_REDIS_REST_TOKEN: string; UPSTASH_DISABLE_TELEMETRY?: string; }, - opts?: Omit + opts?: Omit, ): Redis { // @ts-ignore These will be defined by cloudflare const url = env?.UPSTASH_REDIS_REST_URL ?? UPSTASH_REDIS_REST_URL; @@ -112,12 +101,12 @@ export class Redis extends core.Redis { if (!url) { throw new Error( - "Unable to find environment variable: `UPSTASH_REDIS_REST_URL`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_URL`" + "Unable to find environment variable: `UPSTASH_REDIS_REST_URL`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_URL`", ); } if (!token) { throw new Error( - "Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_TOKEN`" + "Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_TOKEN`", ); } return new Redis({ ...opts, url, token }, env); diff --git a/platforms/nodejs.ts b/platforms/nodejs.ts index bd712c5b..4defb7c0 100644 --- a/platforms/nodejs.ts +++ b/platforms/nodejs.ts @@ -14,9 +14,7 @@ import { VERSION } from "../version"; * Workaround for nodejs 14, where atob is not included in the standardlib */ if (typeof atob === "undefined") { - global.atob = function (b64: string) { - return Buffer.from(b64, "base64").toString("utf-8"); - }; + global.atob = (b64: string) => Buffer.from(b64, "base64").toString("utf-8"); } export type * from "../pkg/commands/types"; export type { Requester, UpstashRequest, UpstashResponse }; @@ -55,6 +53,7 @@ export type RedisConfigNodejs = { * For more check: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal */ signal?: AbortSignal; + latencyLogging?: boolean; agent?: any; } & core.RedisOptions & RequesterConfig; @@ -104,18 +103,14 @@ export class Redis extends core.Redis { configOrRequester.url.endsWith(" ") || /\r|\n/.test(configOrRequester.url) ) { - console.warn( - "The redis url contains whitespace or newline, which can cause errors!" - ); + console.warn("The redis url contains whitespace or newline, which can cause errors!"); } if ( configOrRequester.token.startsWith(" ") || configOrRequester.token.endsWith(" ") || /\r|\n/.test(configOrRequester.token) ) { - console.warn( - "The redis token contains whitespace or newline, which can cause errors!" - ); + console.warn("The redis token contains whitespace or newline, which can cause errors!"); } const client = new HttpClient({ @@ -131,19 +126,14 @@ export class Redis extends core.Redis { super(client, { automaticDeserialization: configOrRequester.automaticDeserialization, enableTelemetry: !process.env.UPSTASH_DISABLE_TELEMETRY, + latencyLogging: configOrRequester.latencyLogging, }); this.addTelemetry({ runtime: // @ts-ignore - typeof EdgeRuntime === "string" - ? "edge-light" - : `node@${process.version}`, - platform: process.env.VERCEL - ? "vercel" - : process.env.AWS_REGION - ? "aws" - : "unknown", + typeof EdgeRuntime === "string" ? "edge-light" : `node@${process.version}`, + platform: process.env.VERCEL ? "vercel" : process.env.AWS_REGION ? "aws" : "unknown", sdk: `@upstash/redis@${VERSION}`, }); } @@ -161,22 +151,18 @@ export class Redis extends core.Redis { // @ts-ignore process will be defined in node if (typeof process?.env === "undefined") { throw new Error( - 'Unable to get environment variables, `process.env` is undefined. If you are deploying to cloudflare, please import from "@upstash/redis/cloudflare" instead' + 'Unable to get environment variables, `process.env` is undefined. If you are deploying to cloudflare, please import from "@upstash/redis/cloudflare" instead', ); } // @ts-ignore process will be defined in node const url = process?.env.UPSTASH_REDIS_REST_URL; if (!url) { - throw new Error( - "Unable to find environment variable: `UPSTASH_REDIS_REST_URL`" - ); + throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_URL`"); } // @ts-ignore process will be defined in node const token = process?.env.UPSTASH_REDIS_REST_TOKEN; if (!token) { - throw new Error( - "Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`" - ); + throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`"); } return new Redis({ ...config, url, token }); }