-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'beats-otel-collector' into esotel-beats
- Loading branch information
Showing
15 changed files
with
19,102 additions
and
11,900 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package elasticsearch | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter" | ||
"go.opentelemetry.io/collector/config/confighttp" | ||
"go.opentelemetry.io/collector/config/configopaque" | ||
"go.opentelemetry.io/collector/exporter/exporterbatcher" | ||
|
||
"github.com/elastic/beats/v7/libbeat/cloudid" | ||
"github.com/elastic/beats/v7/libbeat/common" | ||
"github.com/elastic/beats/v7/libbeat/outputs" | ||
"github.com/elastic/elastic-agent-libs/config" | ||
) | ||
|
||
// ToOtelConfig converts a Beat config into an OTel elasticsearch exporter config | ||
func ToOtelConfig(beatCfg *config.C) (*elasticsearchexporter.Config, error) { | ||
// Handle cloud.id the same way Beats does, this will also handle | ||
// extracting the Kibana URL (which is required to handle ILM on | ||
// Beats side (currently not supported by ES OTel exporter). | ||
if err := cloudid.OverwriteSettings(beatCfg); err != nil { | ||
return nil, fmt.Errorf("cannot read cloudid: %w", err) | ||
} | ||
|
||
esRawCfg, err := beatCfg.Child("output.elasticsearch", -1) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not parse Elasticsearch output configuration: %w", err) | ||
} | ||
escfg := defaultConfig | ||
if err := esRawCfg.Unpack(&escfg); err != nil { | ||
return nil, err | ||
} | ||
|
||
esToOTelOptions := struct { | ||
Index string `config:"index"` | ||
Pipeline string `config:"pipeline"` | ||
ProxyURL string `config:"proxy_url"` | ||
Hosts []string `config:"hosts" validate:"required"` | ||
}{} | ||
|
||
if err := esRawCfg.Unpack(&esToOTelOptions); err != nil { | ||
return nil, fmt.Errorf("cannot parse Elasticsearch config: %w", err) | ||
} | ||
|
||
hosts := []string{} | ||
for _, h := range esToOTelOptions.Hosts { | ||
esURL, err := common.MakeURL(escfg.Protocol, escfg.Path, h, 9200) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot generate ES URL from host %q", err) | ||
} | ||
hosts = append(hosts, esURL) | ||
} | ||
|
||
// The workers config is can be configured using two keys, so we leverage | ||
// the already existing code to handle it by using `output.HostWorkerCfg`. | ||
workersCfg := outputs.HostWorkerCfg{} | ||
if err := esRawCfg.Unpack(&workersCfg); err != nil { | ||
return nil, fmt.Errorf("cannot read worker/workers from Elasticsearch config: %w", err) | ||
} | ||
|
||
headers := make(map[string]configopaque.String, len(escfg.Headers)) | ||
for k, v := range escfg.Headers { | ||
headers[k] = configopaque.String(v) | ||
} | ||
|
||
otelTLSConfg, err := outputs.TLSCommonToOtel(escfg.Transport.TLS) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot convert SSL config into OTel: %w", err) | ||
} | ||
|
||
otelcfg := elasticsearchexporter.Config{ | ||
LogsIndex: esToOTelOptions.Index, // index | ||
Pipeline: esToOTelOptions.Pipeline, // pipeline | ||
Endpoints: hosts, // hosts, protocol, path, port | ||
NumWorkers: workersCfg.NumWorkers(), // worker/workers | ||
|
||
Authentication: elasticsearchexporter.AuthenticationSettings{ | ||
User: escfg.Username, // username | ||
Password: configopaque.String(escfg.Password), // password | ||
APIKey: configopaque.String(escfg.APIKey), //api_key | ||
}, | ||
|
||
// HTTP Client configuration | ||
ClientConfig: confighttp.ClientConfig{ | ||
ProxyURL: esToOTelOptions.ProxyURL, // proxy_url | ||
Headers: headers, // headers | ||
Timeout: escfg.Transport.Timeout, // timeout | ||
IdleConnTimeout: &escfg.Transport.IdleConnTimeout, // idle_connection_connection_timeout | ||
TLSSetting: otelTLSConfg, | ||
}, | ||
|
||
// Backoff settings | ||
Retry: elasticsearchexporter.RetrySettings{ | ||
Enabled: true, | ||
InitialInterval: escfg.Backoff.Init, // backoff.init | ||
MaxInterval: escfg.Backoff.Max, // backoff.max | ||
}, | ||
|
||
// Batching configuration | ||
Batcher: elasticsearchexporter.BatcherConfig{ | ||
Enabled: ptr(true), | ||
MaxSizeConfig: exporterbatcher.MaxSizeConfig{ | ||
MaxSizeItems: escfg.BulkMaxSize, // bulk_max_size | ||
}, | ||
}, | ||
} | ||
|
||
return &otelcfg, nil | ||
} | ||
|
||
func ptr[T any](v T) *T { | ||
var p T | ||
p = v | ||
return &p | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package elasticsearch | ||
|
||
import ( | ||
_ "embed" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/elastic/elastic-agent-libs/config" | ||
) | ||
|
||
//go:embed testdata/filebeat.yml | ||
var beatYAMLCfg string | ||
|
||
//go:embed testdata/certs/client.crt | ||
var clientCertPem string | ||
|
||
//go:embed testdata/expectedCaPem.crt | ||
var wantCAPem string | ||
|
||
// Generating certs: | ||
// Root CA: openssl req -x509 -ca -sha256 -days 1825 -newkey rsa:2048 -keyout rootCA.key -out rootCA.crt -passout pass:changeme | ||
// Server Cert: openssl req -newkey rsa:2048 -keyout server.key -x509 -days 3650 -out server.crt -passout pass:changeme | ||
// Client Cert: openssl req -newkey rsa:2048 -keyout client.key -x509 -days 3650 -out client.crt -passout pass:changeme -extensions usr_cert | ||
func TestToOtelConfig(t *testing.T) { | ||
beatCfg := config.MustNewConfigFrom(beatYAMLCfg) | ||
|
||
otelCfg, err := ToOtelConfig(beatCfg) | ||
if err != nil { | ||
t.Fatalf("could not convert Beat config to OTel elasicsearch exporter: %s", err) | ||
} | ||
|
||
if otelCfg.Endpoint != "" { | ||
t.Errorf("OTel endpoint must be emtpy got %s", otelCfg.Endpoint) | ||
} | ||
|
||
expectedHost := "https://es-hostname.elastic.co:443" | ||
if len(otelCfg.Endpoints) != 1 || otelCfg.Endpoints[0] != expectedHost { | ||
t.Errorf("OTel endpoints must contain only %q, got %q", expectedHost, otelCfg.Endpoints) | ||
} | ||
|
||
if got, want := otelCfg.Authentication.User, "elastic-cloud"; got != want { | ||
t.Errorf("expecting User %q, got %q", want, got) | ||
} | ||
|
||
if got, want := string(otelCfg.Authentication.Password), "password"; got != want { | ||
t.Errorf("expecting password to be '%s', got '%s' instead", want, got) | ||
} | ||
|
||
if got, want := string(otelCfg.Authentication.APIKey), "secret key"; got != want { | ||
t.Errorf("expecting api_key to be '%s', got '%s' instead", want, got) | ||
} | ||
|
||
if got, want := otelCfg.LogsIndex, "some-index"; got != want { | ||
t.Errorf("expecting logs index to be '%s', got '%s' instead", want, got) | ||
} | ||
|
||
if got, want := otelCfg.Pipeline, "some-ingest-pipeline"; got != want { | ||
t.Errorf("expecting pipeline to be '%s', got '%s' instead", want, got) | ||
} | ||
|
||
if got, want := otelCfg.ClientConfig.ProxyURL, "https://proxy.url"; got != want { | ||
t.Errorf("expecting proxy URL to be '%s', got '%s' instead", want, got) | ||
} | ||
|
||
if got, want := string(otelCfg.ClientConfig.TLSSetting.CertPem), clientCertPem; got != want { | ||
t.Errorf("expecting client certificate %q got %q", want, got) | ||
} | ||
|
||
gotCAPem := strings.TrimSpace(string(otelCfg.ClientConfig.TLSSetting.CAPem)) | ||
wantCAPem = strings.TrimSpace(wantCAPem) | ||
if gotCAPem != wantCAPem { | ||
t.Errorf("expecting CA PEM:\n%s\ngot:\n%s", wantCAPem, gotCAPem) | ||
} | ||
|
||
if !*otelCfg.Batcher.Enabled { | ||
t.Error("expecting batcher.enabled to be true") | ||
} | ||
|
||
if got, want := otelCfg.Batcher.MaxSizeItems, 42; got != want { | ||
t.Errorf("expecting batcher.max_size_items = %d got %d", want, got) | ||
} | ||
|
||
if !otelCfg.Retry.Enabled { | ||
t.Error("expecting retyr.enabled to be true") | ||
} | ||
|
||
if got, want := otelCfg.Retry.InitialInterval, time.Second*42; got != want { | ||
t.Errorf("expecting retry.initial_interval '%s', got '%s'", got, want) | ||
} | ||
|
||
if got, want := otelCfg.NumWorkers, 30; got != want { | ||
t.Errorf("expecting num_workers %d got %d", want, got) | ||
} | ||
|
||
headers := map[string]string{ | ||
"X-Header-1": "foo", | ||
"X-Bar-Header": "bar", | ||
} | ||
|
||
for k, v := range headers { | ||
gotV := string(otelCfg.Headers[k]) | ||
if gotV != v { | ||
t.Errorf("expecting header[%s]='%s', got '%s", k, v, gotV) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIID2jCCAsKgAwIBAgIUXSGhi1rVH7ftDmJ6TlavLsY/74MwDQYJKoZIhvcNAQEL | ||
BQAwgY8xCzAJBgNVBAYTAlVTMRAwDgYDVQQIDAdGbG9yaWRhMRAwDgYDVQQHDAdP | ||
cmxhbmRvMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxFzAVBgNV | ||
BAMMDkVsYXN0aWMgQ2xpZW50MSAwHgYJKoZIhvcNAQkBFhFjbGllbnRAZWxhc3Rp | ||
Yy5jbzAeFw0yNDA5MjUxOTAzNDZaFw0zNDA5MjMxOTAzNDZaMIGPMQswCQYDVQQG | ||
EwJVUzEQMA4GA1UECAwHRmxvcmlkYTEQMA4GA1UEBwwHT3JsYW5kbzEhMB8GA1UE | ||
CgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMRcwFQYDVQQDDA5FbGFzdGljIENs | ||
aWVudDEgMB4GCSqGSIb3DQEJARYRY2xpZW50QGVsYXN0aWMuY28wggEiMA0GCSqG | ||
SIb3DQEBAQUAA4IBDwAwggEKAoIBAQDAVGPlx4U2BpWfQlyMNraLMjdJAo4PjO2G | ||
rDbwg2cAO4QFbMECEiNHakvuJ3zVVDO+HsBdkLWr8nO4iXmZDokfDrOrANJuqq16 | ||
p022soC8pJQz9uIBWTnxDGd/wdofi4H+V5uaMhw961sgB7GREyBWNRBQzhcFyQEP | ||
XkR1/G52PcuzM5H9cnOSy7jc62g8Pkk8c2eZu3ADmvgWSH0b5pFUIvKsq068QjKP | ||
qoHYXn38d/SSeCX57tzKsj+mBzp0cr1f9jeXmKeu68wPYG14aj9WmmY6ICAPvqPF | ||
BKNLhXn2xPlZzv93zjiUR5bnitenVxvsmwjn5XvlgH56/fh3Y3aPAgMBAAGjLDAq | ||
MAkGA1UdEwQCMAAwHQYDVR0OBBYEFJlsmqi3qid9YoWj4N7GQvAywRzbMA0GCSqG | ||
SIb3DQEBCwUAA4IBAQCAtBwyiRYAGeAcN/UuMEcnMXP8QNrnCO/unoCbyFsByFQT | ||
TcwMrS441hGPp/cAa8Fx0cP+oqrO99G1YHCzhprYVqIi/W9MsvRnR7Nh8SSS2/ld | ||
0Gv9g+DU89NMzE5hlMCt5V0ydKbRj+ChKDsKlgQSopbrArjxHQv4Hb234HSZAR5N | ||
OkJ1rNCF7wMD+xlNzEWZAHl7qjHuG8C4xWP207dXGYuY3064rBqv9hypLxj7RuZn | ||
qesVBabxXBCL6Y1foh5OLLHyEWw28yfK/PnVdqU0lLrBhW9VJ6mQ9XCwZxf/tlSk | ||
B3FafTQk4ZtU+4bVJuiAiQI7DeqpIFU6Lczds2gG | ||
-----END CERTIFICATE----- |
Oops, something went wrong.