splunkmysql

package module
v1.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2024 License: Apache-2.0 Imports: 4 Imported by: 1

README

Splunk Instrumentation for the MySQL Driver Package

Go Reference

This package instruments the github.com/go-sql-driver/mysql package using the splunksql package.

Getting Started

This package is design to be a drop-in replacement for the existing use of the mysql package. The blank identified imports of that package can be replaced with this package, and the standard library sql.Open function can be replaced with the equivalent Open from splunksql.

An example can be found here.

Documentation

Overview

Package splunkmysql provides instrumentation for the github.com/go-sql-driver/mysql package.

To use this package, replace any blank identified imports of the github.com/go-sql-driver/mysql package with an import of this package and use the splunksql.Open function as a replacement for any sql.Open function use. For example, if your code looks like this to start.

import (
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
)
// ...
db, err := sql.Open("mysql", "user:password@/dbname")
// ...

Update to this.

import (
	_ "github.com/signalfx/splunk-otel-go/instrumentation/github.com/go-sql-driver/mysql/splunkmysql"
	"github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql"
)
// ...
db, err := splunksql.Open("mysql", "user:password@/dbname")
// ...
Example
package main

import (
	"database/sql"
	"fmt"
	"net/http"
	"strconv"
	"strings"
	"time"

	"github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql"
	_ "github.com/signalfx/splunk-otel-go/instrumentation/github.com/go-sql-driver/mysql/splunkmysql"
)

type server struct {
	DB *sql.DB
}

func (s *server) listenAndServe() error {
	// Requests to /square/n will return the square of n.
	http.HandleFunc("/square/", s.handle)
	return http.ListenAndServe(":80", nil)
}

func (s *server) handle(w http.ResponseWriter, req *http.Request) {
	idx := strings.LastIndex(req.URL.Path, "/")
	n, err := strconv.Atoi(req.URL.Path[idx+1:])
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	query := "SELECT squareNumber FROM squarenum WHERE number = ?"
	var nSquared int
	// Propagate the context to ensure created spans are included in any
	// existing trace.
	if err := s.DB.QueryRowContext(req.Context(), query, n).Scan(&nSquared); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	fmt.Fprintf(w, "%d", nSquared)
}

func main() {
	// Create a traced connection to the MySQL database.
	db, err := splunksql.Open("mysql", "user:password@/dbname")
	if err != nil {
		panic(err)
	}
	defer db.Close()
	// Set recommended default DB configuration.
	db.SetConnMaxLifetime(time.Minute * 3)
	db.SetMaxOpenConns(10)
	db.SetMaxIdleConns(10)

	// Validate DSN data by opening a connection. There is no parent context
	// to pass here so the span created from this operation will be in its own
	// trace.
	if err := db.Ping(); err != nil {
		panic(err)
	}

	srv := &server{DB: db}
	if err := srv.listenAndServe(); err != nil {
		panic(err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DSNParser

func DSNParser(dataSourceName string) (splunksql.ConnectionConfig, error)

DSNParser parses the data source connection name for a connection to a MySQL database using the github.com/go-sql-driver/mysql client package.

Types

This section is empty.

Directories

Path Synopsis
test module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL