@ms-cloudpack/telemetry
v0.10.6
Published
Helpers for reporting telemetry in Cloudpack.
Downloads
2,847
Keywords
Readme
@ms-cloudpack/telemetry
A library for telemetry that gets reported to Application Insights by using Open Telemetry standards.
Currently, this library only supports Tracing. Metrics and Logs can be added later when needed.
Example usage
- Create a client:
const telemetryClient = new TelemetryClient({
serviceNamespace: 'cloudpack',
serviceName: 'cli',
productVersion: '1.2.3',
instrumentationKey: '<Application insights Instrumentation Key>',
logLevel: 'verbose',
});
- Add shared attributes
telemetryClient.setSharedSpanAttribute('sessionId', 'my-session-id');
These attributes will get added to all spans.
- Get the tracer
const tracer = telemetryClient.getTracer();
- Create a span / add events / end the span
const span = tracer.createSpan('my-unit-of-work');
// do work
span.addEvent('my-event', { myCustomAttribute: 'my-custom-value' });
try {
// do more work
} catch (err) {
span.recordException(err);
}
// end the span when the unit-of-work completed.
span.end();
- Shutdown TelemetryClient before application exists
telemetryClient.shutdown();
- This will force flush all the remaining data to remote servers and prevent more data to be collected.
- Make sure that all spans have been ended before shuting down the telemetry client