Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,30 @@ type TypedClient struct {
*typedapi.API
}

// NewBaseClient creates a new client free of any API.
func NewBaseClient(cfg Config) (*BaseClient, error) {
tp, err := newTransport(cfg)
if err != nil {
return nil, err
}

compatHeaderEnv := os.Getenv(esCompatHeader)
compatibilityHeader, _ := strconv.ParseBool(compatHeaderEnv)

client := &BaseClient{
Transport: tp,
disableMetaHeader: cfg.DisableMetaHeader,
metaHeader: initMetaHeader(tp),
compatibilityHeader: cfg.EnableCompatibilityMode || compatibilityHeader,
}

if cfg.DiscoverNodesOnStart {
go client.DiscoverNodes()
}

return client, nil
}

// NewDefaultClient creates a new client with default options.
//
// It will use http://localhost:9200 as the default address.
Expand Down
30 changes: 16 additions & 14 deletions internal/build/cmd/generate/commands/gensource/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func new` + g.Endpoint.MethodWithNamespace() + `Func(t Transport) ` + g.Endpoint
}

if transport, ok := t.(Instrumented); ok {
r.instrument = transport.InstrumentationEnabled()
r.Instrument = transport.InstrumentationEnabled()
}

return r.Do(r.ctx, t)
Expand Down Expand Up @@ -287,7 +287,7 @@ type ` + g.Endpoint.MethodWithNamespace() + `Request struct {`)
g.w("\n\n\tHeader\thttp.Header\n")

g.w("\n\tctx context.Context\n")
g.w("\n\tinstrument Instrumentation\n")
g.w("\n\tInstrument Instrumentation\n")
g.w("}\n")
}

Expand Down Expand Up @@ -341,6 +341,8 @@ func (f ` + g.Endpoint.MethodWithNamespace() + `) WithContext(v context.Context)
pDesc = strings.Replace(pDesc, "use `_all` or empty string", "use _all", -1)
}

pDesc = strings.Replace(pDesc, "\n", "", -1)

