Skip to content

Commit 01ac716

Browse files
authored
feat(clerk-js): Add TelemetryCollector instance to backend client (#2134)
1 parent 5d6973e commit 01ac716

File tree

17 files changed

+100
-42
lines changed

17 files changed

+100
-42
lines changed

packages/backend/src/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { deprecatedObjectProperty } from '@clerk/shared/deprecated';
2+
import type { TelemetryCollectorOptions } from '@clerk/shared/telemetry';
3+
import { TelemetryCollector } from '@clerk/shared/telemetry';
4+
import type { SDKMetadata } from '@clerk/types';
25

36
import type { CreateBackendApiOptions } from './api';
47
import { createBackendApiClient } from './api';
@@ -21,16 +24,23 @@ export type ClerkOptions = CreateBackendApiOptions &
2124
CreateAuthenticateRequestOptions['options'],
2225
'audience' | 'jwtKey' | 'proxyUrl' | 'secretKey' | 'publishableKey' | 'domain' | 'isSatellite'
2326
>
24-
>;
27+
> & { sdkMetadata?: SDKMetadata; telemetry?: Pick<TelemetryCollectorOptions, 'disabled' | 'debug'> };
2528

2629
export function Clerk(options: ClerkOptions) {
2730
const opts = { ...options };
2831
const apiClient = createBackendApiClient(opts);
2932
const requestState = createAuthenticateRequest({ options: opts, apiClient });
33+
const telemetry = new TelemetryCollector({
34+
...options.telemetry,
35+
publishableKey: opts.publishableKey,
36+
secretKey: opts.secretKey,
37+
...(opts.sdkMetadata ? { sdk: opts.sdkMetadata.name, sdkVersion: opts.sdkMetadata.version } : {}),
38+
});
3039

3140
const clerkInstance = {
3241
...apiClient,
3342
...requestState,
43+
telemetry,
3444
/**
3545
* @deprecated This prop has been deprecated and will be removed in the next major release.
3646
*/
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Clerk } from '@clerk/backend';
22

3-
import { API_URL, API_VERSION, JWT_KEY, SECRET_KEY } from './constants';
3+
import { API_URL, API_VERSION, JWT_KEY, SDK_METADATA, SECRET_KEY } from './constants';
44

55
export const createClerkClient = Clerk;
66

@@ -9,4 +9,5 @@ export const clerkClient = createClerkClient({
99
apiUrl: API_URL,
1010
apiVersion: API_VERSION,
1111
jwtKey: JWT_KEY,
12+
sdkMetadata: SDK_METADATA,
1213
});

packages/fastify/src/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ export const SECRET_KEY = process.env.CLERK_SECRET_KEY || '';
66
export const PUBLISHABLE_KEY = process.env.CLERK_PUBLISHABLE_KEY || '';
77
export const API_URL = process.env.CLERK_API_URL || apiUrlFromPublishableKey(PUBLISHABLE_KEY);
88
export const JWT_KEY = process.env.CLERK_JWT_KEY || '';
9+
export const SDK_METADATA = {
10+
name: PACKAGE_NAME,
11+
version: PACKAGE_VERSION,
12+
};
913

1014
export const { Cookies, Headers } = constants;

packages/fastify/src/global.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare global {
2+
const PACKAGE_NAME: string;
3+
const PACKAGE_VERSION: string;
4+
}
5+
6+
export {};

packages/fastify/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
"resolveJsonModule": true,
1616
"declarationDir": "dist/types"
1717
},
18-
"include": ["src/index.ts"],
18+
"include": ["src/index.ts", "src/global.d.ts"],
1919
"exclude": ["node_modules"]
2020
}

