Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions pkg/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export type HttpClientConfig = {
options?: Options;
retry?: RetryConfig;
agent?: any;
telemetry?: Telemetry;
} & RequesterConfig;

export class HttpClient implements Requester {
Expand Down Expand Up @@ -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";
Expand Down
9 changes: 8 additions & 1 deletion pkg/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ export type RedisOptions = {
* @default true
*/
automaticDeserialization?: boolean;

enableTelemetry?: boolean;
};

/**
Expand All @@ -147,6 +149,7 @@ export type RedisOptions = {
export class Redis {
protected client: Requester;
protected opts?: CommandOptions<any, any>;
protected enableTelemetry: boolean;

/**
* Create a new redis client
Expand All @@ -162,6 +165,7 @@ export class Redis {
constructor(client: Requester, opts?: RedisOptions) {
this.client = client;
this.opts = opts;
this.enableTelemetry = opts?.enableTelemetry ?? true;
}

/**
Expand All @@ -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
Expand Down
10 changes: 2 additions & 8 deletions pkg/test-utils.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
});
};

Expand Down
14 changes: 6 additions & 8 deletions platforms/cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as core from "../pkg/redis.ts";
import type {
HttpClientConfig,
Requester,
UpstashRequest,
UpstashResponse,
Expand Down Expand Up @@ -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}`,
});
}

/*
Expand Down
5 changes: 5 additions & 0 deletions platforms/fastly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down Expand Up @@ -80,5 +81,9 @@ export class Redis extends core.Redis {
super(client, {
automaticDeserialization: config.automaticDeserialization,
});
this.addTelemetry({
sdk: `@upstash/redis@${VERSION}`,
platform: "fastly",
});
}
}
22 changes: 10 additions & 12 deletions platforms/node_with_fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import * as core from "../pkg/redis.ts";
import {
HttpClient,
HttpClientConfig,
Requester,
RequesterConfig,
UpstashRequest,
Expand Down Expand Up @@ -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}`,
});
}

Expand Down
24 changes: 11 additions & 13 deletions platforms/nodejs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import * as core from "../pkg/redis.ts";
import {
HttpClient,
HttpClientConfig,
Requester,
RequesterConfig,
UpstashRequest,
Expand Down Expand Up @@ -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}`,
});
}

Expand Down