Skip to content

Commit 2c0e4da

Browse files
Feat: Add constructor for baseclient (#990) (#992)
* add constructor for BaseClient * expose instrument for esapi to allow injection via struct * use exposed instrument for esapi to allow injection via struct Co-authored-by: Laurent Saint-Félix <[email protected]>
1 parent a8d9967 commit 2c0e4da

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

elasticsearch.go

+24
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,30 @@ type TypedClient struct {
154154
*typedapi.API
155155
}
156156

157+
// NewBaseClient creates a new client free of any API.
158+
func NewBaseClient(cfg Config) (*BaseClient, error) {
159+
tp, err := newTransport(cfg)
160+
if err != nil {
161+
return nil, err
162+
}
163+
164+
compatHeaderEnv := os.Getenv(esCompatHeader)
165+
compatibilityHeader, _ := strconv.ParseBool(compatHeaderEnv)
166+
167+
client := &BaseClient{
168+
Transport: tp,
169+
disableMetaHeader: cfg.DisableMetaHeader,
170+
metaHeader: initMetaHeader(tp),
171+
compatibilityHeader: cfg.EnableCompatibilityMode || compatibilityHeader,
172+
}
173+
174+
if cfg.DiscoverNodesOnStart {
175+
go client.DiscoverNodes()
176+
}
177+
178+
return client, nil
179+
}
180+
157181
// NewDefaultClient creates a new client with default options.
158182
//
159183
// It will use http://localhost:9200 as the default address.

internal/build/cmd/generate/commands/gensource/generator.go

+16-14
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func new` + g.Endpoint.MethodWithNamespace() + `Func(t Transport) ` + g.Endpoint
137137
}
138138
139139
if transport, ok := t.(Instrumented); ok {
140-
r.instrument = transport.InstrumentationEnabled()
140+
r.Instrument = transport.InstrumentationEnabled()
141141
}
142142
143143
return r.Do(r.ctx, t)
@@ -287,7 +287,7 @@ type ` + g.Endpoint.MethodWithNamespace() + `Request struct {`)
287287
g.w("\n\n\tHeader\thttp.Header\n")
288288

289289
g.w("\n\tctx context.Context\n")
290-
g.w("\n\tinstrument Instrumentation\n")
290+
g.w("\n\tInstrument Instrumentation\n")
291291
g.w("}\n")
292292
}
293293

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

