@@ -35,6 +35,7 @@ import (
3535 "github.com/elastic/go-elasticsearch/v8/internal/version"
3636
3737 "github.com/elastic/elastic-transport-go/v8/elastictransport"
38+ tpversion "github.com/elastic/elastic-transport-go/v8/elastictransport/version"
3839)
3940
4041const (
@@ -43,12 +44,17 @@ const (
4344 // Version returns the package version as a string.
4445 Version = version .Client
4546 unknownProduct = "the client noticed that the server is not Elasticsearch and we do not support this unknown product"
47+
48+ // HeaderClientMeta Key for the HTTP Header related to telemetry data sent with
49+ // each request to Elasticsearch.
50+ HeaderClientMeta = "x-elastic-client-meta"
4651)
4752
4853var (
4954 esCompatHeader = "ELASTIC_CLIENT_APIVERSIONING"
5055 userAgent string
5156 reGoVersion = regexp .MustCompile (`go(\d+\.\d+\..+)` )
57+ reMetaVersion = regexp .MustCompile ("([0-9.]+)(.*)" )
5258)
5359
5460func init () {
@@ -105,6 +111,7 @@ type Config struct {
105111type Client struct {
106112 * esapi.API // Embeds the API methods
107113 Transport elastictransport.Interface
114+ metaHeader string
108115 compatibilityHeader bool
109116
110117 disableMetaHeader bool
@@ -216,6 +223,7 @@ func NewClient(cfg Config) (*Client, error) {
216223 client := & Client {
217224 Transport : tp ,
218225 disableMetaHeader : cfg .DisableMetaHeader ,
226+ metaHeader : initMetaHeader (tp ),
219227 compatibilityHeader : cfg .EnableCompatibilityMode || compatibilityHeader ,
220228 }
221229 client .API = esapi .New (client )
@@ -239,9 +247,9 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
239247 if ! c .disableMetaHeader {
240248 existingMetaHeader := req .Header .Get (HeaderClientMeta )
241249 if existingMetaHeader != "" {
242- req .Header .Set (HeaderClientMeta , strings .Join ([]string {metaHeader , existingMetaHeader }, "," ))
250+ req .Header .Set (HeaderClientMeta , strings .Join ([]string {c . metaHeader , existingMetaHeader }, "," ))
243251 } else {
244- req .Header .Add (HeaderClientMeta , metaHeader )
252+ req .Header .Add (HeaderClientMeta , c . metaHeader )
245253 }
246254 } else {
247255 req .Header .Del (HeaderClientMeta )
@@ -392,3 +400,61 @@ func initUserAgent() string {
392400
393401 return b .String ()
394402}
403+
404+ func initMetaHeader (transport interface {}) string {
405+ var b strings.Builder
406+ var strippedGoVersion string
407+ var strippedEsVersion string
408+ var strippedTransportVersion string
409+
410+ strippedEsVersion = buildStrippedVersion (Version )
411+ strippedGoVersion = buildStrippedVersion (runtime .Version ())
412+
413+ if _ , ok := transport .(* elastictransport.Client ); ok {
414+ strippedTransportVersion = buildStrippedVersion (tpversion .Transport )
415+ } else {
416+ strippedTransportVersion = strippedEsVersion
417+ }
418+
419+ var duos = [][]string {
420+ {
421+ "es" ,
422+ strippedEsVersion ,
423+ },
424+ {
425+ "go" ,
426+ strippedGoVersion ,
427+ },
428+ {
429+ "t" ,
430+ strippedTransportVersion ,
431+ },
432+ {
433+ "hc" ,
434+ strippedGoVersion ,
435+ },
436+ }
437+
438+ var arr []string
439+ for _ , duo := range duos {
440+ arr = append (arr , strings .Join (duo , "=" ))
441+ }
442+ b .WriteString (strings .Join (arr , "," ))
443+
444+ return b .String ()
445+ }
446+
447+ func buildStrippedVersion (version string ) string {
448+ v := reMetaVersion .FindStringSubmatch (version )
449+
450+ if len (v ) == 3 && ! strings .Contains (version , "devel" ) {
451+ switch {
452+ case v [2 ] != "" :
453+ return v [1 ] + "p"
454+ default :
455+ return v [1 ]
456+ }
457+ }
458+
459+ return "0.0p"
460+ }
0 commit comments