Skip to content

Commit 5f3dd3e

Browse files
authored
Add the Elastic product origin header when talking to Elasticsearch or Kibana. (#29966)
Set the beats product origin header by default when communicating with Elasticsearch or Kibana.
1 parent 33fc960 commit 5f3dd3e

7 files changed

Lines changed: 66 additions & 18 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
18+
// Package productorigin defines the Elastic product origin header.
19+
package productorigin
20+
21+
const (
22+
// Identifies a request as originating from an Elastic product. Has the side effect of
23+
// suppressing Elasticsearch API deprecation warnings in Kibana when set.
24+
Header = "X-Elastic-Product-Origin"
25+
26+
// Applicable values from https://github.com/elastic/kibana/blob/main/x-pack/plugins/upgrade_assistant/common/constants.ts#L50
27+
Observability = "observability"
28+
Beats = "beats"
29+
)

libbeat/esleg/eslegclient/connection.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"go.elastic.co/apm/module/apmelasticsearch"
3131

3232
"github.com/elastic/beats/v7/libbeat/common"
33+
"github.com/elastic/beats/v7/libbeat/common/productorigin"
3334
"github.com/elastic/beats/v7/libbeat/common/transport"
3435
"github.com/elastic/beats/v7/libbeat/common/transport/httpcommon"
3536
"github.com/elastic/beats/v7/libbeat/common/transport/kerberos"
@@ -84,7 +85,9 @@ type ConnectionSettings struct {
8485
func NewConnection(s ConnectionSettings) (*Connection, error) {
8586
logger := logp.NewLogger("esclientleg")
8687

87-
s = settingsWithDefaults(s)
88+
if s.IdleConnTimeout == 0 {
89+
s.IdleConnTimeout = 1 * time.Minute
90+
}
8891

8992
u, err := url.Parse(s.URL)
9093
if err != nil {
@@ -117,6 +120,14 @@ func NewConnection(s ConnectionSettings) (*Connection, error) {
117120
}
118121
userAgent := useragent.UserAgent(s.Beatname)
119122

123+
// Default the product origin header to beats if it wasn't already set.
124+
if _, ok := s.Headers[productorigin.Header]; !ok {
125+
if s.Headers == nil {
126+
s.Headers = make(map[string]string)
127+
}
128+
s.Headers[productorigin.Header] = productorigin.Beats
129+
}
130+
120131
httpClient, err := s.Transport.Client(
121132
httpcommon.WithLogger(logger),
122133
httpcommon.WithIOStats(s.Observer),
@@ -155,15 +166,6 @@ func NewConnection(s ConnectionSettings) (*Connection, error) {
155166
return &conn, nil
156167
}
157168

158-
func settingsWithDefaults(s ConnectionSettings) ConnectionSettings {
159-
settings := s
160-
if settings.IdleConnTimeout == 0 {
161-
settings.IdleConnTimeout = 1 * time.Minute
162-
}
163-
164-
return settings
165-
}
166-
167169
// NewClients returns a list of Elasticsearch clients based on the given
168170
// configuration. It accepts the same configuration parameters as the Elasticsearch
169171
// output, except for the output specific configuration options. If multiple hosts

libbeat/esleg/eslegclient/connection_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"testing"
2626

2727
"github.com/stretchr/testify/require"
28+
29+
"github.com/elastic/beats/v7/libbeat/common/productorigin"
2830
)
2931

3032
func TestAPIKeyEncoding(t *testing.T) {
@@ -71,18 +73,21 @@ func TestHeaders(t *testing.T) {
7173
expected map[string][]string
7274
}{
7375
{input: map[string]string{
74-
"Accept": "application/vnd.elasticsearch+json;compatible-with=7",
75-
"Content-Type": "application/vnd.elasticsearch+json;compatible-with=7",
76-
"X-My-Header": "true"},
76+
"Accept": "application/vnd.elasticsearch+json;compatible-with=7",
77+
"Content-Type": "application/vnd.elasticsearch+json;compatible-with=7",
78+
productorigin.Header: "elastic-product",
79+
"X-My-Header": "true"},
7780
expected: map[string][]string{
78-
"Accept": {"application/vnd.elasticsearch+json;compatible-with=7"},
79-
"Content-Type": {"application/vnd.elasticsearch+json;compatible-with=7"},
80-
"X-My-Header": {"true"}}},
81+
"Accept": {"application/vnd.elasticsearch+json;compatible-with=7"},
82+
"Content-Type": {"application/vnd.elasticsearch+json;compatible-with=7"},
83+
productorigin.Header: {"elastic-product"},
84+
"X-My-Header": {"true"}}},
8185
{input: map[string]string{
8286
"X-My-Header": "true"},
8387
expected: map[string][]string{
84-
"Accept": {"application/json"},
85-
"X-My-Header": {"true"}}},
88+
"Accept": {"application/json"},
89+
productorigin.Header: {productorigin.Beats},
90+
"X-My-Header": {"true"}}},
8691
} {
8792
conn, err := NewConnection(ConnectionSettings{
8893
Headers: td.input,

metricbeat/module/elasticsearch/metricset.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/pkg/errors"
2525

26+
"github.com/elastic/beats/v7/libbeat/common/productorigin"
2627
"github.com/elastic/beats/v7/metricbeat/helper"
2728
"github.com/elastic/beats/v7/metricbeat/mb"
2829
"github.com/elastic/beats/v7/metricbeat/mb/parse"
@@ -90,6 +91,8 @@ func NewMetricSet(base mb.BaseMetricSet, servicePath string) (*MetricSet, error)
9091
return nil, err
9192
}
9293

94+
http.SetHeaderDefault(productorigin.Header, productorigin.Beats)
95+
9396
config := struct {
9497
Scope Scope `config:"scope"`
9598
XPackEnabled bool `config:"xpack.enabled"`

metricbeat/module/kibana/settings/settings.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package settings
2020
import (
2121
"fmt"
2222

23+
"github.com/elastic/beats/v7/libbeat/common/productorigin"
2324
"github.com/elastic/beats/v7/metricbeat/helper"
2425
"github.com/elastic/beats/v7/metricbeat/mb"
2526
"github.com/elastic/beats/v7/metricbeat/mb/parse"
@@ -77,6 +78,8 @@ func (m *MetricSet) init() (err error) {
7778
return err
7879
}
7980

81+
httpHelper.SetHeaderDefault(productorigin.Header, productorigin.Beats)
82+
8083
kibanaVersion, err := kibana.GetVersion(httpHelper, kibana.SettingsPath)
8184
if err != nil {
8285
return err

metricbeat/module/kibana/stats/stats.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/pkg/errors"
2424

25+
"github.com/elastic/beats/v7/libbeat/common/productorigin"
2526
"github.com/elastic/beats/v7/metricbeat/helper"
2627
"github.com/elastic/beats/v7/metricbeat/mb"
2728
"github.com/elastic/beats/v7/metricbeat/mb/parse"
@@ -84,6 +85,8 @@ func (m *MetricSet) init() error {
8485
return err
8586
}
8687

88+
statsHTTP.SetHeaderDefault(productorigin.Header, productorigin.Beats)
89+
8790
kibanaVersion, err := kibana.GetVersion(statsHTTP, kibana.StatsPath)
8891
if err != nil {
8992
return err

metricbeat/module/kibana/status/status.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package status
1919

2020
import (
21+
"github.com/elastic/beats/v7/libbeat/common/productorigin"
2122
"github.com/elastic/beats/v7/metricbeat/helper"
2223
"github.com/elastic/beats/v7/metricbeat/mb"
2324
"github.com/elastic/beats/v7/metricbeat/mb/parse"
@@ -59,6 +60,8 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
5960
return nil, err
6061
}
6162

63+
http.SetHeaderDefault(productorigin.Header, productorigin.Beats)
64+
6265
return &MetricSet{
6366
ms,
6467
http,

0 commit comments

Comments
 (0)