trace

package module
v0.0.0-...-74b744a Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2019 License: MIT Imports: 8 Imported by: 0

README

opentracing-exts

GoDoc Go Report Card

This package provides opentracing span options and span logging utility types and functions.

go get -u github.com/code-willing/opentracing-exts
import (
    otexts "github.com/code-willing/opentracing-exts"
)

Logging

Easily log span errors and set the correct span tags and log fields.

func example() (err error) {
    span := opentracing.StartSpan("name")
    defer func() {
        if err != nil {
            otexts.LogError(span, err)
        }
        span.Finish()
    }()
}

Log an error with extra log fields.

func example(a, b int) (err error) {
    span := opentracing.StartSpan("name")
    defer func() {
        if err != nil {
            otexts.LogErrorWithFields(span, err, map[string]interface{}{
                "a": a,
                "b": b,
            })
        }
        span.Finish()
    }()
}

JSON marshal the log field values.


type jsonThing struct {
    a int `json:"a"`
    b int `json:"a"`
}

func example(t jsonThing) (err error) {
    span := opentracing.StartSpan("name")
    defer func() {
        if err != nil {
            otexts.LogErrorWithFields(span, err, otexts.LogFields{
                "my-thing": t,
            }.Encode())
        }
        span.Finish()
    }()
}

Span Options

Start spans with specific client/server tags set:

// Start a span with RPC client tags set.
span := opentracing.StartSpan("name", otexts.RPCTags{
    Kind:        ext.SpanKindRPCClientEnum,
    PeerAddr:    "http://service.name.io/",
    PeerService: "service",
})

// Start a span with RPC server tags set.
span := opentracing.StartSpan("name", otexts.RPCTags{
    Kind:        ext.SpanKindRPCClientEnum,
    PeerAddr:    "http://service.name.io/",
    PeerService: "service",
})

// Start a new span with SQL database client tags set.
span := opentracing.StartSpan("name", trace.DBTags{
    Type:      "sql",
    Instance:  "test",
    User:      "username",
    Statement: "SELECT * FROM test",
})

// Start a new span with NoSQL database client tags set.
span := opentracing.StartSpan("name", trace.DBTags{
	Type:      "redis",
	Instance:  "test",
	User:      "username",
	Statement: "SET mykey 'WuValue'",
})

// Start a new span with HTTP tags set.
span := opentracing.StartSpan("name", trace.HTTPTags{
	Method:     http.MethodGet,
	URL:        "http://example.com/test?foo=bar",
	StatusCode: http.StatusOK,
})

Set span tags for events:

func execDB(stmt database.Stmt) (err error) {
    span := opentracing.StartSpan("name")
    defer func() {
        if err != nil {
            otexts.SetDBTags(span, otexts.DBTags{
                Type:      "sql",
                Instance:  "test",
                User:      "username",
                Statement: stmt.String(),
            })
            otexts.LogErrorWithFields(span, err, map[string]interface{}{
                "a": a,
                "b": b,
            })
        }
        span.Finish()
    }()
}

Documentation

Overview

Package opentracing-exts provides opentracing span options and span logging utilities.

See https://github.com/opentracing/specification/blob/master/semantic_conventions.md.

Index

Examples

Constants

View Source
const (
	LogFieldErrorKind   = "error.kind"
	LogFieldErrorObject = "error.object"
	LogFieldEvent       = "event"
	LogFieldMessage     = "message"
	LogFieldStack       = "stack"

	LogEventError = "error"
)

Standard opentracing log field names. See https://github.com/opentracing/specification/blob/master/semantic_conventions.md#log-fields-table.

Variables

This section is empty.

Functions

func LogError

func LogError(span opentracing.Span, err error)

LogError logs an error for an opentracing span, setting the standard error tags and log fields.

func LogErrorWithFields

func LogErrorWithFields(span opentracing.Span, err error, fields map[string]interface{})

LogErrorWithFields logs an error with the specified extra lof fields for an opentracing span, setting the standard error tags and log fields. The log field names "event", "error.kind", and "message" are reserved and will be ignored if set in the specified fields.

func LogErrorf

func LogErrorf(span opentracing.Span, err error, format string, args ...interface{})

LogErrorf logs an error with the specified format for an opentracing span, setting the standard error tags and log fields.

func SetDBTags

func SetDBTags(span opentracing.Span, t DBTags)

SetDBTags sets the standard database tags on the specified span.

