在 Nodejs 容器应用程序的应用程序洞察中启用实时指标

问题描述 投票:0回答:1

我使用了这个 GitHub 存储库(https://github.com/Azure-Samples/azure-monitor-opentelemetry-node.js.git),它在应用程序启用 Azure Monitor OpenTelemetry for .NET、Node.js 中给出。 Node.js、Python 和 Java 应用程序(Microsoft azure 文档)。 那么如何为此启用实时指标呢?

如果我使用它,我可以可视化应用程序地图(带有请求/失败)。但我无法获得实时指标。 那么如何使用 OpenTelemetry 为该应用程序启用实时指标?

node.js azure azure-application-insights open-telemetry
1个回答
0
投票

如果我使用它,我可以可视化应用程序地图(带有请求/失败)。但我无法获得实时指标。那么如何使用 OpenTelemetry 为该应用程序启用实时指标?

更新

OTLP Exporter
使用
AzureMonitorTraceExporter
而不是
OTLPTraceExporter
AzureMonitorTraceExporter
旨在将遥测数据发送到 Azure Monitor。

  • 添加指标工具来收集指标数据,例如 CPU、内存和其他性能指标。

azureMonitor.ts:

import { useAzureMonitor, AzureMonitorOpenTelemetryOptions, AzureMonitorTraceExporter } from "@azure/monitor-opentelemetry";
import { trace, metrics, Span, SpanKind, TraceFlags } from '@opentelemetry/api';
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { Resource } from "@opentelemetry/resources";
import { SEMATTRS_ENDUSER_ID, SEMATTRS_HTTP_CLIENT_IP, SEMRESATTRS_SERVICE_INSTANCE_ID, SEMRESATTRS_SERVICE_NAME, SEMRESATTRS_SERVICE_NAMESPACE, SemanticAttributes, SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
import { BatchSpanProcessor, ReadableSpan, SpanProcessor } from "@opentelemetry/sdk-trace-base";
import { HttpInstrumentationConfig } from "@opentelemetry/instrumentation-http";
import { NodeMetricExporter } from "@opentelemetry/exporter-node-metrics";

export function initializeTelemetry() {
    const customResource = new Resource({
        [SEMRESATTRS_SERVICE_NAME]: "my-helloworld-service",
        [SEMRESATTRS_SERVICE_NAMESPACE]: "my-namespace",
        [SEMRESATTRS_SERVICE_INSTANCE_ID]: "my-instance"
    });

    const options: AzureMonitorOpenTelemetryOptions = {
        resource: customResource,
        instrumentationOptions: {
            azureSdk: { enabled: true },
            mongoDb: { enabled: true },
            mySql: { enabled: true },
            postgreSql: { enabled: true },
            redis: { enabled: true },
            redis4: { enabled: true },
        },
    };

    addSpanProcessor(options);
    addOTLPExporter(options);
    useAzureMonitor(options);
    addMetricsInstrumentation();
}

function addMetricsInstrumentation() {
    const exporter = new NodeMetricExporter();
    metrics.addMetricProcessor(exporter);
}

function addSpanProcessor(options: AzureMonitorOpenTelemetryOptions) {
    class SpanEnrichingProcessor implements SpanProcessor {
        forceFlush(): Promise<void> {
            return Promise.resolve();
        }
        shutdown(): Promise<void> {
            return Promise.resolve();
        }
        onStart(_span: Span): void {}
        onEnd(span: ReadableSpan) {
            if (span.kind == SpanKind.INTERNAL) {
                span.spanContext().traceFlags = TraceFlags.NONE;
            } else {
                span.attributes["CustomDimension1"] = "value1";
                span.attributes["CustomDimension2"] = "value2";
                span.attributes[SEMATTRS_HTTP_CLIENT_IP] = "<IP Address>";
                span.attributes[SEMATTRS_ENDUSER_ID] = "<User ID>";
            }
        }
    }

    if (options.spanProcessors?.length > 0) {
        options.spanProcessors.push(new SpanEnrichingProcessor());
    } else {
        options.spanProcessors = [new SpanEnrichingProcessor()];
    }
}

function addOTLPExporter(options: AzureMonitorOpenTelemetryOptions) {
    const traceExporter = new AzureMonitorTraceExporter();
    if (options.spanProcessors?.length > 0) {
        options.spanProcessors.push(new BatchSpanProcessor(traceExporter));
    } else {
        options.spanProcessors = [new BatchSpanProcessor(traceExporter)];
    }
}
  • 添加了使用
    NodeMetricExporter
    收集指标数据的指标检测。更新了资源创建以使用
    new Resource()
    构造函数。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.