elastictrace

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2017 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package elastictrace provides tracing for the Elastic Elasticsearch client. Supports v3 (gopkg.in/olivere/elastic.v3), v5 (gopkg.in/olivere/elastic.v5) but with v3 you must use `DoC` on all requests to capture the request context.

Example (V3)

To trace elastic.v3 you create a TracedHTTPClient in the same way but all requests must use the DoC() call to pass the request context.

package main

import (
	"context"
	elastictrace "github.com/DataDog/dd-trace-go/contrib/olivere/elastic"
	"github.com/DataDog/dd-trace-go/tracer"

	elasticv3 "gopkg.in/olivere/elastic.v3"
)

func main() {
	tc := elastictrace.NewTracedHTTPClient("my-elasticsearch-service", tracer.DefaultTracer)
	client, _ := elasticv3.NewClient(
		elasticv3.SetURL("http://127.0.0.1:9200"),
		elasticv3.SetHttpClient(tc),
	)

	// Spans are emitted for all
	client.Index().
		Index("twitter").Type("tweet").Index("1").
		BodyString(`{"user": "test", "message": "hello"}`).
		DoC(context.Background())

	// Use a context to pass information down the call chain
	root := tracer.NewRootSpan("parent.request", "web", "/tweet/1")
	ctx := root.Context(context.Background())
	client.Get().
		Index("twitter").Type("tweet").Index("1").
		DoC(ctx)
	root.Finish()
}
Output:

Example (V5)

To start tracing elastic.v5 requests, create a new TracedHTTPClient that you will use when initializing the elastic.Client.

package main

import (
	"context"
	elastictrace "github.com/DataDog/dd-trace-go/contrib/olivere/elastic"
	"github.com/DataDog/dd-trace-go/tracer"

	elasticv5 "gopkg.in/olivere/elastic.v5"
)

func main() {
	tc := elastictrace.NewTracedHTTPClient("my-elasticsearch-service", tracer.DefaultTracer)
	client, _ := elasticv5.NewClient(
		elasticv5.SetURL("http://127.0.0.1:9200"),
		elasticv5.SetHttpClient(tc),
	)

	// Spans are emitted for all
	client.Index().
		Index("twitter").Type("tweet").Index("1").
		BodyString(`{"user": "test", "message": "hello"}`).
		Do(context.Background())

	// Use a context to pass information down the call chain
	root := tracer.NewRootSpan("parent.request", "web", "/tweet/1")
	ctx := root.Context(context.Background())
	client.Get().
		Index("twitter").Type("tweet").Index("1").
		Do(ctx)
	root.Finish()
}
Output:

Index

Examples

Constants

View Source
const MaxContentLength = 500 * 1024

MaxContentLength is the maximum content length for which we'll read and capture the contents of the request body. Anything larger will still be traced but the body will not be captured as trace metadata.

Variables

View Source
var (
	IdRegexp         = regexp.MustCompile("/([0-9]+)([/\\?]|$)")
	IdPlaceholder    = []byte("/?$2")
	IndexRegexp      = regexp.MustCompile("[0-9]{2,}")
	IndexPlaceholder = []byte("?")
)

Functions

func NewTracedHTTPClient

func NewTracedHTTPClient(service string, tracer *tracer.Tracer) *http.Client

NewTracedHTTPClient returns a new TracedTransport that traces HTTP requests.

func NewTracedHTTPClientWithTransport

func NewTracedHTTPClientWithTransport(service string, tracer *tracer.Tracer, transport *http.Transport) *http.Client

NewTracedHTTPClientWithTransport returns a new TracedTransport that traces HTTP requests and takes in a Transport to use something other than the default.

func Quantize

func Quantize(span *tracer.Span)

Quantize quantizes an Elasticsearch to extract a meaningful resource from the request. We quantize based on the method+url with some cleanup applied to the URL. URLs with an ID will be generalized as will (potential) timestamped indices.

Types

type TracedTransport

type TracedTransport struct {
	*http.Transport
	// contains filtered or unexported fields
}

TracedTransport is a traced HTTP transport that captures Elasticsearch spans.

func (*TracedTransport) RoundTrip

func (t *TracedTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip satisfies the RoundTripper interface, wraps the sub Transport and captures a span of the Elasticsearch request.

Jump to

Keyboard shortcuts

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