npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@elastic/opentelemetry-instrumentation-openai

v0.4.1

Published

OpenTelemetry instrumentation for the `openai` OpenAI client library

Downloads

381

Readme

Elastic's OpenTelemetry instrumentation for openai

This module, @elastic/opentelemetry-instrumentation-openai, provides automatic instrumentation of openai, the OpenAI Node.js client library.

It attempts to track the GenAI semantic conventions.

Status

Instrumented OpenAI API endpoints:

Supported versions

  • This instruments the openai package in the range: >=4.19.0 <5.
  • This supports Node.js 18 and later. (openai@4 currently only tests with Node.js v18.)

Semantic Conventions

This instrumentation currently implements version 1.29.0 of the GenAI semantic-conventions: https://opentelemetry.io/docs/specs/semconv/gen-ai/

Installation

npm install @elastic/opentelemetry-instrumentation-openai

Usage

First install the packages used in the example:

npm install openai \
  @opentelemetry/sdk-node \
  @elastic/opentelemetry-instrumentation-openai

Save this to a file, say "example.js". (This example shows the OTel setup code and app code in the same file. Typically, the OTel setup code would be in a separate file and run via node -r .... See a more complete OTel setup example here.)

const {NodeSDK} = require('@opentelemetry/sdk-node');
const {OpenAIInstrumentation} = require('@elastic/opentelemetry-instrumentation-openai');
const sdk = new NodeSDK({
    instrumentations: [
        new OpenAIInstrumentation({
            // See the "Configuration" section below.
            captureMessageContent: true,
        })
    ]
})
sdk.start();
process.once('beforeExit', async () => { await sdk.shutdown() });

const OpenAI = require('openai');
async function main() {
    const openai = new OpenAI();
    const result = await openai.chat.completions.create({
        model: 'gpt-4o-mini',
        messages: [
            {role: 'user', content: 'Say hello world.'}
        ]
    });
    console.log(result.choices[0]?.message?.content);
}
main();

Then run it:

OPENAI_API_KEY=sk-... \
    node example.js

By default, the NodeSDK will export telemetry via OTLP. As a first example to see the telemetry on the console use:

OTEL_TRACES_EXPORTER=console \
    OTEL_LOGS_EXPORTER=console \
    OTEL_METRICS_EXPORTER=console \
    OPENAI_API_KEY=sk-... \
    node example.js

Examples

In the "examples/" directory, use-chat.js is a simple script using the OpenAI Chat Completion API. First, run the script without instrumentation.

Using OpenAI:

OPENAI_API_KEY=sk-... \
    node use-chat.js

Using Azure OpenAI (this assumes your Azure OpenAI endpoint has a model deployment with the name 'gpt-4o-mini'):

AZURE_OPENAI_ENDPOINT=https://YOUR-ENDPOINT-NAME.openai.azure.com \
    AZURE_OPENAI_API_KEY=... \
    OPENAI_API_VERSION=2024-10-01-preview \
    node use-chat.js

Using Ollama (a tool for running LLMs locally, it exposes an OpenAI-compatible API):

ollama serve

# When using Ollama, we default to qwen2.5:0.5b, which is a small model. You
# can choose a larger one, or a different tool capable model like mistral-nemo.
export MODEL_CHAT=qwen2.5
ollama pull $MODEL_CHAT

OPENAI_BASE_URL=http://localhost:11434/v1 \
    node use-chat.js

Now, to run with instrumentation, you can use examples/telemetry.js to bootstrap the OpenTelemetry SDK using this instrumentation. Add the Node.js -r ./telemetry.js option to bootstrap before the script runs. For example:

# Configure the OTel SDK as appropriate for your setup:
export OTEL_EXPORTER_OTLP_ENDPOINT=https://{your-otlp-endpoint.example.com}
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=..."
export OTEL_SERVICE_NAME=my-service

OPENAI_API_KEY=sk-... \
    node -r ./telemetry.js use-chat.js

Configuration

| Option | Type | Description | |-------------------------|-----------|-------------| | captureMessageContent | boolean | Enable capture of content data, such as prompt and completion content. Default false to avoid possible exposure of sensitive data. OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT environment variable overrides. |

For example:

cd examples
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true \
    OPENAI_API_KEY=sk-... \
    node -r ./telemetry.js use-chat.js

ESM

OpenTelemetry instrumentation of ECMAScript Module (ESM) code -- code using import ... rather than require(...) -- is experimental and very limited. This section shows that it is possible to get instrumentation of openai working with ESM code.

npm install
npm run compile
cd examples
node --import ./telemetry.mjs use-chat-esm.mjs

See the comments in examples/telemetry.mjs for limitations with this. The limitations are with OpenTelemetry JS, not with this instrumentation.

(TODO: Create and point to a follow-up issue(s) for necessary OTel JS work for this support.)