Skip to content

Commit 3b3b8e3

Browse files
committed
pass the telemtry properties through the config
1 parent 5008a00 commit 3b3b8e3

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

src/telemetry/telemetry.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ type EventResult = {
1414
};
1515

1616
export class Telemetry {
17-
public static baseCommonProperties: CommonProperties = {
18-
...MACHINE_METADATA,
19-
hosting_mode: "standalone",
20-
};
21-
2217
private isBufferingEvents: boolean = true;
2318
/** Resolves when the setup is complete or a timeout occurs */
2419
public setupPromise: Promise<[string, boolean]> | undefined;
@@ -40,14 +35,21 @@ export class Telemetry {
4035
userConfig: UserConfig,
4136
deviceId: DeviceId,
4237
{
43-
commonProperties = this.baseCommonProperties,
38+
commonProperties = {},
4439
eventCache = EventCache.getInstance(),
4540
}: {
41+
commonProperties?: Partial<CommonProperties>;
4642
eventCache?: EventCache;
47-
commonProperties?: CommonProperties;
4843
} = {}
4944
): Telemetry {
50-
const instance = new Telemetry(session, userConfig, commonProperties, { eventCache, deviceId });
45+
const mergedProperties = {
46+
...MACHINE_METADATA,
47+
...commonProperties,
48+
};
49+
const instance = new Telemetry(session, userConfig, mergedProperties, {
50+
eventCache,
51+
deviceId,
52+
});
5153

5254
void instance.setup();
5355
return instance;

src/transports/base.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import {
1313
type ConnectionErrorHandler,
1414
connectionErrorHandler as defaultConnectionErrorHandler,
1515
} from "../common/connectionErrorHandler.js";
16+
import type { CommonProperties } from "../telemetry/types.js";
1617

1718
export type TransportRunnerConfig = {
1819
userConfig: UserConfig;
1920
createConnectionManager?: ConnectionManagerFactoryFn;
2021
connectionErrorHandler?: ConnectionErrorHandler;
2122
additionalLoggers?: LoggerBase[];
23+
telemetryProperties?: Partial<CommonProperties>;
2224
};
2325

2426
export abstract class TransportRunnerBase {
@@ -27,16 +29,19 @@ export abstract class TransportRunnerBase {
2729
protected readonly userConfig: UserConfig;
2830
private readonly createConnectionManager: ConnectionManagerFactoryFn;
2931
private readonly connectionErrorHandler: ConnectionErrorHandler;
32+
private readonly telemetryProperties: Partial<CommonProperties>;
3033

3134
protected constructor({
3235
userConfig,
3336
createConnectionManager = createMCPConnectionManager,
3437
connectionErrorHandler = defaultConnectionErrorHandler,
3538
additionalLoggers = [],
39+
telemetryProperties = {},
3640
}: TransportRunnerConfig) {
3741
this.userConfig = userConfig;
3842
this.createConnectionManager = createConnectionManager;
3943
this.connectionErrorHandler = connectionErrorHandler;
44+
this.telemetryProperties = telemetryProperties;
4045
const loggers: LoggerBase[] = [...additionalLoggers];
4146
if (this.userConfig.loggers.includes("stderr")) {
4247
loggers.push(new ConsoleLogger());
@@ -79,7 +84,9 @@ export abstract class TransportRunnerBase {
7984
connectionManager,
8085
});
8186

82-
const telemetry = Telemetry.create(session, this.userConfig, this.deviceId);
87+
const telemetry = Telemetry.create(session, this.userConfig, this.deviceId, {
88+
commonProperties: this.telemetryProperties,
89+
});
8390

8491
const result = new Server({
8592
mcpServer,

tests/integration/transports/streamableHttp.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { StreamableHttpRunner } from "../../../src/transports/streamableHttp.js";
22
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
33
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
4-
import { describe, expect, it, beforeAll, afterAll, beforeEach } from "vitest";
4+
import { describe, expect, it, beforeAll, afterAll, beforeEach, afterEach } from "vitest";
55
import { config } from "../../../src/common/config.js";
66
import type { LoggerType, LogLevel, LogPayload } from "../../../src/common/logger.js";
77
import { LoggerBase, LogId } from "../../../src/common/logger.js";
@@ -158,4 +158,26 @@ describe("StreamableHttpRunner", () => {
158158
expect(serverStartedMessage?.level).toBe("info");
159159
});
160160
});
161+
162+
describe("with telemetry properties", () => {
163+
afterEach(async () => {
164+
await runner.close();
165+
config.telemetry = oldTelemetry;
166+
config.loggers = oldLoggers;
167+
config.httpHeaders = {};
168+
});
169+
170+
it("merges them with the base properties", async () => {
171+
config.telemetry = "enabled";
172+
runner = new StreamableHttpRunner({
173+
userConfig: config,
174+
telemetryProperties: { hosting_mode: "vscode-extension" },
175+
});
176+
await runner.start();
177+
178+
const server = await runner["setupServer"]();
179+
const properties = server["telemetry"].getCommonProperties();
180+
expect(properties.hosting_mode).toBe("vscode-extension");
181+
});
182+
});
161183
});

tests/unit/telemetry.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,9 @@ describe("Telemetry", () => {
214214
});
215215

216216
it("should add hostingMode to events if set", async () => {
217-
Telemetry.baseCommonProperties = {
218-
...Telemetry.baseCommonProperties,
219-
hosting_mode: "vscode-extension",
220-
};
221217
telemetry = Telemetry.create(session, config, mockDeviceId, {
222218
eventCache: mockEventCache as unknown as EventCache,
219+
commonProperties: { hosting_mode: "vscode-extension" },
223220
});
224221
await telemetry.setupPromise;
225222

0 commit comments

Comments
 (0)