Skip to content

Commit cad5d20

Browse files
authored
feat: add telemetry (#58)
1 parent 4c24449 commit cad5d20

File tree

9 files changed

+84
-10
lines changed

9 files changed

+84
-10
lines changed

.github/workflows/release.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ jobs:
2222
node-version: 18
2323

2424
- name: Set package version
25-
run: echo $(jq --arg v "${{ env.VERSION }}" '(.version) = $v' package.json) > package.json
25+
run: |
26+
echo $(jq --arg v "${{ env.VERSION }}" '(.version) = $v' package.json) > package.json
27+
echo "export const VERSION='${{ env.VERSION }}'" > ./version.ts
2628
2729
- name: Setup Bun
2830
uses: oven-sh/setup-bun@v1

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,22 @@ await namespace.fetch(["id-1", "id-2"]);
258258

259259
If you wanna learn more about filtering check: [Metadata Filtering](https://upstash.com/docs/vector/features/filtering)
260260

261+
## Telemetry
262+
263+
This sdk sends anonymous telemetry data to help us improve your experience.
264+
We collect the following:
265+
266+
- SDK version
267+
- Platform (Cloudflare, AWS or Vercel)
268+
- Runtime version ([email protected])
269+
270+
You can opt out by setting the `UPSTASH_DISABLE_TELEMETRY` environment variable
271+
to any truthy value.
272+
273+
```sh
274+
UPSTASH_DISABLE_TELEMETRY=1
275+
```
276+
261277
## Troubleshooting
262278

263279
We have a [Discord](upstash.com/discord) for common problems. If you can't find a solution, please [open an issue](https://github.com/upstash/vector-js/issues/new).

examples/nextjs/bun.lockb

147 KB
Binary file not shown.

src/commands/client/namespace/index.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,3 @@ describe("NAMESPACE", () => {
173173
});
174174
});
175175
});
176-

