goyavetrace

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2024 License: MIT Imports: 8 Imported by: 0

README

dd-trace-goyave

Datadog trace client library for the Goyave framework (Onfocus fork). This provides utilities to start a background tracer and automatically trace all HTTP requests received.

The tracer connects to a locally running datadog agent. This is required for this library to work. By default, the tracer starts with the following options:

  • Service name
  • Environment

The middleware provided with this library traces all received HTTP requests with the following tags:

  • Service name
  • Environment
  • Span type (web)
  • Span kind (server)
  • Request URL
  • Request HTTP method
  • Request route name
  • Response status code
  • Response error and stacktrace (if any)
  • Response user (if any, and if supported)

Usage

First, make sure the following configuration entries are set:

  • app.datadog.service: used to set the service name in the span generated for each request.
  • app.datadog.agentAddr: the datadog agent address to connect the tracer to. By default, the agent listens on 127.0.0.1:8126.

In the main function, start the tracer after loading the config:

import goyavetrace "github.com/onfocusio/dd-trace-goyave"

func main() {
    err := config.Load()
    if err != nil {
        //...
    }

    goyavetrace.Start()
    defer goyavetrace.Stop()
}

The Start() function accepts a variadic slice of tracer.StartOption if you need to add more options.

Be careful when using os.Exit() in the main(): deferred calls are not executed if the program exits this way. You risk not flushing the tracer doing so.

Finally, in the main route registrer, add the global middleware so it is applied to all requests, even if they don't match any route.

import goyavetrace "github.com/onfocusio/dd-trace-goyave"

func Register(router *goyave.Router) {

    // Add custom tags to the spans
    spanOption := func(s tracer.Span, _ *goyave.Response, _ *goyave.Request) {
        s.SetTag(ext.ManualKeep, true)
    }

    router.GlobalMiddleware((&goyavetrace.Middleware{
        SpanOption: spanOption, // Optional
    }).Handle)

    //...
}
Tracing users

If the request has an authenticated user, their ID, name and email address can be added to the trace. To make it possible, the user structure must implement the DatadogUserConverter interface:

type User struct {
	// ...
}

func (u User) ToDatadogUser() goyavetrace.DatadogUser {
	return goyavetrace.DatadogUser{
		ID:    u.ID,
		Name:  u.Name,
		Email: u.Email,
	}
}

If the user doesn't have a name or email (an API key for example), you can omit them and they won't be included in the tag.

Documentation

Index

Constants

View Source
const (
	TagUser = "user"
)

Span tag names

Variables

This section is empty.

Functions

func Start

func Start(opts ...tracer.StartOption)

Start starts the tracer with the given set of options. The "agent address", "service" and "env" options are provided by default based on the following config entries:

  • `app.datadog.agentAddr`
  • `app.datadog.service`
  • `app.environment`

It will stop and replace any running tracer, meaning that calling it several times will result in a restart of the tracer by replacing the current instance with a new one.

func Stop

func Stop()

Stop stops the started tracer. Subsequent calls are valid but become no-op.

Types

type DatadogUser

type DatadogUser struct {
	Name  string `json:",omitempty"`
	Email string `json:",omitempty"`
	ID    int64
}

DatadogUser minimal structure to identify a user at the origin of a trace.

func (DatadogUser) String

func (u DatadogUser) String() string

String returns a JSON representation of the structure.

type DatadogUserConverter

type DatadogUserConverter interface {
	ToDatadogUser() DatadogUser
}

DatadogUserConverter if be implemented by the user structure, a "user" tag will be added to the span with the returned `DatadogUser` structure as a value.

type Middleware

type Middleware struct {

	// SpanOption if not nil, this function is executed before finishing the span
	// associated with the request. This can be used to add custom tags to the span.
	SpanOption SpanOption
}

Middleware substitutes the original response writer with a trace writer wrapper.

func (Middleware) Handle

func (m Middleware) Handle(next goyave.Handler) goyave.Handler

Handle substitutes the original response writer with a trace writer wrapper.

type SpanOption added in v0.2.0

type SpanOption func(tracer.Span, *goyave.Response, *goyave.Request)

SpanOption function altering a span before finishing it. This can be used to add custom tags to the span.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer doesn't affect the response but captures information about the request and the response to convert it as tags for a datadog span. The span is finished and reported when `Close()` is called.

func NewWriter

func NewWriter(response *goyave.Response, request *goyave.Request, spanOption SpanOption) *Writer

NewWriter creates a new writer meant for use in a single response. Starts the span right away with the common options (service name, uri, method, route name, span kind and type).

The given `SpanOption` is executed before ending the span (at the end of the request life-cycle) and can be used to add tags to the span. Can be `nil`.

func (*Writer) Close

func (w *Writer) Close() error

Close the underlying writer, adds the status, error, and user tags to the span and finishes it.

func (*Writer) PreWrite

func (w *Writer) PreWrite(b []byte)

PreWrite calls PreWrite on the child writer if it implements PreWriter.

func (*Writer) Write

func (w *Writer) Write(b []byte) (int, error)

Jump to

Keyboard shortcuts

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