Skip to content

Commit 873e507

Browse files
committed
Move insights connection to .js, minor organization changes
1 parent 3f6bf76 commit 873e507

6 files changed

Lines changed: 64 additions & 33 deletions

File tree

packages/b2c-dx-mcp/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@
5151
"default": "./dist/commands/mcp.js"
5252
}
5353
},
54+
"./config": {
55+
"development": "./src/config.ts",
56+
"import": {
57+
"types": "./dist/config.d.ts",
58+
"default": "./dist/config.js"
59+
}
60+
},
5461
"./tools": {
5562
"development": "./src/tools/index.ts",
5663
"import": {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
* Configuration constants.
8+
*
9+
* @module config
10+
*/
11+
12+
const DEFAULT_APPLICATION_INSIGHTS_CONNECTION_STRING =
13+
'InstrumentationKey=acf013a7-d3b9-49e3-9d26-03fd87dbec0b;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/;ApplicationId=efe01240-2e8f-40c1-938a-534cca8a270b';
14+
15+
/**
16+
* Application Insights connection string for telemetry.
17+
*
18+
* Can be overridden via the `B2C_DX_MCP_APP_INSIGHTS_CONNECTION_STRING` environment variable for testing.
19+
*/
20+
export const APPLICATION_INSIGHTS_CONNECTION_STRING =
21+
process.env.B2C_DX_MCP_APP_INSIGHTS_CONNECTION_STRING ?? DEFAULT_APPLICATION_INSIGHTS_CONNECTION_STRING;

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

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
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';
7+
import {APPLICATION_INSIGHTS_CONNECTION_STRING} from '../config.js';
108

119
/**
1210
* Utility modules for the B2C DX MCP server.
@@ -20,30 +18,10 @@ import {fileURLToPath} from 'node:url';
2018
export * from './constants.js';
2119
export * from './types.js';
2220

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-
4321
/**
44-
* Load the Application Insights connection string from the config.json file.
22+
* Load the Application Insights connection string.
4523
* @returns The connection string or undefined if not configured
4624
*/
4725
export function loadAppInsightsKey(): string | undefined {
48-
return loadConfigValue('applicationInsightsConnectionString');
26+
return APPLICATION_INSIGHTS_CONNECTION_STRING;
4927
}

packages/b2c-tooling-sdk/src/telemetry/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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-
export type {TelemetryAttributes, TelemetryOptions} from './types.js';
7+
export type {TelemetryAttributes, TelemetryEventProperties, TelemetryOptions} from './types.js';
88
export {Telemetry} from './telemetry.js';
99

1010
import {Telemetry} from './telemetry.js';

packages/b2c-tooling-sdk/src/telemetry/telemetry.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import fs from 'node:fs';
99
import path from 'node:path';
1010
import os from 'node:os';
1111
import {TelemetryReporter} from '@salesforce/telemetry';
12-
import type {TelemetryAttributes, TelemetryOptions} from './types.js';
12+
import type {TelemetryAttributes, TelemetryEventProperties, TelemetryOptions} from './types.js';
1313

1414
const generateRandomId = (): string => randomBytes(20).toString('hex');
1515

@@ -118,25 +118,22 @@ export class Telemetry {
118118
*/
119119
sendEvent(eventName: string, attributes: TelemetryAttributes = {}): void {
120120
try {
121-
this.reporter?.sendTelemetryEvent(eventName, {
122-
// TODO create interface for clarity
121+
const eventProperties: TelemetryEventProperties = {
123122
...this.attributes,
124123
...attributes,
125-
// Identifiers
126124
sessionId: this.sessionId,
127125
cliId: this.cliId,
128-
// System information
129126
version: this.version,
130127
platform: process.platform,
131128
arch: process.arch,
132129
nodeVersion: process.version,
133130
nodeEnv: process.env.NODE_ENV,
134131
origin: this.project,
135-
// Timestamps
136132
date: new Date().toUTCString(),
137133
timestamp: String(Date.now()),
138134
processUptime: process.uptime() * 1000,
139-
});
135+
};
136+
this.reporter?.sendTelemetryEvent(eventName, eventProperties);
140137
} catch {
141138
// ignore send errors
142139
}

packages/b2c-tooling-sdk/src/telemetry/types.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,34 @@ export interface TelemetryAttributes {
1111
[key: string]: boolean | number | string | undefined;
1212
}
1313

14+
/**
15+
* Core properties automatically included with every telemetry event.
16+
*/
17+
export interface TelemetryEventProperties extends TelemetryAttributes {
18+
/** Unique session identifier (generated per Telemetry instance) */
19+
sessionId: string;
20+
/** Persistent CLI identifier (stored in ~/.{project}/cliid) */
21+
cliId: string;
22+
/** Package version */
23+
version: string;
24+
/** Operating system platform (e.g., 'darwin', 'linux', 'win32') */
25+
platform: string;
26+
/** CPU architecture (e.g., 'x64', 'arm64') */
27+
arch: string;
28+
/** Node.js version */
29+
nodeVersion: string;
30+
/** NODE_ENV environment variable value */
31+
nodeEnv: string | undefined;
32+
/** Project name / origin of the event */
33+
origin: string;
34+
/** Human-readable UTC date string */
35+
date: string;
36+
/** Unix timestamp in milliseconds */
37+
timestamp: string;
38+
/** Process uptime in milliseconds */
39+
processUptime: number;
40+
}
41+
1442
/**
1543
* Options for creating a Telemetry instance.
1644
*/

0 commit comments

Comments
 (0)