Enhancing Vector Database Observability: Langtrace x Milvus Integration

Ali Waleed

Software Engineer

Nov 19, 2024

Introduction

In the era of AI-driven applications, vector databases like Milvus are powering next-generation search engines, recommendation systems, and data retrieval solutions. But with great power comes great complexity. As these systems handle high-dimensional vectors and large-scale queries, observability becomes a critical need—not just for troubleshooting but for optimizing performance and ensuring seamless user experiences.

This is where Langtrace steps in, offering deep observability for Milvus’s workflows. From collection creation to vector searches, Langtrace provides the insights developers need to monitor, debug, and optimize every step of their pipeline. In this guide, we’ll explore how Langtrace integrates with Milvus, showcasing its capabilities through a step-by-step walkthrough of essential Milvus functions.

By the end, you’ll see how Langtrace x Milvus transforms your observability game, giving you full visibility into your vector database operations. Let’s dive in!

Why Observability in Vector Databases is Important

As machine learning and AI systems continue to evolve, vector databases like Milvus have become indispensable for managing high-dimensional data. Whether you’re building image recognition systems, recommendation engines, or semantic search tools, vector DBs provide the backbone for storing and retrieving embeddings efficiently.

But with this power comes complexity. Vector searches involve intricate computations like similarity scoring, approximate nearest neighbor (ANN) algorithms, and indexing optimizations. Without proper observability, issues such as slow query performance, indexing errors, or misaligned embeddings can be hard to diagnose and fix.

This is why observability in vector databases is not just a luxury—it’s a necessity:

Debugging Made Easy: Trace errors back to their root cause, whether they stem from embedding quality or database configuration.

Performance Optimization: Monitor query latencies, index build times, and other metrics to fine-tune performance.

Workflow Insights: Gain a comprehensive view of how your data flows through the pipeline—from collection creation to vector retrieval.

Langtrace bridges this gap by bringing OpenTelemetry-powered observability to Milvus. It tracks each stage of your database workflow, providing actionable insights through spans, metrics, and events. Let’s explore this integration step by step, starting with the basics of collection creation.

Step-by-Step Guide: Langtrace x Milvus Integration in Action

In this section, we’ll walk through the key functions of Milvus while showcasing how Langtrace enhances observability at each step. Along the way, we’ll include screenshots of the Langtrace dashboard and relevant code snippets to bring the integration to life.

  1. Create a virtual enviroment

    python -m venv .venv & source
  2. Install Pymilvus & Langtrace

    pip install pymilvus "pymilvus[model]"
  3. Initialize Pymilvus client & Langtrace

    from langtrace_python_sdk import langtrace
    from pymilvus import MilvusClient, model
    langtrace.init(api_key=<your_api_key>)
    client = MilvusClient("milvus_demo.db")
  4. Create Collection

    COLLECTION_NAME = "demo_collection"
    def create_collection(collection_name: str = COLLECTION_NAME):
      if client.has_collection(collection_name=collection_name):
          client.drop_collection(collection_name=collection_name)
    
      client.create_collection(
          collection_name=collection_name,
        # The vectors we will use in this demo has 768 dimensions
          dimension=768,  
      )
  1. Create an embedding helper

    embedding_fn = model.DefaultEmbeddingFunction()
    
    def create_embedding(docs: List[str] = [], subject: str = "history"):
      """
      Create embeddings for the given documents.
      """
    
      vectors = embedding_fn.encode_documents(docs)
      # Each entity has id, vector representation, 
      # raw text, and a subject label that we use
      # to demo metadata filtering later.
      data = [
          {
            "id": i, 
            "vector": vectors[i], 
            "text": docs[i], 
            "subject": subject
          }
          for i in range(len(vectors))
      ]
      return data
  2. Insert Data

    def insert_data(collection_name: str = COLLECTION_NAME, data: List[dict] = []):
      client.insert(
          collection_name=collection_name,
          data=data,
      )
    
    # insert Alan Turing's history
    turing_data = create_embedding(
      docs=[
          "Artificial intelligence was founded as an academic discipline in 1956.",
          "Alan Turing was the first person to conduct substantial research in AI.",
          "Born in Maida Vale, London, Turing was raised in southern England.",
      ]
    )
    insert_data(data=turing_data)
  1. Query data

    def query(collection_name: str = COLLECTION_NAME, query: str = ""):
      res = client.query(
          collection_name=collection_name,
          filter=query,
          output_fields=["text", "subject"],
      )
    
      print(res)
    query(query="subject == 'history'")
  1. Vector Search

    def vector_search(collection_name: str = COLLECTION_NAME, queries: List[str] = []):
      query_vectors = embedding_fn.encode_queries(queries)
    
      res = client.search(
          # target collection
          collection_name="demo_collection",  
          data=query_vectors,
          # number of returned entities
          limit=2,  
          # specifies fields to be returned
          output_fields=["text", "subject"],  
          timeout=10,
          partition_names=["history"],
          anns_field="vector",
          search_params={"nprobe": 10},
      )
    vector_search(queries=["Who is Alan Turing?"])
    
    
  1. Now let's observe the full trace for the whole pipeline

    @with_langtrace_root_span("milvus_example")
    def main():
        create_collection()
        # insert Alan Turing's history
        turing_data = create_embedding(
            docs=[
                "Artificial intelligence was founded as an academic discipline in 1956.",
                "Alan Turing was the first person to conduct substantial research in AI.",
                "Born in Maida Vale, London, Turing was raised in southern England.",
            ]
        )
        insert_data(data=turing_data)
    
        # insert AI Drug Discovery
        drug_data = create_embedding(
            docs=[
                "Machine learning has been used for drug design.",
                "Computational synthesis with AI algorithms predicts molecular properties.",
                "DDR1 is involved in cancers and fibrosis.",
            ],
            subject="biology",
        )
        insert_data(data=drug_data)
    
        vector_search(queries=["Who is Alan Turing?"])
        query(query="subject == 'history'")
        query(query="subject == 'biology'")
    
    
    if __name__ == "__main__":
        main()
    
    

Conclusion

The integration of Langtrace with Milvus unlocks a powerful combination of vector database performance and deep observability. By tracing each operation—from collection creation to vector search—you gain unparalleled visibility into your data workflows. With this setup, you can debug faster, optimize query performance, and ensure your applications run seamlessly.

Whether you’re building AI-powered search engines, recommendation systems, or knowledge graphs, the ability to monitor and optimize your database workflows is no longer optional—it’s essential. Langtrace not only bridges this gap but also empowers you to focus on innovation while it handles the complexities of observability.

Ready to elevate your Milvus workflows? Start exploring Langtrace x Milvus today, and take your vector database observability to the next level. 🚀

Useful Resources

Ready to try Langtrace?

Try out the Langtrace SDK with just 2 lines of code.

Ready to deploy?

Try out the Langtrace SDK with just 2 lines of code.

Want to learn more?

Check out our documentation to learn more about how langtrace works

Join the Community

Check out our Discord community to ask questions and meet customers