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 ¶
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 ¶
var ( IdRegexp = regexp.MustCompile("/([0-9]+)([/\\?]|$)") IdPlaceholder = []byte("/?$2") IndexRegexp = regexp.MustCompile("[0-9]{2,}") IndexPlaceholder = []byte("?") )
Functions ¶
func NewTracedHTTPClient ¶
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.
Types ¶
type TracedTransport ¶
TracedTransport is a traced HTTP transport that captures Elasticsearch spans.