Monitor HuggingFace using OpenTelemetry
Shield360 uses OpenTelemetry Auto-Instrumentation to help you monitor LLM applications built using models from HuggingFace. This includes tracking performance, token usage, costs, and how users interact with the application.
Auto-instrumentation means you don’t have to set up monitoring manually for different LLMs, frameworks, or databases. By simply adding Shield360 in your application, all the necessary monitoring configurations are automatically set up.
The integration is compatible with
- HuggingFace Transformers Python SDK client
>= 4.48.0 - HuggingFace Inference TypeScript SDK (
@huggingface/inference)>= 2.0.0 - Transformers.js (
@huggingface/transformers>= 3or@xenova/transformers) for local inference
Supported APIs
Section titled “Supported APIs”| SDK | Instrumented surface | Inference type |
|---|---|---|
| Python | TextGenerationPipeline.__call__ | Local (transformers) |
| TypeScript | @huggingface/inference chat completions | Remote (Inference API) |
| TypeScript | Transformers.js pipeline calls (@huggingface/transformers / @xenova/transformers) | Local |
Local text-generation is reported as the chat operation to match the Python SDK. Other local Transformers.js pipelines (summarization, translation, fill-mask, question-answering, classification) are reported as text_completion, and feature-extraction / sentence-similarity as embeddings.
Prerequisites
Section titled “Prerequisites”- Install the HuggingFace SDK yourself. Shield360 does not bundle it as a dependency.
- For TypeScript, call
shield360.init()before the HuggingFace module is first loaded so OpenTelemetry can hook into the SDK at runtime.
TypeScript example (local Transformers.js)
Section titled “TypeScript example (local Transformers.js)”import shield360 from "shield360";
shield360.init({ otlpEndpoint: "YOUR_OTEL_ENDPOINT" });
import { pipeline } from "@huggingface/transformers";
const generator = await pipeline("text-generation", "Xenova/distilgpt2");const output = await generator("OpenTelemetry makes observability", { max_new_tokens: 32, temperature: 0.7,});Configuration
Section titled “Configuration”| Option | Behavior |
|---|---|
captureMessageContent | Gates gen_ai.input.messages / gen_ai.output.messages on spans and in events |
disableEvents | Suppresses all inference events |
disableMetrics | Global - metrics instruments are not set up |
disabledInstrumentors: ['transformers'] | Disables the local Transformers.js instrumentation |
disabledInstrumentors: ['huggingface'] | Disables the remote Inference API instrumentation |
Get started
Section titled “Get started”Open your command line or terminal and run:
pip install shield360npm install shield360# Remote Inference APInpm install @huggingface/inference# or local inference with Transformers.jsnpm install @huggingface/transformersThe older @xenova/transformers package is also supported for local inference.
Perfect for existing applications - no code modifications needed:
# Configure via CLI argumentsshield360-instrument \ --service-name my-ai-app \ --environment production \ --otlp-endpoint YOUR_OTEL_ENDPOINT \ python your_app.py# Configure via environment variablesexport OTEL_SERVICE_NAME=my-ai-appexport OTEL_DEPLOYMENT_ENVIRONMENT=productionexport OTEL_EXPORTER_OTLP_ENDPOINT=YOUR_OTEL_ENDPOINT
# Run with zero code changesshield360-instrument python your_app.pyimport shield360
shield360.init(otlp_endpoint="YOUR_OTEL_ENDPOINT")Add the following two lines to your application code:
import shield360
shield360.init()Then, configure the your OTLP endpoint using environment variable:
export OTEL_EXPORTER_OTLP_ENDPOINT=YOUR_OTEL_ENDPOINTimport shield360 from "shield360"
shield360.init({ otlpEndpoint: "YOUR_OTEL_ENDPOINT" })Add the following two lines to your application code:
import shield360 from "shield360"
shield360.init()Then, configure the your OTLP endpoint using environment variable:
export OTEL_EXPORTER_OTLP_ENDPOINT=YOUR_OTEL_ENDPOINTReplace: YOUR_OTEL_ENDPOINT with the URL of your OpenTelemetry backend, such as http://127.0.0.1:4318 if you are using Shield360 and a local OTel Collector.
To send metrics and traces to other Observability tools, refer to the supported destinations.
For more advanced configurations and application use cases, visit the SDK configuration reference or TypeScript SDK reference.