Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/beats-otel-collector' into bea…
Browse files Browse the repository at this point in the history
…ts-otel-collector
  • Loading branch information
AndersonQ committed Sep 26, 2024
2 parents 52b1110 + 08ea5d5 commit 1e0e3a3
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
5 changes: 5 additions & 0 deletions libbeat/outputs/tls_to_otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ import (
func TLSCommonToOTel(tlscfg *tlscommon.Config) (configtls.ClientConfig, error) {
logger := logp.L().Named("tls-to-otel")
insecureSkipVerify := false

if tlscfg == nil {
return configtls.ClientConfig{}, nil
}

if tlscfg.VerificationMode == tlscommon.VerifyNone {
insecureSkipVerify = true
}
Expand Down
91 changes: 91 additions & 0 deletions x-pack/filebeat/cmd/customProvider/beatprovider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package customProvider

Check failure on line 1 in x-pack/filebeat/cmd/customProvider/beatprovider.go

View workflow job for this annotation

GitHub Actions / check

is missing the license header

import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"strings"

"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/outputs/elasticsearch"
"github.com/elastic/elastic-agent-libs/config"
"github.com/go-viper/mapstructure/v2"
"go.opentelemetry.io/collector/confmap"
)

const schemeName = "filebeat"

type provider struct{}

func NewFactory() confmap.ProviderFactory {
return confmap.NewProviderFactory(newProvider)
}

func newProvider(confmap.ProviderSettings) confmap.Provider {
return &provider{}
}

func (fmp *provider) Retrieve(_ context.Context, uri string, _ confmap.WatcherFunc) (*confmap.Retrieved, error) {
if !strings.HasPrefix(uri, schemeName+":") {
return nil, fmt.Errorf("%q uri is not supported by %q provider", uri, schemeName)
}

cfg, err := cfgfile.Load(filepath.Clean(uri[len(schemeName)+1:]), nil)
if err != nil {
return nil, err

}

esCfg, err := elasticsearch.ToOtelConfig(cfg)
if err != nil {
return nil, err
}

var tempMap map[string]any
err = mapstructure.Decode(esCfg, &tempMap)
if err != nil {
return nil, err
}

newCfg := config.NewConfig()
newCfg.SetString("otelconsumer", -1, "")

cfg.SetChild("output", -1, newCfg)

var receiverMap map[string]any
cfg.Unpack(&receiverMap)

cfgMap := map[string]any{
"exporters": map[string]any{
"elasticsearch": tempMap,
"debug": map[string]any{},
},
"receivers": map[string]any{
"filebeatreceiver": receiverMap,
},
"service": map[string]any{
"pipeline": map[string]any{
"logs": map[string]any{
"exporters": []string{
"debug",
},
"receivers": []string{"filebeatreceiver"},
},
},
},
}

s, _ := json.MarshalIndent(cfgMap, "", " ")

fmt.Println(string(s))
return confmap.NewRetrieved(cfgMap)
}

func (*provider) Scheme() string {
return schemeName
}

func (*provider) Shutdown(context.Context) error {
return nil
}
13 changes: 13 additions & 0 deletions x-pack/filebeat/cmd/customProvider/beatprovider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package customProvider

Check failure on line 1 in x-pack/filebeat/cmd/customProvider/beatprovider_test.go

View workflow job for this annotation

GitHub Actions / check

is missing the license header

import (
"context"
"fmt"
"testing"
)

func TestBeatProvider(t *testing.T) {
p := provider{}
fmt.Println(p.Retrieve(context.Background(), "filebeat:/Users/khushijain/Documents/beats/x-pack/filebeat/filebeat.yml", nil))

}
4 changes: 3 additions & 1 deletion x-pack/filebeat/cmd/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"

"github.com/elastic/beats/v7/x-pack/filebeat/cmd/customProvider"
"github.com/elastic/beats/v7/x-pack/filebeat/fbreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/filelogreceiver"
Expand Down Expand Up @@ -71,12 +72,13 @@ func OtelCmd() *cobra.Command {
Factories: components,
ConfigProviderSettings: otelcol.ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: []string{"file:/Users/vihasmakwana/Desktop/Vihas/elastic/elastic-agent/otel.yml"},
URIs: []string{"filebeat:/Users/vihasmakwana/Desktop/Vihas/elastic/elastic-agent/otel.yml"},
ProviderFactories: []confmap.ProviderFactory{
fileprovider.NewFactory(),
httpprovider.NewFactory(),
httpsprovider.NewFactory(),
yamlprovider.NewFactory(),
customProvider.NewFactory(),
},
},
},
Expand Down

0 comments on commit 1e0e3a3

Please sign in to comment.