簡體   English   中英

如何使用 opentelemetry 將數據發送到 azure monitor(app insight)來部署服務?

[英]How to deploy service with using opentelemetry sending data to azure monitor(app insight)?

我有一個簡單的反應 nodejs 應用程序 ( http://23.100.37.60:3080/ ) 托管在 AKS 中,下面是我的 nodejs 后端服務器文件、我的部署清單 yml 文件和我的 docker 文件。 我按照此說明( https://learn.microsoft.com/en-us/azure/azure-monitor/app/opentelemetry-enable?tabs=nodejs )嘗試通過 opentelemetry 將數據發送到 app insight,但從未看到任何數據流入應用程序洞察力。 我想知道我是否配置不正確。 感謝您的幫助!

服務器.js

const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
const { AzureMonitorTraceExporter } = require("@azure/monitor-opentelemetry-exporter");
const { BatchSpanProcessor, SimpleSpanProcessor, ConsoleSpanExporter } = require("@opentelemetry/sdk-trace-base");
const { NodeTracerProvider, NodeTracerConfig } = require("@opentelemetry/sdk-trace-node");
const { MeterProvider, MeterProviderOptions } = require("@opentelemetry/sdk-metrics");
const { trace, context, diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');
const { Resource } = require("@opentelemetry/resources");
const { SemanticResourceAttributes } = require("@opentelemetry/semantic-conventions");
const { registerInstrumentations } = require('@opentelemetry/instrumentation');

const express = require('express');
const path = require('path');
const app = express(),
bodyParser = require("body-parser");
port = 3080;

// place holder for the data
const users = [];

const testResource = new Resource({
  [SemanticResourceAttributes.SERVICE_NAME]: "my-helloworld-service",
  [SemanticResourceAttributes.SERVICE_NAMESPACE]: "my-namespace",
  [SemanticResourceAttributes.SERVICE_INSTANCE_ID]: "my-instance",
});

const tracerProviderConfig = {
  resource: testResource
};
const meterProviderConfig = {
  resource: testResource
};

const provider = new NodeTracerProvider(tracerProviderConfig);
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.ALL);

const meterProvider = new MeterProvider(meterProviderConfig);

// Create an exporter instance.
const exporter = new AzureMonitorTraceExporter({
  connectionString: "my connection string copied from app insight"
});

// Add the exporter to the provider.
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));

provider.register();

registerInstrumentations({
  instrumentations: [new HttpInstrumentation()],
});

const tracer = trace.getTracer("example-basic-tracer-node-server");

app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, '../my-app/build')));

app.get('/api/users', (req, res) => {
  console.log('api/users called!')
  res.json(users);
});

app.post('/api/user', (req, res) => {
  const span = tracer.startSpan("add-user-span");
  const user = req.body.user;
  console.log('Adding user:::::', user);
  users.push(user);
  span.addEvent('a user has been added', {
      'user': JSON.stringify(user)
  })
  span.end();
  res.json("user addedd");
});

app.get('/', (req,res) => {
  res.sendFile(path.join(__dirname, '../my-app/build/index.html'));
});

app.listen(port, () => {
    console.log(`Server listening on the port::${port}`);
});

清單.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: react-webapp
  name: react-webapp
spec:
  replicas: 5
  selector:
    matchLabels:
      app: react-webapp
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: react-webapp
    spec:
      containers:
      - image: danpoc0acr.azurecr.io/dan-poc0-app-sample:v2
        name: webapp
        imagePullPolicy: Always
        resources: {}
        ports:
          - containerPort: 3080 
status: {}

---

apiVersion: v1
kind: Service
metadata:
  name: react-webapp
  labels:
    run: react-webapp
spec:
  ports:
  - port: 3080
    protocol: TCP
  selector:
    app: react-webapp
  type: LoadBalancer

泊塢文件

FROM node:10 AS ui-build
WORKDIR /usr/src/app
COPY my-app/ ./my-app/
RUN cd my-app && npm install && npm run build

FROM node:10 AS server-build
WORKDIR /root/
COPY --from=ui-build /usr/src/app/my-app/build ./my-app/build
COPY api/package*.json ./api/
RUN cd api && npm install
COPY api/server.js ./api/

EXPOSE 3080

CMD ["node", "./api/server.js"]

您問題中的 docker 文件使用圖像節點:10,這兩個版本不受支持:-

  • 應用洞察,以及
  • 開放遙測

查看以下文檔的先決條件部分以了解詳細信息 - 為 .NET、Node.js 和 Python 應用程序啟用 Azure Monitor OpenTelemetry

您可以嘗試使用 AppInsights 和 OpenTelemetry 都支持的較新節點圖像(v14、16 或 18)。

另外,請注意此預覽版的限制 由於不支持“實時指標”,AppInsights 的數據攝取可能會延遲幾分鍾。 等待大約 5 分鍾,讓數據出現在 AppInsights 中。 您可以使用 applicationInsights 日志中的union *查詢來檢查幾分鍾后是否有任何數據到達 AppInsights 工作區。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM