你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

适用于 JavaScript 的 Azure Monitor OpenTelemetry 导出程序客户端库 - 版本 1.0.0-beta.27

npm 版本

开始

此导出程序包假定应用程序已使用 OpenTelemetry SDK 检测 。 准备好导出 OpenTelemetry 数据后,可以将此导出程序添加到应用程序。

安装包

npm install @azure/monitor-opentelemetry-exporter

当前支持的环境

有关详细信息,请参阅我们的 支持策略

警告: 此 SDK 仅适用于 Node.js 环境。 将 Application Insights JavaScript SDK 用于浏览器环境。

先决条件

分布式跟踪

将导出程序添加到现有的 OpenTelemetry Tracer 提供程序(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
  })
);

指标

将导出程序添加到现有的 OpenTelemetry 计量提供程序(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);

原木

将日志导出程序添加到现有的 OpenTelemetry Logger 提供程序(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);

采样

可以启用采样来限制收到的遥测记录量。 若要在 Application Insights 中启用正确的采样,请使用 ApplicationInsightsSampler,如下所示。

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();

例子

有关几个冠军方案的完整示例,请参阅 samples/ 文件夹。

关键概念

有关 OpenTelemetry 项目的详细信息,请查看 OpenTelemetry 规范

故障 排除

启用调试日志记录

可以通过更改提供程序的日志记录级别来启用调试日志记录。

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();

后续步骤

此导出程序用于 OpenTelemetry JS

插件注册表

若要查看是否已为正在使用的库创建插件,请查看 OpenTelemetry 注册表

如果无法在注册表中查找库,请随时在 opentelemetry-js-contrib建议新的插件请求。

贡献

若要参与此库,请阅读 贡献指南 了解有关如何生成和测试代码的详细信息。

印象