Azure Monitor OpenTelemetry Exporter client library for JavaScript - version 1.0.0-beta.27
Getting started
This exporter package assumes your application is already instrumented with the OpenTelemetry SDK. Once you are ready to export OpenTelemetry data, you can add this exporter to your application.
Install the package
npm install @azure/monitor-opentelemetry-exporter
Currently supported environments
See our support policy for more details.
Warning: This SDK only works for Node.js environments. Use the Application Insights JavaScript SDK for browser environments.
Prerequisites
Distributed Tracing
Add the exporter to your existing OpenTelemetry Tracer Provider (NodeTracerProvider
/ BasicTracerProvider
)
const { AzureMonitorTraceExporter } = require("@azure/monitor-opentelemetry-exporter");
const { BatchSpanProcessor } = require("@opentelemetry/sdk-trace-base");
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const { Resource } = require("@opentelemetry/resources");
const { SemanticResourceAttributes } = require("@opentelemetry/semantic-conventions");
const tracerProvider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: "basic-service",
}),
});
// Register Tracer Provider as global
tracerProvider.register();
// Create an exporter instance
const exporter = new AzureMonitorTraceExporter({
connectionString:
process.env["APPLICATIONINSIGHTS_CONNECTION_STRING"] || "<your connection string>"
});
// Add the exporter to the Provider
tracerProvider.addSpanProcessor(
new BatchSpanProcessor(exporter, {
bufferTimeout: 15000,
bufferSize: 1000
})
);
Metrics
Add the exporter to your existing OpenTelemetry Meter Provider (MeterProvider
)
const { metrics } = require("@opentelemetry/api");
const { MeterProvider, PeriodicExportingMetricReader } = require("@opentelemetry/sdk-metrics");
const { AzureMonitorMetricExporter } = require("@azure/monitor-opentelemetry-exporter");
// Add the exporter into the MetricReader and register it with the MeterProvider
const exporter = new AzureMonitorMetricExporter({
connectionString:
process.env["APPLICATIONINSIGHTS_CONNECTION_STRING"] || "<your connection string>",
});
const metricReaderOptions = {
exporter: exporter,
};
const metricReader = new PeriodicExportingMetricReader(metricReaderOptions);
const meterProvider = new MeterProvider();
meterProvider.addMetricReader(metricReader);
// Register Meter Provider as global
metrics.setGlobalMeterProvider(meterProvider);
Logs
Add the Log Exporter to your existing OpenTelemetry Logger Provider (LoggerProvider
)
const { logs } = require("@opentelemetry/api-logs");
const { LoggerProvider, BatchLogRecordProcessor } = require("@opentelemetry/sdk-logs");
const { AzureMonitorLogExporter } = require("@azure/monitor-opentelemetry-exporter");
// Add the Log exporter into the logRecordProcessor and register it with the LoggerProvider
const exporter = new AzureMonitorLogExporter({
connectionString:
process.env["APPLICATIONINSIGHTS_CONNECTION_STRING"] || "<your connection string>",
});
const logRecordProcessor = new BatchLogRecordProcessor(exporter);
const loggerProvider = new LoggerProvider();
loggerProvider.addLogRecordProcessor(logRecordProcessor);
// Register logger Provider as global
logs.setGlobalLoggerProvider(loggerProvider);
Sampling
You can enable sampling to limit the amount of telemetry records you receive. In order to enable correct sampling in Application Insights, use the ApplicationInsightsSampler
as shown below.
const { ApplicationInsightsSampler } = require("@azure/monitor-opentelemetry-exporter");
const { BatchSpanProcessor } = require("@opentelemetry/sdk-trace-base");
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const { Resource } = require("@opentelemetry/resources");
const { SemanticResourceAttributes } = require("@opentelemetry/semantic-conventions");
// Sampler expects a sample rate of between 0 and 1 inclusive
// A rate of 0.75 means approximately 75 % of your traces will be sent
const aiSampler = new ApplicationInsightsSampler(0.75);
const provider = new NodeTracerProvider({
sampler: aiSampler,
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: "basic-service",
}),
});
provider.register();
Examples
For complete samples of a few champion scenarios, see the samples/
folder.
Key concepts
For more information on the OpenTelemetry project, please review the OpenTelemetry Specifications.
Troubleshooting
Enable debug logging
You can enable debug logging by changing the logging level of your provider.
const { DiagConsoleLogger, DiagLogLevel, diag } = require("@opentelemetry/api");
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const provider = new NodeTracerProvider();
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.ALL);
provider.register();
Next steps
This exporter is made to be used with the OpenTelemetry JS.
Plugin Registry
To see if a plugin has already been made for a library you are using, please check out the OpenTelemetry Registry.
If you cannot your library in the registry, feel free to suggest a new plugin request at opentelemetry-js-contrib
.
Contributing
If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.
Azure SDK for JavaScript