How to Use OpenTelemetry SQL Database Driver Wrapper for Go

Sep 3, 2021 | Programming

In this blog post, we’ll guide you through setting up the OpenTelemetry SQL Database Driver Wrapper for Go. This wrapper effortlessly integrates with your existing database code, providing the ability to instrument interactions with your database, complete with traces and metrics. Let’s roll up our sleeves and get started!

Table of Contents

Prerequisites

Before you begin, ensure you have the following prerequisites installed:

  • Go version 1.22 or higher.

Install

From version v0.5.0, the project has been rebranded to go.nhat.io/otelsql.

Usage

To use the otelsql wrapper with your application, you need to register it. Here’s how you do it:

package example

import (
    "database/sql"
    "go.nhat.io/otelsql"
)

func openDB(dsn string) (*sql.DB, error) {
    driverName, err := otelsql.Register("postgres", 
        otelsql.AllowRoot(),
        otelsql.TraceQueryWithoutArgs(),
        otelsql.WithDatabaseName("my_database"),
    )
    if err != nil {
        return nil, err
    }
    return sql.Open(driverName, dsn)
}

The presented code registers the otelsql wrapper using the specified PostgreSQL driver and opens a connection to the database. Think of this registration as a passport that grants your database interactions the ability to send traces and metrics. Just as a passport identifies who you are in a foreign land, your registration allows your interactions to be recognized and monitored.

Options

Driver Options

Here are the various options you can use when registering the driver:

  • WithMeterProvider(metric.MeterProvider): Specify a meter provider.
  • WithTracerProvider(trace.TracerProvider): Specify a tracer provider.
  • WithDefaultAttributes(...attribute.KeyValue): Add extra attributes for the recorded spans and metrics.
  • AllowRoot(): Create root spans in the absence of existing spans.

Extras

Span Name Formatter

This allows you to customize the format of your span names. By default, spans are named based on the SQL method, such as sql:exec. You can modify this to suit your needs using:

otelsql.WithSpanNameFormatter(func(_ context.Context, op string) string {
    return "main-db: " + op
})

Convert Error to Span Status

Errors can be converted into span statuses. If you want to ignore sql.ErrSkip, you can do so by:

otelsql.ConvertErrorToSpanStatus(func(err error) (codes.Code, string) {
    if err == nil {
        return codes.Ok, ""
    }
    return codes.Error, err.Error()
})

Metrics

To ensure your database is performing optimally, you can track various metrics such as:

  • Active and idle connections.
  • Latency of database calls.

An example of client metrics is:

db_sql_client_latency_bucket

Traces

Traces are systematically recorded and can help diagnose and monitor database operations at a granular level. Ensure to configure your application to utilize context methods where possible.

Migration from ocsql

Transitioning from ocsql is straightforward as the options are similar. You’ll find that the behavior remains consistent, easing the migration process.

Compatibility

The wrapper is compatible with various operating systems including Ubuntu, macOS, and Windows. See the GitHub repository for a detailed list of compatible versions.

Troubleshooting

If you encounter any issues, consider the following troubleshooting steps:

  • Ensure your database driver is correctly registered.
  • Double-check your DSN (Data Source Name).
  • Review your context management, especially if using AllowRoot().

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

With this comprehensive guide, you’re now equipped to enhance your Go applications with the capabilities offered by OpenTelemetry’s SQL database driver wrapper. Happy coding!

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox