elastic

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2024 License: Apache-2.0, BSD-3-Clause, Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package elastic provides functions to trace the github.com/elastic/go-elasticsearch packages.

Example (V8)
package main

import (
	"context"
	"log"
	"strings"

	elastictrace "github.com/0angelic0/dd-trace-go/contrib/elastic/go-elasticsearch.v6"
	"github.com/0angelic0/dd-trace-go/ddtrace/tracer"

	elasticsearch "github.com/elastic/go-elasticsearch/v8"
	"github.com/elastic/go-elasticsearch/v8/esapi"
)

func main() {
	cfg := elasticsearch.Config{
		Transport: elastictrace.NewRoundTripper(elastictrace.WithServiceName("my-es-service")),
		Addresses: []string{
			"http://127.0.0.1:9200",
		},
	}
	es, err := elasticsearch.NewClient(cfg)
	if err != nil {
		log.Fatalf("Error creating the client: %s", err)
	}

	_, err = esapi.IndexRequest{
		Index:      "twitter",
		DocumentID: "1",
		Body:       strings.NewReader(`{"user": "test", "message": "hello"}`),
	}.Do(context.Background(), es)

	if err != nil {
		log.Fatalf("Error creating index: %s", err)
	}

	// 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"),
	)

	_, err = esapi.GetRequest{
		Index:      "twitter",
		DocumentID: "1",
	}.Do(ctx, es)

	if err != nil {
		log.Fatalf("Error getting index: %s", err)
	}

	root.Finish()

}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRoundTripper

func NewRoundTripper(opts ...ClientOption) http.RoundTripper

NewRoundTripper 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

func WithAnalytics(on bool) ClientOption

WithAnalytics enables Trace Analytics for all started spans.

func WithAnalyticsRate

func WithAnalyticsRate(rate float64) ClientOption

WithAnalyticsRate sets the sampling rate for Trace Analytics events correlated to started spans.

func WithResourceNamer

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.RoundTripper) ClientOption

WithTransport sets the given transport as an http.Transport for the client.

Jump to

Keyboard shortcuts

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