Condividi tramite


Libreria di strumentazione OpenTelemetry di Azure per JavaScript

Introduttiva

Ambienti attualmente supportati

Per altri dettagli, vedere i criteri di supporto .

Prerequisiti

È necessario configurare OpenTelemetry SDK per produrre dati di telemetria. Durante la configurazione di OpenTelemetry non rientra nell'ambito di questo file README, è consigliabile esaminare la documentazione di OpenTelemetry per iniziare a usare OpenTelemetry.

Installare il pacchetto @azure/opentelemetry-instrumentation-azure-sdk

Installare la libreria client di Strumentazione dati OpenTelemetry di Azure con npm:

npm install @azure/opentelemetry-instrumentation-azure-sdk

Supporto browser

JavaScript Bundle

Per usare questa libreria client nel browser, è prima necessario usare un bundler. Per informazioni dettagliate su come eseguire questa operazione, vedere la documentazione di creazione di bundle .

Concetti chiave

  • La funzione createAzureSdkInstrumentation è l'hook principale esportato da questa libreria che consente di creare un oggetto strumentazione di Azure SDK da registrare con OpenTelemetry.

Esempi

Abilitare la strumentazione OpenTelemetry

import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import { SimpleSpanProcessor, ConsoleSpanExporter } from "@opentelemetry/tracing";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { createAzureSdkInstrumentation } from "@azure/opentelemetry-instrumentation-azure-sdk";
import { KeyClient } from "@azure/keyvault-keys";
import { DefaultAzureCredential } from "@azure/identity";

// Set-up and configure a Node Tracer Provider using OpenTelemetry SDK.
const provider = new NodeTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register();

registerInstrumentations({
  instrumentations: [createAzureSdkInstrumentation()],
});

// Continue to import any Azure SDK client libraries after registering the instrumentation.
// import { KeyClient } from "@azure/keyvault-keys";
// import { DefaultAzureCredential } from "@azure/identity";

const keyClient = new KeyClient("https://my.keyvault.azure.net", new DefaultAzureCredential());

// Tracing is now enabled using automatic span propagation with an active context.
await keyClient.getKey("MyKeyName");

// If your scenario requires manual span propagation, all Azure client libraries
// support explicitly passing a parent context via an `options` parameter.
// Get a tracer from a registered provider, create a span, and get the current context.
const tracer = opentelemetry.trace.getTracer("my-tracer");
const span = tracer.startSpan("main");
const ctx = opentelemetry.trace.setSpan(opentelemetry.context.active(), span);

await keyClient.getKey("MyKeyName", {
  tracingOptions: {
    // ctx will be used as the parent context for all operations.
    tracingContext: ctx,
  },
});

Risoluzione dei problemi

Registrazione

L'abilitazione della registrazione può aiutare a individuare informazioni utili sugli errori. Per visualizzare un log di richieste e risposte HTTP, impostare la variabile di ambiente AZURE_LOG_LEVEL su info. In alternativa, la registrazione può essere abilitata in fase di esecuzione chiamando setLogLevel nel @azure/logger:

import { setLogLevel } from "@azure/logger";

setLogLevel("info");

Per istruzioni più dettagliate su come abilitare i log, è possibile esaminare la documentazione del pacchetto @azure/logger.

Strumentazione per i moduli ES

Questo pacchetto usa @opentelemetry/strumentazione per configurare gli hook e i caricatori necessari. Per istruzioni sulla configurazione della traccia per i pacchetti ESM, vedere README di @opentelemetry/strumentazione.

Contribuire

Per contribuire a questa libreria, leggere la guida contribuire per altre informazioni su come compilare e testare il codice.