From 0a5284ef3d539c6b38f61ad8083dd88065bba22c Mon Sep 17 00:00:00 2001 From: Andreas Thomas Date: Thu, 19 Jan 2023 13:25:35 +0100 Subject: [PATCH] fix: allow disabling telemetry from other sdks --- pkg/http.ts | 10 ---------- pkg/redis.ts | 9 ++++++++- pkg/test-utils.ts | 10 ++-------- platforms/cloudflare.ts | 14 ++++++-------- platforms/fastly.ts | 5 +++++ platforms/node_with_fetch.ts | 22 ++++++++++------------ platforms/nodejs.ts | 24 +++++++++++------------- 7 files changed, 42 insertions(+), 52 deletions(-) diff --git a/pkg/http.ts b/pkg/http.ts index f0e27ae9..2c39b3dd 100644 --- a/pkg/http.ts +++ b/pkg/http.ts @@ -81,7 +81,6 @@ export type HttpClientConfig = { options?: Options; retry?: RetryConfig; agent?: any; - telemetry?: Telemetry; } & RequesterConfig; export class HttpClient implements Requester { @@ -112,15 +111,6 @@ export class HttpClient implements Requester { ...config.headers, }; - if (config.telemetry?.runtime) { - this.headers["Upstash-Telemetry-Runtime"] = config.telemetry.runtime; - } - if (config.telemetry?.platform) { - this.headers["Upstash-Telemetry-Platform"] = config.telemetry.platform; - } - if (config.telemetry?.sdk) { - this.headers["Upstash-Telemetry-Sdk"] = config.telemetry.sdk; - } if (this.options.responseEncoding === "base64") { this.headers["Upstash-Encoding"] = "base64"; diff --git a/pkg/redis.ts b/pkg/redis.ts index 182f1a0d..1381734d 100644 --- a/pkg/redis.ts +++ b/pkg/redis.ts @@ -139,6 +139,8 @@ export type RedisOptions = { * @default true */ automaticDeserialization?: boolean; + + enableTelemetry?: boolean; }; /** @@ -147,6 +149,7 @@ export type RedisOptions = { export class Redis { protected client: Requester; protected opts?: CommandOptions; + protected enableTelemetry: boolean; /** * Create a new redis client @@ -162,6 +165,7 @@ export class Redis { constructor(client: Requester, opts?: RedisOptions) { this.client = client; this.opts = opts; + this.enableTelemetry = opts?.enableTelemetry ?? true; } /** @@ -183,7 +187,10 @@ export class Redis { /** * Technically this is not private, we can hide it from intellisense by doing this */ - private addTelemetry = (telemetry: Telemetry) => { + protected addTelemetry = (telemetry: Telemetry) => { + if (!this.enableTelemetry) { + return; + } 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 diff --git a/pkg/test-utils.ts b/pkg/test-utils.ts index 18744447..b6bc5b05 100644 --- a/pkg/test-utils.ts +++ b/pkg/test-utils.ts @@ -1,6 +1,5 @@ import { DelCommand } from "./commands/del.ts"; -import { HttpClient, HttpClientConfig } from "./http.ts"; -import { VERSION } from "../version.ts"; +import { HttpClient } from "./http.ts"; /** * crypto.randomUUID() is not available in dnt crypto shim @@ -24,15 +23,10 @@ export const newHttpClient = () => { if (!token) { throw new Error("Could not find token"); } - const telemetry: HttpClientConfig["telemetry"] = {}; - if (!Deno.env.get("UPSTASH_DISABLE_TELEMETRY")) { - telemetry.runtime = `deno@${Deno.version.deno}`; - telemetry.sdk = `@upstash/redis@${VERSION}`; - } + return new HttpClient({ baseUrl: url, headers: { authorization: `Bearer ${token}` }, - telemetry, }); }; diff --git a/platforms/cloudflare.ts b/platforms/cloudflare.ts index 788c3df7..cc5750ac 100644 --- a/platforms/cloudflare.ts +++ b/platforms/cloudflare.ts @@ -1,6 +1,5 @@ import * as core from "../pkg/redis.ts"; import type { - HttpClientConfig, Requester, UpstashRequest, UpstashResponse, @@ -66,23 +65,22 @@ export class Redis extends core.Redis { ); } - const telemetry: HttpClientConfig["telemetry"] = {}; - if (!env?.UPSTASH_DISABLE_TELEMETRY) { - telemetry.platform = "cloudflare"; - telemetry.sdk = `@upstash/redis@${VERSION}`; - } - const client = new HttpClient({ retry: config.retry, baseUrl: config.url, headers: { authorization: `Bearer ${config.token}` }, responseEncoding: config.responseEncoding, - telemetry, }); super(client, { + enableTelemetry: !env?.UPSTASH_DISABLE_TELEMETRY, automaticDeserialization: config.automaticDeserialization, }); + // This is only added of the user has not disabled telemetry + this.addTelemetry({ + platform: "cloudflare", + sdk: `@upstash/redis@${VERSION}`, + }); } /* diff --git a/platforms/fastly.ts b/platforms/fastly.ts index 4844efd9..736260e3 100644 --- a/platforms/fastly.ts +++ b/platforms/fastly.ts @@ -6,6 +6,7 @@ import type { UpstashResponse, } from "../pkg/http.ts"; import { HttpClient } from "../pkg/http.ts"; +import { VERSION } from "../version.ts"; export type { Requester, UpstashRequest, UpstashResponse }; @@ -80,5 +81,9 @@ export class Redis extends core.Redis { super(client, { automaticDeserialization: config.automaticDeserialization, }); + this.addTelemetry({ + sdk: `@upstash/redis@${VERSION}`, + platform: "fastly", + }); } } diff --git a/platforms/node_with_fetch.ts b/platforms/node_with_fetch.ts index 98cc623c..18891c78 100644 --- a/platforms/node_with_fetch.ts +++ b/platforms/node_with_fetch.ts @@ -3,7 +3,6 @@ import * as core from "../pkg/redis.ts"; import { HttpClient, - HttpClientConfig, Requester, RequesterConfig, UpstashRequest, @@ -122,27 +121,26 @@ export class Redis extends core.Redis { ); } - const telemetry: HttpClientConfig["telemetry"] = {}; - if (!process.env.UPSTASH_DISABLE_TELEMETRY) { - telemetry.runtime = `node@${process.version}`; - telemetry.platform = process.env.VERCEL - ? "vercel" - : process.env.AWS_REGION - ? "aws" - : "unknown"; - telemetry.sdk = `@upstash/redis@${VERSION}`; - } const client = new HttpClient({ baseUrl: configOrRequester.url, retry: configOrRequester.retry, headers: { authorization: `Bearer ${configOrRequester.token}` }, // agent: configOrRequester.agent, responseEncoding: configOrRequester.responseEncoding, - telemetry, }); super(client, { automaticDeserialization: configOrRequester.automaticDeserialization, + enableTelemetry: !process.env.UPSTASH_DISABLE_TELEMETRY, + }); + this.addTelemetry({ + runtime: `node@${process.version}`, + platform: process.env.VERCEL + ? "vercel" + : process.env.AWS_REGION + ? "aws" + : "unknown", + sdk: `@upstash/redis@${VERSION}`, }); } diff --git a/platforms/nodejs.ts b/platforms/nodejs.ts index bdebcb0a..306c9076 100644 --- a/platforms/nodejs.ts +++ b/platforms/nodejs.ts @@ -3,7 +3,6 @@ import * as core from "../pkg/redis.ts"; import { HttpClient, - HttpClientConfig, Requester, RequesterConfig, UpstashRequest, @@ -116,28 +115,27 @@ export class Redis extends core.Redis { ); } - const telemetry: HttpClientConfig["telemetry"] = {}; - if (!process.env.UPSTASH_DISABLE_TELEMETRY) { - telemetry.runtime = `node@${process.version}`; - telemetry.platform = process.env.VERCEL - ? "vercel" - : process.env.AWS_REGION - ? "aws" - : "unknown"; - telemetry.sdk = `@upstash/redis@${VERSION}`; - } - const client = new HttpClient({ baseUrl: configOrRequester.url, retry: configOrRequester.retry, headers: { authorization: `Bearer ${configOrRequester.token}` }, agent: configOrRequester.agent, responseEncoding: configOrRequester.responseEncoding, - telemetry, }); super(client, { automaticDeserialization: configOrRequester.automaticDeserialization, + enableTelemetry: !process.env.UPSTASH_DISABLE_TELEMETRY, + }); + + this.addTelemetry({ + runtime: `node@${process.version}`, + platform: process.env.VERCEL + ? "vercel" + : process.env.AWS_REGION + ? "aws" + : "unknown", + sdk: `@upstash/redis@${VERSION}`, }); }