otes

package
v0.10.3 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package otes provides es client with opentracing. For documentation about es usage, see https://github.com/olivere/elastic

Integration

package otes exports the configuration factoryIn the following format:

	es:
	  default:
        "url": "http://localhost:9200"
		"index":       "",
		"username":    "",
		"password":    "",
		"shards":      0,
		"replicas":    0,
		"sniff":       false,
		"healthCheck": false,

Add the es dependency to core:

var c *core.C = core.New()
c.Provide(otes.Providers())

Then you can invoke etcd from the application.

c.Invoke(func(client *elastic.Client) {
	// Do something with es
})

Sometimes there are valid reasons to connect to more than one es server. Inject otes.Maker to factory a *elastic.Client with a specific configuration entry.

c.Invoke(function(maker otes.Maker) {
	client, err := maker.Make("default")
	// do something with client
})
Example
if os.Getenv("ELASTICSEARCH_ADDR") == "" {
	fmt.Println("set ELASTICSEARCH_ADDR to run example")
	return
}
c := core.New(core.WithInline("log.level", "none"))
c.ProvideEssentials()
c.Provide(otes.Providers())
c.Invoke(func(esClient *elastic.Client) {
	running := esClient.IsRunning()
	fmt.Println(running)
})
Output:

true
Example (Providers)
if os.Getenv("ELASTICSEARCH_ADDR") == "" {
	fmt.Println("set ELASTICSEARCH_ADDR to run example")
	return
}
c := core.New(
	core.WithInline("log.level", "none"),
	core.WithInline("es.default.url", strings.Split(os.Getenv("ELASTICSEARCH_ADDR"), ",")),
)
c.ProvideEssentials()
c.Provide(otes.Providers(otes.WithClientConstructor(func(args otes.ClientArgs) (*elastic.Client, error) {
	return elastic.NewClient(elastic.SetURL(args.Conf.URL...), elastic.SetGzip(true))
})))
c.Invoke(func(esClient *elastic.Client) {
	running := esClient.IsRunning()
	fmt.Println(running)
})
Output:

true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Providers

func Providers(opts ...ProvidersOptionFunc) di.Deps

Providers returns a set of dependency providers. It includes the Maker, the default *elastic.Client and exported configs.

Depends On:
	log.Logger
	contract.ConfigAccessor
	opentracing.Tracer     `optional:"true"`
	contract.Dispatcher    `optional:"true"`
	contract.DIPopulator
Provides:
	Factory
	Maker
	*elastic.Client

Types

type ClientArgs added in v0.9.1

type ClientArgs struct {
	Name      string
	Conf      *Config
	Populator contract.DIPopulator
}

ClientArgs are arguments for constructing elasticsearch clients. Use this as input when providing custom constructor.

type Config added in v0.4.1

type Config struct {
	URL         []string `json:"url" yaml:"url"`
	Index       string   `json:"index" yaml:"index"`
	Username    string   `json:"username" yaml:"username"`
	Password    string   `json:"password" yaml:"password"`
	Shards      int      `json:"shards" yaml:"shards"`
	Replicas    int      `json:"replicas" yaml:"replicas"`
	Sniff       *bool    `json:"sniff" yaml:"sniff"`
	Healthcheck *bool    `json:"healthcheck" yaml:"healthcheck"`
}

Config is the type of elasticsearch configurations.

type ElasticLogAdapter added in v0.4.2

type ElasticLogAdapter struct {
	Logging log.Logger
}

ElasticLogAdapter is an adapter between kitlog and elastic logger interface

func (ElasticLogAdapter) Printf added in v0.4.2

func (l ElasticLogAdapter) Printf(msg string, v ...interface{})

Printf implements elastic.Logger

type EsConfigInterceptor

type EsConfigInterceptor func(name string, opt *Config)

EsConfigInterceptor is an injector type hint that allows user to do last minute modification to es configurations.

type Factory

type Factory struct {
	*di.Factory
}

Factory is a *di.Factory that creates *elastic.Client using a specific configuration entry.

func (Factory) Make

func (r Factory) Make(name string) (*elastic.Client, error)

Make creates *elastic.Client using a specific configuration entry.

type Maker

type Maker interface {
	Make(name string) (*elastic.Client, error)
}

Maker models Factory

type ProvidersOptionFunc added in v0.9.0

type ProvidersOptionFunc func(options *providersOption)

ProvidersOptionFunc is the type of functional providersOption for Providers. Use this type to change how Providers work.

func WithClientConstructor added in v0.9.0

func WithClientConstructor(f func(ClientArgs) (*elastic.Client, error)) ProvidersOptionFunc

WithClientConstructor instructs the Providers to accept an alternative constructor for elasticsearch client. Refer to the package elastic for how to construct a custom elastic.Client.

func WithConfigInterceptor added in v0.9.0

func WithConfigInterceptor(interceptor EsConfigInterceptor) ProvidersOptionFunc

WithConfigInterceptor instructs the Providers to accept the EsConfigInterceptor so that users can change config during runtime. This can be useful when some dynamic computations on configs are required.

func WithReload added in v0.10.0

func WithReload(shouldReload bool) ProvidersOptionFunc

WithReload toggles whether to reload the factory after receiving the OnReload event. By default, the factory won't be reloaded.

type Transport

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

Transport for tracing Elastic operations.

func NewTransport

func NewTransport(opts ...TransportOption) *Transport

NewTransport specifies a transport that will trace Elastic and report back via OpenTracing.

func (*Transport) RoundTrip

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

RoundTrip captures the request and starts an OpenTracing span for Elastic PerformRequest operation.

type TransportOption added in v0.9.0

type TransportOption func(t *Transport)

TransportOption signature for specifying options, e.g. WithRoundTripper.

func WithRoundTripper

func WithRoundTripper(rt http.RoundTripper) TransportOption

WithRoundTripper specifies the http.RoundTripper to call next after this transport. If it is nil (default), the transport will use http.DefaultTransport.

func WithTracer

func WithTracer(tracer opentracing.Tracer) TransportOption

WithTracer specifies the opentracing.Tracer to call this transport.

Jump to

Keyboard shortcuts

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