metaheader.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Licensed to Elasticsearch B.V. under one or more contributor
  2. // license agreements. See the NOTICE file distributed with
  3. // this work for additional information regarding copyright
  4. // ownership. Elasticsearch B.V. licenses this file to you under
  5. // the Apache License, Version 2.0 (the "License"); you may
  6. // not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. package estransport
  18. import (
  19. "regexp"
  20. "runtime"
  21. "strings"
  22. )
  23. // HeaderClientMeta Key for the HTTP Header related to telemetry data sent with
  24. // each request to Elasticsearch.
  25. const HeaderClientMeta = "x-elastic-client-meta"
  26. var metaReVersion = regexp.MustCompile("([0-9.]+)(.*)")
  27. func initMetaHeader() string {
  28. var b strings.Builder
  29. var strippedGoVersion string
  30. var strippedEsVersion string
  31. strippedEsVersion = buildStrippedVersion(Version)
  32. strippedGoVersion = buildStrippedVersion(runtime.Version())
  33. var duos = [][]string{
  34. {
  35. "es",
  36. strippedEsVersion,
  37. },
  38. {
  39. "go",
  40. strippedGoVersion,
  41. },
  42. {
  43. "t",
  44. strippedEsVersion,
  45. },
  46. {
  47. "hc",
  48. strippedGoVersion,
  49. },
  50. }
  51. var arr []string
  52. for _, duo := range duos {
  53. arr = append(arr, strings.Join(duo, "="))
  54. }
  55. b.WriteString(strings.Join(arr, ","))
  56. return b.String()
  57. }
  58. func buildStrippedVersion(version string) string {
  59. v := metaReVersion.FindStringSubmatch(version)
  60. if len(v) == 3 && !strings.Contains(version, "devel") {
  61. switch {
  62. case v[2] != "":
  63. return v[1] + "p"
  64. default:
  65. return v[1]
  66. }
  67. }
  68. return "0.0p"
  69. }