func SetHTTPTags

func SetHTTPTags(span opentracing.Span, t HTTPTags)

SetHTTPTags sets the standard HTTP tags on the specified span.

func SetRPCTags

func SetRPCTags(span opentracing.Span, t RPCTags)

SetRPCTags sets the standard RPC tags on the specified span.

Types

type DBTags

type DBTags struct {
	Type      string // The database type.
	Instance  string // The database instance name.
	User      string // The username of the database accessor.
	Statement string // The database statement used.

	// Optional tags that describe the database peer.
	PeerAddr     string // The remote address.
	PeerHostname string // The remote hostname.
	PeerIPv4     net.IP // The remote IPv4 address.
	PeerIPv6     net.IP // The remote IPv6 address.
	PeerPort     uint16 // The remote port.
	PeerService  string // The remote service name.
}

DBTags is an opentracing.StartSpanOption that sets the standard database tags for a database client call.

See https://github.com/opentracing/specification/blob/master/semantic_conventions.md#database-client-calls.

Example (Nosql)
// Start a new span with database client tags set.
span := opentracing.StartSpan("name", otexts.DBTags{
	Type:      "redis",
	Instance:  "test",
	User:      "username",
	Statement: "SET mykey 'WuValue'",
})
defer span.Finish()
Output:

Example (Sql)
// Start a new span with database client tags set.
span := opentracing.StartSpan("name", otexts.DBTags{
	Type:      "sql",
	Instance:  "test",
	User:      "username",
	Statement: "SELECT * FROM test",
})
defer span.Finish()
Output:

func (DBTags) Apply

func (t DBTags) Apply(opts *opentracing.StartSpanOptions)

Apply implements the opentracing.StartSpanOption interface.

type HTTPTags

type HTTPTags struct {
	Method     string // The HTTP request method.
	URL        string // The HTTP request URL.
	StatusCode int    // The HTTP response status code.
}

HTTPTags is an opentracing.StartSpanOption that sets the standard HTTP tags.

See https://github.com/opentracing/specification/blob/master/semantic_conventions.md#span-tags-table.

Example
// Start a new span with HTTP tags set.
span := opentracing.StartSpan("name", otexts.HTTPTags{
	Method:     http.MethodGet,
	URL:        "http://example.com/test?foo=bar",
	StatusCode: http.StatusOK,
})
defer span.Finish()
Output:

func (HTTPTags) Apply

func (t HTTPTags) Apply(opts *opentracing.StartSpanOptions)

Apply implements the opentracing.StartSpanOption interface.

type LogFields

type LogFields map[string]interface{}

LogFields is a map of opentracing span log field names to values.

func (LogFields) Encode

func (f LogFields) Encode() map[string]interface{}

Encode returns a new map with the values for each key JSON marshaled. If the value for a key could not be marshaled, the original value is preserved.

type RPCTags

type RPCTags struct {
	Kind ext.SpanKindEnum // The span kind, "client" or "server".

	// Optional tags that describe the RPC peer.
	PeerAddr     string // The remote address.
	PeerHostname string // The remote hostname.
	PeerIPv4     net.IP // The remote IPv4 address.
	PeerIPv6     net.IP // The remote IPv6 address.
	PeerPort     uint16 // The remote port.
	PeerService  string // The remote service name.
}

RPCTags is an opentracing.StartSpanOption that sets the standard RPC tags.

See https://github.com/opentracing/specification/blob/master/semantic_conventions.md#rpcs.

Example (Client)
// Start a new span with RPC client tags set.
span := opentracing.StartSpan("name", otexts.RPCTags{
	Kind:        ext.SpanKindRPCClientEnum,
	PeerAddr:    "http://service.name.io/",
	PeerService: "service",
})
defer span.Finish()
Output:

Example (Server)
// Start a new span with RPC server tags set.
span := opentracing.StartSpan("name", otexts.RPCTags{
	Kind:        ext.SpanKindRPCServerEnum,
	PeerAddr:    "http://service.name.io/",
	PeerService: "service",
	PeerIPv4:    net.IPv4(127, 0, 0, 1),
	PeerPort:    8080,
})
defer span.Finish()
Output:

func (RPCTags) Apply

func (t RPCTags) Apply(opts *opentracing.StartSpanOptions)

Apply implements the opentracing.StartSpanOption interface.

Jump to

Keyboard shortcuts

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