Best Practices for Integrating Netra
Follow these proven patterns to get the most out of Netra’s observability, evaluation, and simulation capabilities — from first setup through production at scale.Getting Netra running takes minutes. Getting it running well takes intention. This guide distills the patterns that high-performing teams follow when integrating Netra into production AI systems.
Initialization
Initialize Early, Initialize Once
Netra.init() must be the first thing your application does — before importing or using any LLM provider, framework, or vector database client. Netra patches supported libraries at initialization time; if a library is imported first, its calls won’t be captured.
- Python
- TypeScript
Always Shut Down Gracefully
Netra batches spans before exporting. If your application exits without callingshutdown(), pending spans may be lost — especially in short-lived processes like serverless functions, scripts, or CLI tools.
- Python
- TypeScript
Use Environment Variables for Credentials
Avoid hardcoding API keys in source code. Use environment variables and let the SDK resolve them automatically:NETRA_API_KEY and NETRA_OTLP_ENDPOINT automatically, keeping credentials out of your codebase.
Instrumentation Strategy
Follow the Three-Layer Approach
Adopt instrumentation incrementally. Each layer adds visibility without requiring you to rewrite existing code:Start with auto-instrumentation
Add decorators for business context
@workflow, @agent, and @task decorators to create meaningful span hierarchies that map to your application’s domain logic.Use manual spans for edge cases
Be Selective with Instruments
Don’t instrument everything. Noisy traces from HTTP clients, health checks, or internal utilities obscure the signals that matter.- Python
- TypeScript
blockedSpans parameter supports wildcards — "internal.*" blocks all spans starting with internal., and "*.debug" blocks spans ending with .debug.Context Tracking
Always Set User, Session, and Tenant Context
Context attributes are the foundation of effective filtering, grouping, and analytics in the Netra dashboard. Set them as early as possible in your request lifecycle.- Python
- TypeScript
Always Set Root Input and Output
Root input and output are the most important attributes on a trace. They let you see what went in and what came out at a glance — without expanding span trees.- Python
- TypeScript
Streaming Responses
For streaming outputs, accumulate chunks and set root output after iteration completes:- Python — Raw Stream
- Python — SSE/Generator
- TypeScript — SSE/Express
Decorator Best Practices
Map Decorators to Your Domain
Use decorators to create a span hierarchy that mirrors your application’s architecture:- Python
- TypeScript
"experimentalDecorators": true in your tsconfig.json.Manual Span Best Practices
Always End Spans
In TypeScript, manual spans must be ended explicitly. Usetry/finally to guarantee cleanup, or prefer startActiveSpan() for automatic lifecycle management.
- Python — Context Manager (auto-closes)
- TypeScript — try/finally
- TypeScript — startActiveSpan (recommended)
Environment Configuration
Use Separate Environments
Tag traces by environment to keep production data clean and make it easy to filter in the dashboard:Add Resource Attributes for Rich Metadata
Attach deployment metadata to every span usingresource_attributes / resourceAttributes:
- Python
- TypeScript
Privacy and Security
Control Content Capture
In production, you may want to disable prompt and response content capture for privacy compliance:Enable PII Scrubbing (Python)
For applications handling sensitive data, enable automatic PII detection and redaction:Common Pitfalls
Avoid these frequently encountered mistakes when integrating Netra:Initializing after importing providers
Initializing after importing providers
Netra.init() was called after importing the provider library. Netra patches libraries at init time — if they’re already imported, the patches don’t apply.Fix: Move Netra.init() to the very top of your entrypoint, before any provider imports.Forgetting to await init in TypeScript
Forgetting to await init in TypeScript
Netra.init() in TypeScript is async. Without await, instrumentation may not be ready when LLM calls are made.Fix: Always await Netra.init({ ... }).Missing root input and output
Missing root input and output
set_root_input / set_root_output were never called.Fix: Call them at the entry and exit points of your top-level workflow. This is the single most impactful practice for trace readability.Not calling shutdown in short-lived processes
Not calling shutdown in short-lived processes
Netra.shutdown() (or await Netra.shutdown() in TypeScript) before the process exits.Mixing Python and TypeScript conventions
Mixing Python and TypeScript conventions
snake_case in TypeScript (app_name) or camelCase in Python (appName).Fix: Python uses snake_case (app_name, trace_content, set_user_id). TypeScript uses camelCase (appName, traceContent, setUserId). Never mix them.Not ending manual spans in TypeScript
Not ending manual spans in TypeScript
span.end() was not called — typically because it wasn’t in a finally block.Fix: Always call span.end() in a finally block, or use Netra.startActiveSpan() for automatic lifecycle management.Integration Checklist
Use this checklist to verify your Netra integration is production-ready:SDK installed and up to date
pip install --upgrade netra-sdk or npm update netra-sdk to ensure you have the latest version.Environment variables configured
NETRA_API_KEY and NETRA_OTLP_ENDPOINT are set and point to the correct data region.Init called first
Netra.init() is the first SDK call in your entrypoint — before any provider imports or LLM usage.Context is set per request
userId, sessionId, and tenantId are set at the start of each request for proper grouping.Root input and output captured
set_root_input is called at the entry point, and set_root_output (or the streaming equivalent) is called before returning.Environment tag is set
environment parameter distinguishes development, staging, and production traces.Noisy instruments filtered
blocked_spans and block_instruments exclude health checks, internal HTTP, and other non-AI noise.Shutdown is called on exit
Netra.shutdown() is hooked into your application’s graceful shutdown lifecycle.