Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc(instr-openai): improve the README first usage example #475

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions packages/instrumentation-openai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,27 @@ npm install @elastic/opentelemetry-instrumentation-openai

# Usage

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 example at "test/fixtures/telemetry.js".
First install the packages used in the example:

```bash
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](./test/fixtures/telemetry.js).)

```js
const {NodeSDK, tracing, api} = require('@opentelemetry/sdk-node');
const {HttpInstrumentation} = require('@opentelemetry/instrumentation-http');
const {NodeSDK} = require('@opentelemetry/sdk-node');
const {OpenAIInstrumentation} = require('@elastic/opentelemetry-instrumentation-openai');
const sdk = new NodeSDK({
spanProcessor: new tracing.SimpleSpanProcessor(new tracing.ConsoleSpanExporter()),
instrumentations: [
// HTTP instrumentation is not required, but it can be interesting to see
// openai and http spans in the trace.
new HttpInstrumentation(),
new OpenAIInstrumentation({
// See below for OpenAI instrumentation configuration.
// See the "Configuration" section below.
captureMessageContent: true,
})
]
})
Expand All @@ -68,6 +73,25 @@ async function main() {
});
console.log(result.choices[0]?.message?.content);
}
main();
```

Then run it:

```bash
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:

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


Expand Down
Loading