packages/gatsby-plugin-clerk/src/GatsbyClerkProvider.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ import {
77
import { navigate } from 'gatsby';
88
import React from 'react';
99

10-
import { TELEMETRY_DEBUG, TELEMETRY_DISABLED } from './constants';
11-
12-
const SDK_METADATA = {
13-
name: PACKAGE_NAME,
14-
version: PACKAGE_VERSION,
15-
};
10+
import { SDK_METADATA, TELEMETRY_DEBUG, TELEMETRY_DISABLED } from './constants';
1611

1712
__internal__setErrorThrowerOptions({ packageName: 'gatsby-plugin-clerk' });
1813

packages/gatsby-plugin-clerk/src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ export const PROXY_URL = process.env.GATSBY_CLERK_PROXY_URL;
1111

1212
export const TELEMETRY_DISABLED = isTruthy(process.env.GATSBY_CLERK_TELEMETRY_DISABLED);
1313
export const TELEMETRY_DEBUG = isTruthy(process.env.GATSBY_CLERK_TELEMETRY_DEBUG);
14+
15+
export const SDK_METADATA = {
16+
name: PACKAGE_NAME,
17+
version: PACKAGE_VERSION,
18+
};

packages/gatsby-plugin-clerk/src/ssr/clerkClient.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { Clerk } from '@clerk/backend';
22

3-
import { API_URL, API_VERSION, SECRET_KEY } from '../constants';
3+
import { API_URL, API_VERSION, SDK_METADATA, SECRET_KEY, TELEMETRY_DEBUG, TELEMETRY_DISABLED } from '../constants';
44

55
const clerkClient = Clerk({
66
secretKey: SECRET_KEY,
77
apiUrl: API_URL,
88
apiVersion: API_VERSION,
99
// TODO: Fetch version from package.json
1010
userAgent: 'gatsby-plugin-clerk',
11+
sdkMetadata: SDK_METADATA,
12+
telemetry: {
13+
disabled: TELEMETRY_DISABLED,
14+
debug: TELEMETRY_DEBUG,
15+
},
1116
});
1217

1318
const createClerkClient = Clerk;

packages/nextjs/src/server/authMiddleware.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { AuthObject, RequestState } from '@clerk/backend';
22
import { buildRequestUrl, constants } from '@clerk/backend';
33
import { isDevelopmentFromApiKey } from '@clerk/shared/keys';
4-
import { TelemetryCollector } from '@clerk/shared/telemetry';
54
import type { Autocomplete } from '@clerk/types';
65
import type Link from 'next/link';
76
import type { NextFetchEvent, NextMiddleware, NextRequest } from 'next/server';
@@ -10,7 +9,8 @@ import { NextResponse } from 'next/server';
109
import { isRedirect, mergeResponses, paths, setHeader, stringifyHeaders } from '../utils';
1110
import { withLogger } from '../utils/debugLogger';
1211
import { authenticateRequest, handleInterstitialState, handleUnknownState } from './authenticateRequest';
13-
import { PUBLISHABLE_KEY, SECRET_KEY } from './constants';
12+
import { clerkClient } from './clerkClient';
13+
import { SECRET_KEY } from './constants';
1414
import { DEV_BROWSER_JWT_MARKER, setDevBrowserJWTInURL } from './devBrowser';
1515
import {
1616
clockSkewDetected,
@@ -28,14 +28,6 @@ import {
2828
setRequestHeadersOnNextResponse,
2929
} from './utils';
3030

31-
const telemetry = new TelemetryCollector({
32-
verbose: true,
33-
samplingRate: 1,
34-
publishableKey: PUBLISHABLE_KEY,
35-
sdk: PACKAGE_NAME,
36-
sdkVersion: PACKAGE_VERSION,
37-
});
38-
3931
type WithPathPatternWildcard<T> = `${T & string}(.*)`;
4032
type NextTypedRoute<T = Parameters<typeof Link>['0']['href']> = T extends string ? T : never;
4133

@@ -154,7 +146,7 @@ const authMiddleware: AuthMiddleware = (...args: unknown[]) => {
154146
const isApiRoute = createApiRoutes(apiRoutes);
155147
const defaultAfterAuth = createDefaultAfterAuth(isPublicRoute, isApiRoute, params);
156148

157-
telemetry.record('METHOD_CALLED', {
149+
clerkClient.telemetry.record('METHOD_CALLED', {
158150
method: 'authMiddleware',
159151
publicRoutes: Boolean(publicRoutes),
160152
ignoredRoutes: Boolean(ignoredRoutes),

packages/nextjs/src/server/clerkClient.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import { Clerk } from '@clerk/backend';
22

3-
import { API_URL, API_VERSION, DOMAIN, IS_SATELLITE, PROXY_URL, SECRET_KEY } from './constants';
3+
import {
4+
API_URL,
5+
API_VERSION,
6+
DOMAIN,
7+
IS_SATELLITE,
8+
PROXY_URL,
9+
SDK_METADATA,
10+
SECRET_KEY,
11+
TELEMETRY_DEBUG,
12+
TELEMETRY_DISABLED,
13+
} from './constants';
414

515
const clerkClient = Clerk({
616
secretKey: SECRET_KEY,
@@ -11,6 +21,11 @@ const clerkClient = Clerk({
1121
proxyUrl: PROXY_URL,
1222
domain: DOMAIN,
1323
isSatellite: IS_SATELLITE,
24+
sdkMetadata: SDK_METADATA,
25+
telemetry: {
26+
disabled: TELEMETRY_DISABLED,
27+
debug: TELEMETRY_DEBUG,
28+
},
1429
});
1530

1631
const createClerkClient = Clerk;

0 commit comments

Comments
 (0)