kibana/scripts/phoenix.js
Dario Gieselaar 2387e3b88c
[Inference] Instrument inference with OpenTelemetry (#218694)
Instrument the inference chatComplete API with OpenTelemetry, and export
helper functions to create spans w/ the right semconv attributes.
Additionally, optionally export to Langfuse or Phoenix.

## Centralizes OpenTelemetry setup

As this is the first instance of OpenTelemetry based _tracing_ (we
already have metrics in the MonitoringCollection plugin), some
bootstrapping code is necessary to centrally configure OpenTelemetry. To
this end, I've added the following config settings:

- `telemetry.tracing.enabled`: whether OpenTelemetry tracing is enabled
(defaults to undefined, if undefined, falls back to `telemetry.enabled`)
- `telemetry.tracing.sample_rate` (defaults to 1)

The naming of these configuration settings is mostly in-line with [the
Elasticsearch tracing
settings](https://github.com/elastic/elasticsearch/blob/main/TRACING.md).

The following packages (containing bootstrapping logic, utility
functions, types and config schemas) were added:
- `@kbn/telemetry`
- `@kbn/telemetry-config` 
- `@kbn/tracing`

The OpenTelemetry bootstrapping depends on @kbn/apm-config-loader, as it
has the same constraints - it needs to run before any other code, and it
needs to read the raw config.

Additionally, a root `telemetry` logger was added that captures
OpenTelemetry logs.

Note that there is no default exporter for spans, which means that
although spans are being recorded, they do not get exported.

## Instrument chatComplete calls

Calls to `chatComplete` now create OpenTelemetry spans, roughly
following semantic conventions (which for GenAI are very much in flux).
Some helper functions were added to create other inference spans. These
helper functions use baggage to determine whether the created inference
span is the "root" of an inference trace. This allows us to export these
spans as if it were root spans - something that is needed to be able to
easily visualize these in other tools.

Leveraging these inference spans, two exporters are added. One for
[Phoenix](https://github.com/Arize-ai/phoenix) and one for
[Langfuse](https://github.com/langfuse/langfuse/tree/main): two
open-source LLM Observability suites. This allows engineers that use the
Inference plugin to be able to inspect and improve their LLM-based
workflows with much less effort.

For both Phoenix and Langfuse, two service scripts were added. Run `node
scripts/phoenix` or `node scripts/langfuse` to get started. Both scripts
work with zero-config - they will log generated Kibana config to stdout.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
2025-05-07 11:44:29 +02:00

11 lines
550 B
JavaScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
require('../src/setup_node_env');
require('@kbn/inference-cli/scripts/phoenix');