344+
pDesc = strings.Replace(pDesc, "\n", "", -1)
345+
344346
// Generate annotation
345347
b.WriteString("\n// With" + pFieldName)
346348
if typ == "*gensource.Part" {
@@ -509,7 +511,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(providedCtx context.
509511
ctx context.Context
510512
)` + "\n\n")
511513

512-
g.w(`if instrument, ok := r.instrument.(Instrumentation); ok {
514+
g.w(`if instrument, ok := r.Instrument.(Instrumentation); ok {
513515
ctx = instrument.Start(providedCtx, "` + g.Endpoint.Name + `")
514516
defer instrument.Close(ctx)
515517
}
@@ -655,27 +657,27 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(providedCtx context.
655657
requiredArgsValidation.WriteString(`if r.` + p + ` == nil { return nil, errors.New("` + a.Name + ` is required and cannot be nil") }` + "\n")
656658
pathGrow.WriteString(`len(strconv.Itoa(*r.` + p + `)) + `)
657659
pathContent.WriteString(` path.WriteString(strconv.Itoa(*r.` + p + `))` + "\n")
658-
pathContent.WriteString(`if instrument, ok := r.instrument.(Instrumentation); ok {
660+
pathContent.WriteString(`if instrument, ok := r.Instrument.(Instrumentation); ok {
659661
instrument.RecordPathPart(ctx, "` + a.Name + `", strconv.Itoa(*r.` + p + `))
660662
}` + "\n")
661663
case "string":
662664
pathGrow.WriteString(`len(r.` + p + `) + `)
663665
pathContent.WriteString(` path.WriteString(r.` + p + `)` + "\n")
664-
pathContent.WriteString(`if instrument, ok := r.instrument.(Instrumentation); ok {
666+
pathContent.WriteString(`if instrument, ok := r.Instrument.(Instrumentation); ok {
665667
instrument.RecordPathPart(ctx, "` + a.Name + `", r.` + p + `)
666668
}` + "\n")
667669
case "list":
668670
requiredArgsValidation.WriteString(`if len(r.` + p + `) == 0 { return nil, errors.New("` + a.Name + ` is required and cannot be nil or empty") }` + "\n")
669671
pathGrow.WriteString(`len(strings.Join(r.` + p + `, ",")) + `)
670672
pathContent.WriteString(` path.WriteString(strings.Join(r.` + p + `, ","))` + "\n")
671-
pathContent.WriteString(`if instrument, ok := r.instrument.(Instrumentation); ok {
673+
pathContent.WriteString(`if instrument, ok := r.Instrument.(Instrumentation); ok {
672674
instrument.RecordPathPart(ctx, "` + a.Name + `", strings.Join(r.` + p + `, ","))
673675
}` + "\n")
674676
case "long":
675677
requiredArgsValidation.WriteString(`if r.` + p + ` == nil { return nil, errors.New("` + a.Name + ` is required and cannot be nil") }` + "\n")
676678
pathGrow.WriteString(`len(strconv.Itoa(*r.` + p + `)) + `)
677679
pathContent.WriteString(` path.WriteString(strconv.Itoa(*r.` + p + `))` + "\n")
678-
pathContent.WriteString(`if instrument, ok := r.instrument.(Instrumentation); ok {
680+
pathContent.WriteString(`if instrument, ok := r.Instrument.(Instrumentation); ok {
679681
instrument.RecordPathPart(ctx, "` + a.Name + `", strconv.Itoa(*r.` + p + `))
680682
}` + "\n")
681683
default:
@@ -698,7 +700,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(providedCtx context.
698700
pathContent.WriteString(` if r.` + p + ` != "" {` + "\n")
699701
pathContent.WriteString(` path.WriteString("/")` + "\n")
700702
pathContent.WriteString(` path.WriteString(r.` + p + `)` + "\n")
701-
pathContent.WriteString(`if instrument, ok := r.instrument.(Instrumentation); ok {
703+
pathContent.WriteString(`if instrument, ok := r.Instrument.(Instrumentation); ok {
702704
instrument.RecordPathPart(ctx, "` + a.Name + `", r.` + p + `)
703705
}` + "\n")
704706
pathContent.WriteString(` }` + "\n")
@@ -707,7 +709,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(providedCtx context.
707709
pathContent.WriteString(` if len(r.` + p + `) > 0 {` + "\n")
708710
pathContent.WriteString(` path.WriteString("/")` + "\n")
709711
pathContent.WriteString(` path.WriteString(strings.Join(r.` + p + `, ","))` + "\n")
710-
pathContent.WriteString(`if instrument, ok := r.instrument.(Instrumentation); ok {
712+
pathContent.WriteString(`if instrument, ok := r.Instrument.(Instrumentation); ok {
711713
instrument.RecordPathPart(ctx, "` + a.Name + `", strings.Join(r.` + p + `, ","))
712714
}` + "\n")
713715
pathContent.WriteString(` }` + "\n")
@@ -717,7 +719,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(providedCtx context.
717719
pathContent.WriteString(` path.Grow(1 + len(value))` + "\n")
718720
pathContent.WriteString(` path.WriteString("/")` + "\n")
719721
pathContent.WriteString(` path.WriteString(value)` + "\n")
720-
pathContent.WriteString(`if instrument, ok := r.instrument.(Instrumentation); ok {
722+
pathContent.WriteString(`if instrument, ok := r.Instrument.(Instrumentation); ok {
721723
instrument.RecordPathPart(ctx, "` + a.Name + `", value)
722724
}` + "\n")
723725
pathContent.WriteString(` }` + "\n")
@@ -857,7 +859,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(providedCtx context.
857859
g.w(`req, err := newRequest(method, path.String(), ` + httpBody + `)` + "\n")
858860

859861
g.w(`if err != nil {
860-
if instrument, ok := r.instrument.(Instrumentation); ok {
862+
if instrument, ok := r.Instrument.(Instrumentation); ok {
861863
instrument.RecordError(ctx, err)
862864
}
863865
return nil, err
@@ -894,7 +896,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(providedCtx context.
894896
}` + "\n\n")
895897

896898
g.w(`
897-
if instrument, ok := r.instrument.(Instrumentation); ok {
899+
if instrument, ok := r.Instrument.(Instrumentation); ok {
898900
instrument.BeforeRequest(req, "` + g.Endpoint.Name + `")
899901
`)
900902
if g.Endpoint.Body != nil {
@@ -906,11 +908,11 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(providedCtx context.
906908
g.w(`}`)
907909
g.w(`
908910
res, err := transport.Perform(req)
909-
if instrument, ok := r.instrument.(Instrumentation); ok {
911+
if instrument, ok := r.Instrument.(Instrumentation); ok {
910912
instrument.AfterRequest(req, "elasticsearch", "` + g.Endpoint.Name + `")
911913
}
912914
if err != nil {
913-
if instrument, ok := r.instrument.(Instrumentation); ok {
915+
if instrument, ok := r.Instrument.(Instrumentation); ok {
914916
instrument.RecordError(ctx, err)
915917
}
916918
return nil, err

0 commit comments

Comments
 (0)