src/commands/client/namespace/index.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ export class Namespace<TIndexMetadata extends Dict = Dict> {
142142
query = <TMetadata extends Dict = TIndexMetadata>(args: CommandArgs<typeof QueryCommand>) =>
143143
new QueryCommand<TMetadata>(args, { namespace: this.namespace }).exec(this.client);
144144

145-
146145
/**
147146
* Initializes a resumable query operation on the vector database.
148147
* This method allows for querying large result sets in multiple chunks or implementing pagination.
@@ -169,9 +168,7 @@ export class Namespace<TIndexMetadata extends Dict = Dict> {
169168
* const secondBatch = await fetchNext(10);
170169
* await stop(); // End the query session
171170
*/
172-
resumableQuery = async <TMetadata extends Dict = TIndexMetadata>(
173-
args: ResumableQueryPayload,
174-
) => {
171+
resumableQuery = async <TMetadata extends Dict = TIndexMetadata>(args: ResumableQueryPayload) => {
175172
const resumableQuery = new ResumableQuery<TMetadata>(args, this.client, this.namespace);
176173
const initialQuery = await resumableQuery.start();
177174
const { fetchNext, stop } = resumableQuery;

src/platforms/cloudflare.ts

+28-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as core from "./../vector";
33

44
export type * from "@commands/types";
55
import type { Dict } from "@commands/client/types";
6+
import { VERSION } from "../../version";
67

78
/**
89
* Connection credentials for upstash vector.
@@ -23,6 +24,14 @@ export type IndexConfig = {
2324
* For more check: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
2425
*/
2526
signal?: AbortSignal;
27+
28+
/**
29+
* Enable telemetry to help us improve the SDK.
30+
* The sdk will send the sdk version, platform and node version as telemetry headers.
31+
*
32+
* @default true
33+
*/
34+
enableTelemetry?: boolean;
2635
} & RequesterConfig;
2736

2837
/**
@@ -73,10 +82,21 @@ export class Index<TIndexMetadata extends Dict = Dict> extends core.Index<TIndex
7382
console.warn("The vector token contains whitespace or newline, which can cause errors!");
7483
}
7584

85+
const enableTelemetry = safeProcess.UPSTASH_DISABLE_TELEMETRY
86+
? false
87+
: (config?.enableTelemetry ?? true);
88+
89+
const telemetryHeaders: Record<string, string> = enableTelemetry
90+
? {
91+
"Upstash-Telemetry-Sdk": `upstash-vector-js@${VERSION}`,
92+
"Upstash-Telemetry-Platform": "cloudflare",
93+
}
94+
: {};
95+
7696
const client = new HttpClient({
7797
baseUrl: url,
7898
retry: config?.retry,
79-
headers: { authorization: `Bearer ${token}` },
99+
headers: { authorization: `Bearer ${token}`, ...telemetryHeaders },
80100
signal: config?.signal,
81101
cache: config?.cache === false ? undefined : config?.cache,
82102
});
@@ -118,7 +138,13 @@ export class Index<TIndexMetadata extends Dict = Dict> extends core.Index<TIndex
118138
}
119139
}
120140

121-
return new Index({ ...config, url, token });
141+
return new Index({
142+
// @ts-expect-error We don't need to type this in the cf env type
143+
enableTelemetry: env?.UPSTASH_DISABLE_TELEMETRY ? false : undefined,
144+
...config,
145+
url,
146+
token,
147+
});
122148
}
123149
}
124150

src/platforms/nodejs.ts

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { HttpClient, type Requester, type RequesterConfig } from "@http";
22
import * as core from "./../vector";
3+
import type { Dict } from "@commands/client/types";
4+
import { VERSION } from "../../version";
5+
import { getRuntime } from "@utils/get-runtime";
36

47
export type * from "@commands/types";
5-
import type { Dict } from "@commands/client/types";
68

79
/**
810
* Connection credentials for upstash vector.
@@ -23,6 +25,14 @@ export type IndexConfig = {
2325
* For more check: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
2426
*/
2527
signal?: AbortSignal;
28+
29+
/**
30+
* Enable telemetry to help us improve the SDK.
31+
* The sdk will send the sdk version, platform and node version as telemetry headers.
32+
*
33+
* @default true
34+
*/
35+
enableTelemetry?: boolean;
2636
} & RequesterConfig;
2737

2838
/**
@@ -93,10 +103,26 @@ export class Index<TIndexMetadata extends Dict = Dict> extends core.Index<TIndex
93103
console.warn("The vector token contains whitespace or newline, which can cause errors!");
94104
}
95105

106+
const enableTelemetry = process.env.UPSTASH_DISABLE_TELEMETRY
107+
? false
108+
: (configOrRequester?.enableTelemetry ?? true);
109+
110+
const telemetryHeaders: Record<string, string> = enableTelemetry
111+
? {
112+
"Upstash-Telemetry-Sdk": `upstash-vector-js@${VERSION}`,
113+
"Upstash-Telemetry-Platform": process.env.VERCEL
114+
? "vercel"
115+
: process.env.AWS_REGION
116+
? "aws"
117+
: "unknown",
118+
"Upstash-Telemetry-Runtime": getRuntime(),
119+
}
120+
: {};
121+
96122
const client = new HttpClient({
97123
baseUrl: url,
98124
retry: configOrRequester?.retry,
99-
headers: { authorization: `Bearer ${token}` },
125+
headers: { authorization: `Bearer ${token}`, ...telemetryHeaders },
100126
cache:
101127
configOrRequester?.cache === false ? undefined : configOrRequester?.cache || "no-store",
102128
signal: configOrRequester?.signal,

src/utils/get-runtime.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function getRuntime() {
2+
if (typeof process === "object" && typeof process.versions == "object" && process.versions.bun)
3+
return `bun@${process.versions.bun}`;
4+
5+
// @ts-expect-error Silence compiler
6+
return typeof EdgeRuntime === "string" ? "edge-light" : `node@${process.version}`;
7+
}

version.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const VERSION = "0.0.0";

0 commit comments

Comments
 (0)