Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit d55678e

Browse files
Blightwidowdraffensperger
authored andcommitted
feature(OcAgentExpoter): Allow to setup the service name from a window variable. (#157)
1 parent dd53697 commit d55678e

File tree

12 files changed

+35
-11
lines changed

12 files changed

+35
-11
lines changed

packages/opencensus-web-core/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-web-core/src/trace/model/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,7 @@ export declare interface WindowWithOcwGlobals extends Window {
5353
* That way the header is not added to all xhrs.
5454
*/
5555
ocTraceHeaderHostRegex?: string | RegExp;
56+
57+
/** Custom name for the service */
58+
ocServiceName?: string;
5659
}

packages/opencensus-web-exporter-ocagent/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-web-initial-load/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-web-initial-load/src/export-initial-load.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ const TRACE_ENDPOINT = '/v1/trace';
3838
/**
3939
* Waits until after the document `load` event fires, and then uses the
4040
* `window.ocAgent` setting to configure an OpenCensus agent exporter and
41-
* export the spans for the initial page load.
41+
* export the spans for the initial page load. You can customize the name
42+
* of the service by specifying `window.ocServiceName`
4243
*/
4344
export function exportRootSpanAfterLoadEvent() {
4445
if (!windowWithOcwGlobals.ocAgent) {
@@ -49,6 +50,7 @@ export function exportRootSpanAfterLoadEvent() {
4950
tracing.registerExporter(
5051
new OCAgentExporter({
5152
agentEndpoint: `${windowWithOcwGlobals.ocAgent}${TRACE_ENDPOINT}`,
53+
serviceName: windowWithOcwGlobals.ocServiceName,
5254
})
5355
);
5456

packages/opencensus-web-initial-load/test/test-export-initial-load.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,26 @@ describe('exportRootSpanAfterLoadEvent', () => {
6767
// Check that trace and span ID from `window.traceparent` are in body sent.
6868
const sendBody = sendSpy.calls.argsFor(0)[0];
6969
expect(sendBody).toContain(hexToBase64(traceId));
70-
expect(sendBody).toContain(hexToBase64(traceId));
70+
expect(sendBody).toContain(hexToBase64(spanId));
71+
});
72+
73+
it('uses service name from window.ocServiceName if specified', () => {
74+
windowWithOcwGlobals.ocAgent = 'http://agent';
75+
windowWithOcwGlobals.ocServiceName = 'serviceName';
76+
windowWithOcwGlobals.traceparent =
77+
'00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01';
78+
79+
exportRootSpanAfterLoadEvent();
80+
81+
jasmine.clock().tick(300000);
82+
expect(XMLHttpRequest.prototype.open).toHaveBeenCalledWith(
83+
'POST',
84+
'http://agent/v1/trace'
85+
);
86+
expect(XMLHttpRequest.prototype.send).toHaveBeenCalledTimes(1);
87+
// Check that serviceName from `window.ocServiceName` is in body sent.
88+
const sendBody = sendSpy.calls.argsFor(0)[0];
89+
expect(sendBody).toContain(windowWithOcwGlobals.ocServiceName);
7190
});
7291

7392
it('does not export spans if traceparent sampling hint set to zero', () => {

packages/opencensus-web-instrumentation-perf/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-web-instrumentation-zone-peer-dep/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-web-instrumentation-zone/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-web-propagation-tracecontext/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)