Helpers for reporting telemetry in Cloudpack.
A library for reporting Cloudpack telemetry to Application Insights using Open Telemetry standards.
Telemetry is only reported by the Cloudpack CLI if a connection string is configured (telemetry.connectionString in cloudpack.config.json, or CLOUDPACK_TELEMETRY_CONNECTION_STRING environment variable). It will only be sent to your own Application Insights instance referenced by the connection string, not to Microsoft.
Currently, this library only supports Tracing. Metrics and Logs can be added later when needed.
1. Create a client:
``ts`
const telemetryClient = new TelemetryClient({
serviceNamespace: 'cloudpack',
serviceName: 'cli',
productVersion: '1.2.3',
instrumentationKey: '
logLevel: 'verbose',
});
2. Add shared attributes
`ts`
telemetryClient.setSharedSpanAttribute('sessionId', 'my-session-id');
These attributes will get added to all spans.
3. Get the tracer
`ts`
const tracer = telemetryClient.getTracer();
4. Create a span / add events / end the span
`ts
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();
`
5. Shutdown TelemetryClient before application exists
`ts``
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