Documentation ¶
Overview ¶
Package elastic provides functions to trace the gopkg.in/olivere/elastic.v{3,5} packages.
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" elasticv3 "gopkg.in/olivere/elastic.v3" elastictrace "gopkg.in/CodapeWild/dd-trace-go.v1/contrib/olivere/elastic" "gopkg.in/CodapeWild/dd-trace-go.v1/ddtrace/tracer" ) func main() { tc := elastictrace.NewHTTPClient(elastictrace.WithServiceName("my-es-service")) 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, ctx := tracer.StartSpanFromContext(context.Background(), "parent.request", tracer.ServiceName("web"), tracer.ResourceName("/tweet/1"), ) 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" elasticv5 "gopkg.in/olivere/elastic.v5" elastictrace "gopkg.in/CodapeWild/dd-trace-go.v1/contrib/olivere/elastic" "gopkg.in/CodapeWild/dd-trace-go.v1/ddtrace/tracer" ) func main() { tc := elastictrace.NewHTTPClient(elastictrace.WithServiceName("my-es-service")) 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, ctx := tracer.StartSpanFromContext(context.Background(), "parent.request", tracer.ServiceName("web"), tracer.ResourceName("/tweet/1"), ) client.Get(). Index("twitter").Type("tweet").Index("1"). Do(ctx) root.Finish() }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewHTTPClient ¶
func NewHTTPClient(opts ...ClientOption) *http.Client
NewHTTPClient returns a new http.Client which traces requests under the given service name.
Types ¶
type ClientOption ¶
type ClientOption func(*clientConfig)
ClientOption represents an option that can be used when creating a client.
func WithAnalytics ¶ added in v1.11.0
func WithAnalytics(on bool) ClientOption
WithAnalytics enables Trace Analytics for all started spans.
func WithAnalyticsRate ¶ added in v1.11.0
func WithAnalyticsRate(rate float64) ClientOption
WithAnalyticsRate sets the sampling rate for Trace Analytics events correlated to started spans.
func WithResourceNamer ¶ added in v1.16.0
func WithResourceNamer(namer func(url, method string) string) ClientOption
WithResourceNamer specifies a quantizing function which will be used to obtain a resource name for a given ElasticSearch request, using the request's URL and method. Note that the default quantizer obfuscates IDs and indexes and by replacing it, sensitive data could possibly be exposed, unless the new quantizer specifically takes care of that.
func WithServiceName ¶
func WithServiceName(name string) ClientOption
WithServiceName sets the given service name for the client.
func WithTransport ¶
func WithTransport(t *http.Transport) ClientOption
WithTransport sets the given transport as an http.Transport for the client.