// Generate annotation
b.WriteString("\n// With" + pFieldName)
if typ == "*gensource.Part" {
Expand Down Expand Up @@ -509,7 +511,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(providedCtx context.
ctx context.Context
)` + "\n\n")

g.w(`if instrument, ok := r.instrument.(Instrumentation); ok {
g.w(`if instrument, ok := r.Instrument.(Instrumentation); ok {
ctx = instrument.Start(providedCtx, "` + g.Endpoint.Name + `")
defer instrument.Close(ctx)
}
Expand Down Expand Up @@ -655,27 +657,27 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(providedCtx context.
requiredArgsValidation.WriteString(`if r.` + p + ` == nil { return nil, errors.New("` + a.Name + ` is required and cannot be nil") }` + "\n")
pathGrow.WriteString(`len(strconv.Itoa(*r.` + p + `)) + `)
pathContent.WriteString(` path.WriteString(strconv.Itoa(*r.` + p + `))` + "\n")
pathContent.WriteString(`if instrument, ok := r.instrument.(Instrumentation); ok {
pathContent.WriteString(`if instrument, ok := r.Instrument.(Instrumentation); ok {
instrument.RecordPathPart(ctx, "` + a.Name + `", strconv.Itoa(*r.` + p + `))
}` + "\n")
case "string":
pathGrow.WriteString(`len(r.` + p + `) + `)
pathContent.WriteString(` path.WriteString(r.` + p + `)` + "\n")
pathContent.WriteString(`if instrument, ok := r.instrument.(Instrumentation); ok {
pathContent.WriteString(`if instrument, ok := r.Instrument.(Instrumentation); ok {
instrument.RecordPathPart(ctx, "` + a.Name + `", r.` + p + `)
}` + "\n")
case "list":
requiredArgsValidation.WriteString(`if len(r.` + p + `) == 0 { return nil, errors.New("` + a.Name + ` is required and cannot be nil or empty") }` + "\n")
pathGrow.WriteString(`len(strings.Join(r.` + p + `, ",")) + `)
pathContent.WriteString(` path.WriteString(strings.Join(r.` + p + `, ","))` + "\n")
pathContent.WriteString(`if instrument, ok := r.instrument.(Instrumentation); ok {
pathContent.WriteString(`if instrument, ok := r.Instrument.(Instrumentation); ok {
instrument.RecordPathPart(ctx, "` + a.Name + `", strings.Join(r.` + p + `, ","))
}` + "\n")
case "long":
requiredArgsValidation.WriteString(`if r.` + p + ` == nil { return nil, errors.New("` + a.Name + ` is required and cannot be nil") }` + "\n")
pathGrow.WriteString(`len(strconv.Itoa(*r.` + p + `)) + `)
pathContent.WriteString(` path.WriteString(strconv.Itoa(*r.` + p + `))` + "\n")
pathContent.WriteString(`if instrument, ok := r.instrument.(Instrumentation); ok {
pathContent.WriteString(`if instrument, ok := r.Instrument.(Instrumentation); ok {
instrument.RecordPathPart(ctx, "` + a.Name + `", strconv.Itoa(*r.` + p + `))
}` + "\n")
default:
Expand All @@ -698,7 +700,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(providedCtx context.
pathContent.WriteString(` if r.` + p + ` != "" {` + "\n")
pathContent.WriteString(` path.WriteString("/")` + "\n")
pathContent.WriteString(` path.WriteString(r.` + p + `)` + "\n")
pathContent.WriteString(`if instrument, ok := r.instrument.(Instrumentation); ok {
pathContent.WriteString(`if instrument, ok := r.Instrument.(Instrumentation); ok {
instrument.RecordPathPart(ctx, "` + a.Name + `", r.` + p + `)
}` + "\n")
pathContent.WriteString(` }` + "\n")
Expand All @@ -707,7 +709,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(providedCtx context.
pathContent.WriteString(` if len(r.` + p + `) > 0 {` + "\n")
pathContent.WriteString(` path.WriteString("/")` + "\n")
pathContent.WriteString(` path.WriteString(strings.Join(r.` + p + `, ","))` + "\n")
pathContent.WriteString(`if instrument, ok := r.instrument.(Instrumentation); ok {
pathContent.WriteString(`if instrument, ok := r.Instrument.(Instrumentation); ok {
instrument.RecordPathPart(ctx, "` + a.Name + `", strings.Join(r.` + p + `, ","))
}` + "\n")
pathContent.WriteString(` }` + "\n")
Expand All @@ -717,7 +719,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(providedCtx context.
pathContent.WriteString(` path.Grow(1 + len(value))` + "\n")
pathContent.WriteString(` path.WriteString("/")` + "\n")
pathContent.WriteString(` path.WriteString(value)` + "\n")
pathContent.WriteString(`if instrument, ok := r.instrument.(Instrumentation); ok {
pathContent.WriteString(`if instrument, ok := r.Instrument.(Instrumentation); ok {
instrument.RecordPathPart(ctx, "` + a.Name + `", value)
}` + "\n")
pathContent.WriteString(` }` + "\n")
Expand Down Expand Up @@ -857,7 +859,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(providedCtx context.
g.w(`req, err := newRequest(method, path.String(), ` + httpBody + `)` + "\n")

g.w(`if err != nil {
if instrument, ok := r.instrument.(Instrumentation); ok {
if instrument, ok := r.Instrument.(Instrumentation); ok {
instrument.RecordError(ctx, err)
}
return nil, err
Expand Down Expand Up @@ -894,7 +896,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(providedCtx context.
}` + "\n\n")

g.w(`
if instrument, ok := r.instrument.(Instrumentation); ok {
if instrument, ok := r.Instrument.(Instrumentation); ok {
instrument.BeforeRequest(req, "` + g.Endpoint.Name + `")
`)
if g.Endpoint.Body != nil {
Expand All @@ -906,11 +908,11 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(providedCtx context.
g.w(`}`)
g.w(`
res, err := transport.Perform(req)
if instrument, ok := r.instrument.(Instrumentation); ok {
if instrument, ok := r.Instrument.(Instrumentation); ok {
instrument.AfterRequest(req, "elasticsearch", "` + g.Endpoint.Name + `")
}
if err != nil {
if instrument, ok := r.instrument.(Instrumentation); ok {
if instrument, ok := r.Instrument.(Instrumentation); ok {
instrument.RecordError(ctx, err)
}
return nil, err
Expand Down
Loading