Skip to content

Commit 3f6bf76

Browse files
committed
Move telemetry to tooling sdk
1 parent 2425908 commit 3f6bf76

9 files changed

Lines changed: 216 additions & 1132 deletions

File tree

packages/b2c-dx-mcp/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@
8585
"@modelcontextprotocol/sdk": "^1.18.0",
8686
"@oclif/core": "^4",
8787
"@salesforce/b2c-tooling-sdk": "workspace:*",
88-
"@salesforce/telemetry": "^6.1.0",
89-
"applicationinsights": "^3.13.0",
9088
"zod": "^3.25.76"
9189
},
9290
"devDependencies": {

packages/b2c-dx-mcp/src/commands/mcp.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@
130130

131131
import {Flags} from '@oclif/core';
132132
import {BaseCommand, MrtCommand, InstanceCommand} from '@salesforce/b2c-tooling-sdk/cli';
133+
import {createTelemetry, type Telemetry} from '@salesforce/b2c-tooling-sdk/telemetry';
133134
import {StdioServerTransport} from '@modelcontextprotocol/sdk/server/stdio.js';
134135
import {B2CDxMcpServer} from '../server.js';
135136
import {Services} from '../services.js';
136137
import {registerToolsets} from '../registry.js';
137-
import {TOOLSETS, type StartupFlags} from '../utils/index.js';
138-
import {Telemetry} from '../utils/telemetry.js';
138+
import {TOOLSETS, type StartupFlags, loadAppInsightsKey} from '../utils/index.js';
139139

140140
/**
141141
* oclif Command that starts the B2C DX MCP server.
@@ -250,28 +250,18 @@ export default class McpServerCommand extends BaseCommand<typeof McpServerComman
250250
// Initialize telemetry unless disabled
251251
let telemetry: Telemetry | undefined;
252252
if (!this.flags['no-telemetry']) {
253-
telemetry = new Telemetry({
254-
toolsets: (startupFlags.toolsets ?? []).join(','),
253+
telemetry = createTelemetry({
254+
project: 'b2c-dx-mcp',
255+
appInsightsKey: loadAppInsightsKey(),
255256
version: this.config.version,
257+
initialAttributes: {
258+
toolsets: (startupFlags.toolsets ?? []).join(', '),
259+
},
256260
});
257261
await telemetry.start();
258-
259-
// Set up process exit handlers to send SERVER_STATUS stopped events
260-
const sendStop = (signal: string): void => {
261-
telemetry?.sendEvent('SERVER_STATUS', {status: 'stopped', signal});
262+
process.stdin.on('close', (err) => {
263+
telemetry?.sendEvent(err ? 'SERVER_STOPPED_ERROR' : 'SERVER_STOPPED_SUCCESS');
262264
telemetry?.stop();
263-
};
264-
265-
process.on('exit', () => sendStop('exit'));
266-
process.on('SIGINT', () => {
267-
sendStop('SIGINT');
268-
// eslint-disable-next-line n/no-process-exit
269-
process.exit(0);
270-
});
271-
process.on('SIGTERM', () => {
272-
sendStop('SIGTERM');
273-
// eslint-disable-next-line n/no-process-exit
274-
process.exit(0);
275265
});
276266
}
277267

packages/b2c-dx-mcp/src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {ServerOptions} from '@modelcontextprotocol/sdk/server/index.js';
1515
import type {RequestHandlerExtra} from '@modelcontextprotocol/sdk/shared/protocol.js';
1616
import type {Transport} from '@modelcontextprotocol/sdk/shared/transport.js';
1717
import type {ZodRawShape} from 'zod';
18-
import type {Telemetry} from './utils/telemetry.js';
18+
import type {Telemetry} from '@salesforce/b2c-tooling-sdk/telemetry';
1919

2020
/**
2121
* Extended server options.

packages/b2c-dx-mcp/src/utils/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
55
*/
66

7+
import fs from 'node:fs';
8+
import path from 'node:path';
9+
import {fileURLToPath} from 'node:url';
10+
711
/**
812
* Utility modules for the B2C DX MCP server.
913
*
@@ -15,3 +19,31 @@
1519
// output needs .js extensions to work at runtime with Node.js ESM.
1620
export * from './constants.js';
1721
export * from './types.js';
22+
23+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
24+
25+
/**
26+
* Load a value from the config.json file.
27+
* @param key - The key to load from the config file
28+
* @returns The value or undefined if not found
29+
*/
30+
function loadConfigValue(key: string): string | undefined {
31+
try {
32+
const cfgPath = path.resolve(__dirname, './config.json');
33+
if (!fs.existsSync(cfgPath)) return undefined;
34+
const raw = fs.readFileSync(cfgPath, 'utf8');
35+
const cfg = JSON.parse(raw) as Record<string, unknown>;
36+
const v = cfg?.[key];
37+
return typeof v === 'string' && v.trim() ? v.trim() : undefined;
38+
} catch {
39+
return undefined;
40+
}
41+
}
42+
43+
/**
44+
* Load the Application Insights connection string from the config.json file.
45+
* @returns The connection string or undefined if not configured
46+
*/
47+
export function loadAppInsightsKey(): string | undefined {
48+
return loadConfigValue('applicationInsightsConnectionString');
49+
}

packages/b2c-tooling-sdk/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,17 @@
221221
"types": "./dist/cjs/test-utils/index.d.ts",
222222
"default": "./dist/cjs/test-utils/index.js"
223223
}
224+
},
225+
"./telemetry": {
226+
"development": "./src/telemetry/index.ts",
227+
"import": {
228+
"types": "./dist/esm/telemetry/index.d.ts",
229+
"default": "./dist/esm/telemetry/index.js"
230+
},
231+
"require": {
232+
"types": "./dist/cjs/telemetry/index.d.ts",
233+
"default": "./dist/cjs/telemetry/index.js"
234+
}
224235
}
225236
},
226237
"main": "./dist/cjs/index.js",
@@ -289,6 +300,7 @@
289300
"node": ">=22.16.0"
290301
},
291302
"dependencies": {
303+
"@salesforce/telemetry": "^6.1.0",
292304
"archiver": "^7.0.1",
293305
"chokidar": "^5.0.0",
294306
"cliui": "^9.0.1",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2025, Salesforce, Inc.
3+
* SPDX-License-Identifier: Apache-2
4+
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
7+
export type {TelemetryAttributes, TelemetryOptions} from './types.js';
8+
export {Telemetry} from './telemetry.js';
9+
10+
import {Telemetry} from './telemetry.js';
11+
import type {TelemetryOptions} from './types.js';
12+
13+
/**
14+
* Factory function to create a Telemetry instance.
15+
*
16+
* @param options - Telemetry configuration options
17+
* @returns A new Telemetry instance
18+
*
19+
* @example
20+
* ```typescript
21+
* import { createTelemetry } from '@salesforce/b2c-tooling-sdk/telemetry';
22+
*
23+
* const telemetry = createTelemetry({
24+
* project: 'my-mcp-server',
25+
* appInsightsKey: process.env.APP_INSIGHTS_KEY,
26+
* version: '1.0.0',
27+
* initialAttributes: { environment: 'production' },
28+
* });
29+
*
30+
* await telemetry.start();
31+
* telemetry.sendEvent('SERVER_STATUS', { status: 'started' });
32+
* ```
33+
*/
34+
export function createTelemetry(options: TelemetryOptions): Telemetry {
35+
return new Telemetry(options);
36+
}

0 commit comments

Comments
 (0)