An open API service indexing awesome lists of open source software.

https://github.com/go-coldbrew/tracing

Distributed tracing abstraction — OpenTelemetry, OpenTracing, Jaeger, Zipkin, and New Relic
https://github.com/go-coldbrew/tracing

coldbrew distributed-tracing go golang jaeger jaegertracing opentelemetry opentracing zipkin

Last synced: 2 months ago
JSON representation

Distributed tracing abstraction — OpenTelemetry, OpenTracing, Jaeger, Zipkin, and New Relic

Awesome Lists containing this project

README

          

[![CI](https://github.com/go-coldbrew/tracing/actions/workflows/go.yml/badge.svg)](https://github.com/go-coldbrew/tracing/actions/workflows/go.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-coldbrew/tracing)](https://goreportcard.com/report/github.com/go-coldbrew/tracing)
[![GoDoc](https://pkg.go.dev/badge/github.com/go-coldbrew/tracing.svg)](https://pkg.go.dev/github.com/go-coldbrew/tracing)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

# tracing

```go
import "github.com/go-coldbrew/tracing"
```

Package tracing provides distributed tracing for Go applications. It offers features such as collecting performance data, identifying where requests spend most of their time, and segmenting requests.

Traces are created and exported via OpenTelemetry. The core package configures the OTEL tracer provider at startup, sending traces to any OTLP\-compatible backend \(Jaeger, Grafana Tempo, Honeycomb, etc.\) or New Relic.

## Index

- [Constants](<#constants>)
- [func ClientSpan\(operationName string, ctx context.Context\) \(context.Context, oteltrace.Span\)](<#ClientSpan>)
- [func CloneContextValues\(parent context.Context\) context.Context](<#CloneContextValues>)
- [func GRPCTracingSpan\(operationName string, ctx context.Context\) context.Context](<#GRPCTracingSpan>)
- [func MergeContextValues\(parent context.Context, main context.Context\) context.Context](<#MergeContextValues>)
- [func MergeParentContext\(parent context.Context, main context.Context\) context.Context](<#MergeParentContext>)
- [func NewContextWithParentValues\(parent context.Context\) context.Context](<#NewContextWithParentValues>)
- [type Span](<#Span>)
- [func NewDatastoreSpan\(ctx context.Context, datastore, operation, collection string\) \(Span, context.Context\)](<#NewDatastoreSpan>)
- [func NewExternalSpan\(ctx context.Context, name string, url string\) \(Span, context.Context\)](<#NewExternalSpan>)
- [func NewHTTPExternalSpan\(ctx context.Context, name string, url string, hdr http.Header\) \(Span, context.Context\)](<#NewHTTPExternalSpan>)
- [func NewInternalSpan\(ctx context.Context, name string\) \(Span, context.Context\)](<#NewInternalSpan>)

## Constants

SupportPackageIsVersion1 is a compile\-time assertion constant. Downstream packages reference this to enforce version compatibility.

```go
const SupportPackageIsVersion1 = true
```


## func [ClientSpan]()

```go
func ClientSpan(operationName string, ctx context.Context) (context.Context, oteltrace.Span)
```

ClientSpan starts a new client span linked to the existing spans if any are found in the context. The returned context should be used in place of the original.


## func [CloneContextValues]()

```go
func CloneContextValues(parent context.Context) context.Context
```

Deprecated: Use [NewContextWithParentValues](<#NewContextWithParentValues>) instead.


## func [GRPCTracingSpan]()

```go
func GRPCTracingSpan(operationName string, ctx context.Context) context.Context
```

GRPCTracingSpan starts a new server span from incoming gRPC metadata. The returned context should be used in place of the original.


## func [MergeContextValues]()

```go
func MergeContextValues(parent context.Context, main context.Context) context.Context
```

MergeContextValues merged the given main context with a parent context, Cancel/Deadline etc are used from the main context and values are looked in both the contexts can be use to merge a parent context with a new context, the new context will have the values from both the contexts


## func [MergeParentContext]()

```go
func MergeParentContext(parent context.Context, main context.Context) context.Context
```

Deprecated: Use [MergeContextValues](<#MergeContextValues>) instead.


## func [NewContextWithParentValues]()

```go
func NewContextWithParentValues(parent context.Context) context.Context
```

NewContextWithParentValues clones a given context values and returns a new context obj which is not affected by Cancel, Deadline etc can be used to pass context values to a new context which is not affected by the parent context cancel/deadline etc from parent


## type [Span]()

Span defines an interface for implementing a tracing span. Consumers use this to create and annotate spans without coupling to a specific tracing backend.

```go
type Span interface {
// End ends the span, can also use Finish()
End()
// Finish ends the span, can also use End()
Finish()
// SetTag sets a tag on the span, can be used to add custom attributes
SetTag(key string, value any)
// SetQuery sets the query on the span, can be used to add query for datastore spans
SetQuery(query string)
// SetError sets the error on the span
SetError(err error) error
}
```


### func [NewDatastoreSpan]()

```go
func NewDatastoreSpan(ctx context.Context, datastore, operation, collection string) (Span, context.Context)
```

NewDatastoreSpan starts a span for tracing data store actions. This is used to trace actions against a data store, for example, a database query or a redis call.

Example

```go
package main

import (
"context"

"github.com/go-coldbrew/tracing"
)

func main() {
ctx := context.Background()

// Create a span for a database operation
span, ctx := tracing.NewDatastoreSpan(ctx, "postgres", "SELECT", "users")
defer span.End()

// Run your query — the span records datastore, operation, and collection
_ = ctx
}
```


### func [NewExternalSpan]()

```go
func NewExternalSpan(ctx context.Context, name string, url string) (Span, context.Context)
```

NewExternalSpan starts a span for tracing external actions. This is used to trace actions against an external service.

Example

```go
package main

import (
"context"

"github.com/go-coldbrew/tracing"
)

func main() {
ctx := context.Background()

// Create a span for an external service call
span, ctx := tracing.NewExternalSpan(ctx, "payment-gateway", "/charge")
defer span.End()

// Make the external call — the span tracks the dependency
_ = ctx
}
```


### func [NewHTTPExternalSpan]()

```go
func NewHTTPExternalSpan(ctx context.Context, name string, url string, hdr http.Header) (Span, context.Context)
```

NewHTTPExternalSpan starts a span for tracing external HTTP actions. It also injects trace propagation headers so the external service can correlate the call back to this service.


### func [NewInternalSpan]()

```go
func NewInternalSpan(ctx context.Context, name string) (Span, context.Context)
```

NewInternalSpan starts a span for tracing internal actions. This is used to trace actions within the same service, for example, a function call.

Example

```go
package main

import (
"context"

"github.com/go-coldbrew/tracing"
)

func main() {
ctx := context.Background()

// Create a span for internal business logic
span, ctx := tracing.NewInternalSpan(ctx, "processOrder")
defer span.End()

// Your logic here — the span tracks duration and errors
_ = ctx
}
```

Generated by [gomarkdoc]()