Skip to content

Commit

Permalink
Merge branch 'main' into ratelimit-data-race
Browse files Browse the repository at this point in the history
  • Loading branch information
mauri870 authored Mar 7, 2025
2 parents a20fc3f + 873743b commit 8f5e11d
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
14 changes: 14 additions & 0 deletions libbeat/otelbeat/beatconverter/beatconverter.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ func (c converter) Convert(_ context.Context, conf *confmap.Conf) error {
beatReceiverConfigKey + "::output::otelconsumer": nil,
}

// inject log level
receiverConfig, err := config.NewConfigFrom(receiverCfg.ToStringMap())
if err != nil {
return fmt.Errorf("Error getting receiver config: %w", err)
}

if level, _ := receiverConfig.String("logging.level", -1); level != "" {
out["service::telemetry::logs::level"], err = getOTelLogLevel(level)
if err != nil {
return fmt.Errorf("error injecting log level: %w", err)

}
}

err = conf.Merge(confmap.NewFromStringMap(out))
if err != nil {
return err
Expand Down
68 changes: 68 additions & 0 deletions libbeat/otelbeat/beatconverter/beatconverter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package beatconverter

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -278,6 +279,73 @@ service:
compareAndAssert(t, expOutput, input)

})
}

func TestLogLevel(t *testing.T) {
c := converter{}
tests := []struct {
name string
level string
expectedLevel string
expectedError string
}{
{
name: "test-debug",
level: "debug",
expectedLevel: "DEBUG",
},
{
name: "test-info",
level: "info",
expectedLevel: "INFO",
},
{
name: "test-warn",
level: "warning",
expectedLevel: "WARN",
},
{
name: "test-error",
level: "error",
expectedLevel: "ERROR",
},
{
name: "test-critical",
level: "critical",
expectedLevel: "ERROR",
},
{
name: "test-error",
level: "blabla",
expectedError: "unrecognized level: blabla",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
supportedInput := fmt.Sprintf(`
receivers:
filebeatreceiver:
logging:
level: %s
filebeat:
inputs:
- type: filestream
enabled: true
id: filestream-input-id
paths:
- /tmp/flog.log
`, test.level)
input := newFromYamlString(t, supportedInput)
err := c.Convert(context.Background(), input)
if test.expectedError != "" {
require.ErrorContains(t, err, test.expectedError)
} else {
require.NoError(t, err)
inputMap := input.Get("service::telemetry::logs::level")
require.Equal(t, test.expectedLevel, inputMap)
}
})
}

}

Expand Down
38 changes: 38 additions & 0 deletions libbeat/otelbeat/beatconverter/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 beatconverter

import (
"fmt"
"strings"
)

func getOTelLogLevel(level string) (string, error) {
switch strings.ToLower(level) {
case "debug":
return "DEBUG", nil
case "info":
return "INFO", nil
case "warning":
return "WARN", nil
case "error", "critical":
return "ERROR", nil
default:
return "", fmt.Errorf("unrecognized level: %s", level)
}
}

0 comments on commit 8f5e11d

Please sign in to comment.