Skip to content

Commit 51b7330

Browse files
Merge pull request #472 from forcedotcom/W-21215399-fix-telemetry-with-schema
fix: @W-21215399 - send only caller attributes in sendTelemetryEventWithSchema
2 parents 43e5e68 + c97a034 commit 51b7330

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ const reporter = await TelemetryReporter.create({
107107
reporter.start();
108108
// Default events use the default schema
109109
reporter.sendTelemetryEvent('event-name', { foo: 'bar' });
110-
// PFT or other schema-specific events: pass schema per call
111-
reporter.sendTelemetryEventWithSchema('pftEventName', { userId: 'user-1', action: 'view' }, pdpEventSchema);
110+
// Schema-specific events: pass only attributes (must include all fields required by the schema; no properties added by reporter)
111+
reporter.sendTelemetryEventWithSchema({ userId: 'user-1', action: 'view', eventName: 'pftEventName' }, pdpEventSchema);
112112
```
113113

114114
**Note:** `sendTelemetryEventWithSchema` sends only to O11y (not AppInsights). Use it for events that must conform to a given schema; use `sendTelemetryEvent` for all other events.

src/o11yReporter.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,20 @@ export class O11yReporter extends BaseReporter {
101101
* Use this method when you need to send events that conform to a particular schema
102102
* (e.g. PFT/pdpEventSchema). Only the events you send via this method use the given schema;
103103
* all other events use the default schema via sendTelemetryEvent.
104+
* Only the caller-provided attributes are sent; no properties (e.g. eventName, common.*)
105+
* are added by the reporter.
104106
*
105-
* @param eventName - Name of the event
106-
* @param attributes - Properties and measurements to publish alongside the event
107-
* @param schema - O11y schema object (e.g. from o11y_schema package)
107+
* The schema is the O11y encoding schema (e.g. from the o11y_schema package), not JSON Schema,
108+
* so this library does not derive or run validation here. Callers should validate attributes
109+
* (e.g. with zod) before calling, using the same schema context they use when importing the schema.
110+
*
111+
* @param attributes - Properties and measurements to publish (only these are sent; no properties added)
112+
* @param schema - O11y encoding schema (e.g. from o11y_schema package)
108113
*/
109-
public async sendTelemetryEventWithSchema(
110-
eventName: string,
111-
attributes: Attributes,
112-
schema: O11ySchema
113-
): Promise<void> {
114+
public async sendTelemetryEventWithSchema(attributes: Attributes, schema: O11ySchema): Promise<void> {
114115
await this.initialized;
115116

116-
const merged = { ...this.commonProperties, ...attributes };
117-
118-
const eventData: { [key: string]: unknown } = {
119-
eventName: `${this.extensionName}/${eventName}`,
120-
...merged,
121-
};
117+
const eventData: { [key: string]: unknown } = { ...attributes };
122118

123119
this.service.logEventWithSchema(eventData, schema);
124120

src/telemetryReporter.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,18 @@ export class TelemetryReporter extends AsyncCreatable<TelemetryOptions> {
163163
* Sends a telemetry event to O11y only with a specific schema.
164164
* Use this for events that must conform to a given schema (e.g. PFT/pdpEventSchema).
165165
* Does not send to AppInsights. Only sends when O11y is enabled and reporter is initialized.
166+
* Only the caller-provided attributes are sent; no properties are added by the reporter.
166167
*
167-
* @param eventName - Name of the event
168-
* @param attributes - Properties and measurements to publish alongside the event
169-
* @param schema - O11y schema object (e.g. from o11y_schema package)
168+
* The schema is the O11y encoding schema (e.g. from the o11y_schema package), not JSON Schema,
169+
* so this library does not derive or run validation here. Callers should validate attributes
170+
* (e.g. with zod) before calling, using the same schema context they use when importing the schema.
171+
*
172+
* @param attributes - Properties and measurements to publish (only these are sent)
173+
* @param schema - O11y encoding schema (e.g. from o11y_schema package)
170174
*/
171-
public sendTelemetryEventWithSchema(eventName: string, attributes: Attributes, schema: O11ySchema): void {
175+
public sendTelemetryEventWithSchema(attributes: Attributes, schema: O11ySchema): void {
172176
if (this.isSfdxTelemetryEnabled() && this.enableO11y && this.o11yReporter) {
173-
void this.o11yReporter.sendTelemetryEventWithSchema(eventName, attributes, schema).catch((error) => {
177+
void this.o11yReporter.sendTelemetryEventWithSchema(attributes, schema).catch((error) => {
174178
this.logger.debug('Failed to send event with schema to O11y:', error);
175179
});
176180
}

test/unit/o11yReporter.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,31 +113,31 @@ describe('O11yReporter', () => {
113113
reporter = new O11yReporter({ project, key, extensionName, o11yUploadEndpoint });
114114
});
115115

116-
it('should call logEventWithSchema with given schema and event data', async () => {
116+
it('should call logEventWithSchema with given schema and caller attributes only', async () => {
117117
const schema = { name: 'pdpEventSchema', version: '1.0' };
118-
const eventName = 'pftEvent';
119118
const attributes = { userId: 'user-1', action: 'view' };
120119

121-
await reporter.sendTelemetryEventWithSchema(eventName, attributes, schema);
120+
await reporter.sendTelemetryEventWithSchema(attributes, schema);
122121

123122
expect(mockO11yService.logEventWithSchema.called).to.be.true;
124123
expect(mockO11yService.logEvent.called).to.be.false;
125124
expect(mockO11yService.forceFlush.called).to.be.true;
126125

127126
const callArgs = mockO11yService.logEventWithSchema.firstCall.args;
128-
expect(callArgs[0].eventName).to.equal(`${extensionName}/${eventName}`);
127+
expect(callArgs[0]).to.deep.equal(attributes);
129128
expect(callArgs[0].userId).to.equal('user-1');
130129
expect(callArgs[0].action).to.equal('view');
131130
expect(callArgs[1]).to.equal(schema);
132131
});
133132

134-
it('should merge common properties with attributes', async () => {
133+
it('should include only caller-provided attributes (no eventName or common properties)', async () => {
135134
const schema = { name: 'custom-schema', version: '1.0' };
136-
await reporter.sendTelemetryEventWithSchema('myEvent', { foo: 'bar' }, schema);
135+
await reporter.sendTelemetryEventWithSchema({ foo: 'bar' }, schema);
137136

138137
const eventData = mockO11yService.logEventWithSchema.firstCall.args[0];
139-
expect(eventData['common.extensionName']).to.equal(extensionName);
140138
expect(eventData.foo).to.equal('bar');
139+
expect(eventData.eventName).to.be.undefined;
140+
expect(eventData['common.extensionName']).to.be.undefined;
141141
});
142142
});
143143

0 commit comments

Comments
 (0)