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
6 changes: 3 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -689,9 +689,9 @@ jobs:
- name: Inject variables
working-directory: ./examples/fastly
run: |
sed -i 's;<UPSTASH_REDIS_REST_URL>;${{ secrets.UPSTASH_REDIS_REST_URL_CLOUD }};' fastly.toml
sed -i 's;<UPSTASH_REDIS_REST_URL>;${{ secrets.UPSTASH_REDIS_REST_URL_CLOUD }};' src/index.js
sed -i 's;<UPSTASH_REDIS_REST_TOKEN>;${{ secrets.UPSTASH_REDIS_REST_TOKEN_CLOUD }};' src/index.js
sed -i 's;<UPSTASH_REDIS_REST_URL>;${{ secrets.UPSTASH_REDIS_REST_URL }};' fastly.toml
sed -i 's;<UPSTASH_REDIS_REST_URL>;${{ secrets.UPSTASH_REDIS_REST_URL }};' src/index.js
sed -i 's;<UPSTASH_REDIS_REST_TOKEN>;${{ secrets.UPSTASH_REDIS_REST_TOKEN }};' src/index.js

- name: Deploy
working-directory: ./examples/fastly
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/fastly/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
51 changes: 32 additions & 19 deletions pkg/http.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { UpstashError } from "./error.ts";
import { Telemetry } from "./types.ts";

export type UpstashRequest = {
path?: string[];
Expand Down Expand Up @@ -80,25 +81,7 @@ export type HttpClientConfig = {
options?: Options;
retry?: RetryConfig;
agent?: any;
telemetry?: {
/**
* Upstash-Telemetry-Sdk
* @example @upstash/[email protected]
*/
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 {
Expand Down Expand Up @@ -157,6 +140,36 @@ export class HttpClient implements Requester {
}
}

public mergeTelemetry(telemetry: Telemetry): void {
function merge(
obj: Record<string, string>,
key: string,
value?: string,
): Record<string, string> {
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<TResult>(
req: UpstashRequest,
): Promise<UpstashResponse<TResult>> {
Expand Down
14 changes: 14 additions & 0 deletions pkg/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
/**
Expand Down Expand Up @@ -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);
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/types.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
export type CommandArgs<TCommand extends new (...args: any) => any> =
ConstructorParameters<TCommand>[0];

export type Telemetry = {
/**
* Upstash-Telemetry-Sdk
* @example @upstash/[email protected]
*/
sdk?: string;

/**
* Upstash-Telemetry-Platform
* @example cloudflare
*/
platform?: string;

/**
* Upstash-Telemetry-Runtime
* @example node@v18
*/
runtime?: string;
};