Skip to content

Commit

Permalink
Update attributes processor, tidy go mod
Browse files Browse the repository at this point in the history
  • Loading branch information
ptodev committed Jun 16, 2023
1 parent dfab9f8 commit 356d973
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 48 deletions.
66 changes: 30 additions & 36 deletions component/otelcol/config_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,46 +241,40 @@ var (
_ encoding.TextUnmarshaler = (*SeverityLevel)(nil)
)

// The severity levels should be in sync with "opentelemetry-collector/pdata/plog/logs.go"
var severityLevels = map[SeverityLevel]struct {
sevLevelNum int32
otelLogLevelStr string
}{
"TRACE": {1, "Trace"},
"TRACE2": {2, "Trace2"},
"TRACE3": {3, "Trace3"},
"TRACE4": {4, "Trace4"},
"DEBUG": {5, "Debug"},
"DEBUG2": {6, "Debug2"},
"DEBUG3": {7, "Debug3"},
"DEBUG4": {8, "Debug4"},
"INFO": {9, "Info"},
"INFO2": {10, "Info2"},
"INFO3": {11, "Info3"},
"INFO4": {12, "Info4"},
"WARN": {13, "Warn"},
"WARN2": {14, "Warn2"},
"WARN3": {15, "Warn3"},
"WARN4": {16, "Warn4"},
"ERROR": {17, "Error"},
"ERROR2": {18, "Error2"},
"ERROR3": {19, "Error3"},
"ERROR4": {20, "Error4"},
"FATAL": {21, "Fatal"},
"FATAL2": {22, "Fatal2"},
"FATAL3": {23, "Fatal3"},
"FATAL4": {24, "Fatal4"},
// The severity levels should be in sync with "opentelemetry-collector/pdata/plog/severity_number.go"
var severityLevels = map[SeverityLevel]plog.SeverityNumber{
"TRACE": 1,
"TRACE2": 2,
"TRACE3": 3,
"TRACE4": 4,
"DEBUG": 5,
"DEBUG2": 6,
"DEBUG3": 7,
"DEBUG4": 8,
"INFO": 9,
"INFO2": 10,
"INFO3": 11,
"INFO4": 12,
"WARN": 13,
"WARN2": 14,
"WARN3": 15,
"WARN4": 16,
"ERROR": 17,
"ERROR2": 18,
"ERROR3": 19,
"ERROR4": 20,
"FATAL": 21,
"FATAL2": 22,
"FATAL3": 23,
"FATAL4": 24,
}

// UnmarshalText implements encoding.TextUnmarshaler for SeverityLevel.
func (sl *SeverityLevel) UnmarshalText(text []byte) error {
if sevLevelInfo, exists := severityLevels[SeverityLevel(text)]; exists {
// Check if this is a valid plog severity number
plogInt := plog.SeverityNumber(sevLevelInfo.sevLevelNum)
if plogInt.String() == sevLevelInfo.otelLogLevelStr {
*sl = SeverityLevel(sevLevelInfo.otelLogLevelStr)
return nil
}
agentSevLevelStr := SeverityLevel(text)
if _, exists := severityLevels[agentSevLevelStr]; exists {
*sl = agentSevLevelStr
return nil
}
return fmt.Errorf("unrecognized severity level %q", string(text))
}
49 changes: 48 additions & 1 deletion component/otelcol/config_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/grafana/agent/component/otelcol"
"github.com/grafana/agent/pkg/river"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/plog"
"k8s.io/utils/pointer"
)

Expand Down Expand Up @@ -77,7 +78,7 @@ func TestConvertMatchProperties(t *testing.T) {
},
"log_bodies": []string{"AUTH.*"},
"log_severity_number": map[string]interface{}{
"min": int32(2),
"min": plog.SeverityNumber(2),
"match_undefined": true,
},
"log_severity_texts": []string{
Expand Down Expand Up @@ -200,6 +201,13 @@ func TestUnmarshalSeverityLevel(t *testing.T) {
match_undefined = true
`,
},
{
name: "valid INFO config without matching undefined",
cfg: `
min = "INFO"
match_undefined = false
`,
},
{
name: "valid WARN config",
cfg: `
Expand Down Expand Up @@ -258,3 +266,42 @@ func TestUnmarshalSeverityLevel(t *testing.T) {
})
}
}

func TestUnmarshalSeverityLevelString(t *testing.T) {
for _, sevLevelStr := range []string{
"TRACE",
"TRACE2",
"TRACE3",
"TRACE4",
"DEBUG",
"DEBUG2",
"DEBUG3",
"DEBUG4",
"INFO",
"INFO2",
"INFO3",
"INFO4",
"WARN",
"WARN2",
"WARN3",
"WARN4",
"ERROR",
"ERROR2",
"ERROR3",
"ERROR4",
"FATAL",
"FATAL2",
"FATAL3",
"FATAL4",
} {
sevLevelStr := sevLevelStr

t.Run(sevLevelStr, func(t *testing.T) {
t.Parallel()

var sl otelcol.SeverityLevel
require.NoError(t, sl.UnmarshalText([]byte(sevLevelStr)))
require.Equal(t, sevLevelStr, string(sl))
})
}
}
85 changes: 85 additions & 0 deletions component/otelcol/processor/attributes/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package attributes_test

import (
"context"
"fmt"
"net"
"testing"
"time"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/grafana/agent/pkg/river"
"github.com/grafana/agent/pkg/util"
"github.com/grafana/dskit/backoff"
"github.com/mitchellh/mapstructure"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/attributesprocessor"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/client"
Expand All @@ -22,6 +24,89 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace"
)

// These are tests for SeverityLevel and not for the attributes processor as a whole.
// However, because Otel's LogSeverityNumberMatchProperties structure is internal
// we are not able ot test it directly.
// The only way is to create a whole attributesprocessor.Config, because that struct is public.
// This is why the test is in attributes_test.go instead of config_filter_test.go.
func TestSeverityLevelMatchesOtel(t *testing.T) {
type TestDefinition struct {
name string
cfg string
expectedOtelSevStr string
}

var tests []TestDefinition

for _, testInfo := range []struct {
agentSevStr string
otelSevStr string
}{
{"TRACE", "Trace"},
{"TRACE2", "Trace2"},
{"TRACE3", "Trace3"},
{"TRACE4", "Trace4"},
{"DEBUG", "Debug"},
{"DEBUG2", "Debug2"},
{"DEBUG3", "Debug3"},
{"DEBUG4", "Debug4"},
{"INFO", "Info"},
{"INFO2", "Info2"},
{"INFO3", "Info3"},
{"INFO4", "Info4"},
{"WARN", "Warn"},
{"WARN2", "Warn2"},
{"WARN3", "Warn3"},
{"WARN4", "Warn4"},
{"ERROR", "Error"},
{"ERROR2", "Error2"},
{"ERROR3", "Error3"},
{"ERROR4", "Error4"},
{"FATAL", "Fatal"},
{"FATAL2", "Fatal2"},
{"FATAL3", "Fatal3"},
{"FATAL4", "Fatal4"},
} {
cfgTemplate := `
match_type = "strict"
log_severity {
min = "%s"
match_undefined = true
}
`

newTest := TestDefinition{
name: testInfo.agentSevStr,
cfg: fmt.Sprintf(cfgTemplate, testInfo.agentSevStr),
expectedOtelSevStr: testInfo.otelSevStr,
}
tests = append(tests, newTest)
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var matchProperties otelcol.MatchProperties
err := river.Unmarshal([]byte(tt.cfg), &matchProperties)

require.NoError(t, err)

input := make(map[string]interface{})

matchConfig, err := matchProperties.Convert()
require.NoError(t, err)
input["include"] = matchConfig

var result attributesprocessor.Config
err = mapstructure.Decode(input, &result)
require.NoError(t, err)

require.Equal(t, tt.expectedOtelSevStr, result.MatchConfig.Include.LogSeverityNumber.Min.String())
})
}
}

// A lot of the TestDecode tests were inspired by tests in the Otel repo:
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.63.0/processor/attributesprocessor/testdata/config.yaml

Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ require (
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.24.0
golang.org/x/crypto v0.9.0
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
golang.org/x/net v0.10.0
golang.org/x/oauth2 v0.8.0
golang.org/x/sys v0.8.0
Expand Down Expand Up @@ -241,7 +241,6 @@ require (
github.com/Microsoft/hcsshim v0.9.8 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210920160938-87db9fbc61c7 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
Expand Down Expand Up @@ -394,7 +393,7 @@ require (
github.com/hashicorp/vic v1.5.1-0.20190403131502-bbfe86ec9443 // indirect
github.com/hashicorp/yamux v0.0.0-20190923154419-df201c70410d // indirect
github.com/hetznercloud/hcloud-go v1.41.0 // indirect
github.com/hodgesds/perf-utils v0.5.1 // indirect
github.com/hodgesds/perf-utils v0.7.0 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/illumos/go-kstat v0.0.0-20210513183136-173c9b0a9973 // indirect
github.com/imdario/mergo v0.3.15 // indirect
Expand Down Expand Up @@ -549,7 +548,6 @@ require (
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
github.com/xo/dburl v0.13.0 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.etcd.io/etcd/api/v3 v3.5.7 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.7 // indirect
Expand All @@ -567,6 +565,7 @@ require (
gonum.org/v1/gonum v0.13.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6 // indirect
gopkg.in/fsnotify/fsnotify.v1 v1.4.7 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
11 changes: 4 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,6 @@ github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN
github.com/99designs/keyring v1.2.1 h1:tYLp1ULvO7i3fI5vE21ReQuj99QFSs7lGm0xWyJo87o=
github.com/99designs/keyring v1.2.1/go.mod h1:fc+wB5KTk9wQ9sDx0kFXB3A0MaeGHM9AwRStKOQ5vOA=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20210715213245-6c3934b029d8 h1:V8krnnfGj4pV65YLUm3C0/8bl7V5Nry2Pwvy3ru/wLc=
github.com/AlekSi/pointer v1.1.0 h1:SSDMPcXD9jSl8FPy9cRzoRaMJtm9g9ggGTxecRUbQoI=
github.com/AlekSi/pointer v1.1.0/go.mod h1:y7BvfRI3wXPWKXEBhU71nbnIEEZX0QTSB2Bj48UJIZE=
github.com/Azure/azure-amqp-common-go/v3 v3.0.0/go.mod h1:SY08giD/XbhTz07tJdpw1SoxQXHPN30+DI3Z04SYqyg=
github.com/Azure/azure-event-hubs-go/v3 v3.2.0/go.mod h1:BPIIJNH/l/fVHYq3Rm6eg4clbrULrQ3q7+icmqHyyLc=
github.com/Azure/azure-pipeline-go v0.1.8/go.mod h1:XA1kFWRVhSK+KNFiOhfv83Fv8L9achrP7OxIzeTn1Yg=
Expand Down Expand Up @@ -1248,7 +1246,6 @@ github.com/frankban/quicktest v1.10.0/go.mod h1:ui7WezCLWMWxVWr1GETZY3smRy0G4KWq
github.com/frankban/quicktest v1.10.2/go.mod h1:K+q6oSqb0W0Ininfk863uOk1lMy69l/P6txr3mVT54s=
github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k=
github.com/frankban/quicktest v1.13.0/go.mod h1:qLE0fzW0VuyUAJgPU19zByoIr0HtCHN/r/VLSOOIySU=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down Expand Up @@ -3067,10 +3064,10 @@ github.com/tencentcloud/tencentcloud-sdk-go v1.0.162/go.mod h1:asUz5BPXxgoPGaRgZ
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.194/go.mod h1:yrBKWhChnDqNz1xuXdSbWXG56XawEq0G5j1lg4VwBD4=
github.com/tencentyun/cos-go-sdk-v5 v0.7.31/go.mod h1:4E4+bQ2gBVJcgEC9Cufwylio4mXOct2iu05WjgEBx1o=
github.com/tent/http-link-go v0.0.0-20130702225549-ac974c61c2f9/go.mod h1:RHkNRtSLfOK7qBTHaeSX1D6BNpI3qw7NTxsmNr4RvN8=
github.com/testcontainers/testcontainers-go v0.20.2-0.20230613144507-029fef7f7480 h1:pCwdY2BVnSEA85QzoK+NNhF11mDcCekGg7UlKWC45Gg=
github.com/testcontainers/testcontainers-go v0.20.2-0.20230613144507-029fef7f7480/go.mod h1:c1ez3WVRHq7T/Aj+X3TIipFBwkBaNT5iNCY8+1b83Ng=
github.com/testcontainers/testcontainers-go/modules/k3s v0.0.0-20230613144507-029fef7f7480 h1:ggqIrWL6xt5ArABCvvRkNN7ek+50B7otkDDzfoNbNA4=
github.com/testcontainers/testcontainers-go/modules/k3s v0.0.0-20230613144507-029fef7f7480/go.mod h1:Pa91ahCbzRB6d9FBi6UAjurTEm7WmyBVeuklLkwAKKs=
github.com/testcontainers/testcontainers-go v0.20.2-0.20230615142642-c175df34bd1d h1:HFijN6Qvpl+Cbt5M3lrFZcp4Gy+3RXUqn5+6lk9+/X8=
github.com/testcontainers/testcontainers-go v0.20.2-0.20230615142642-c175df34bd1d/go.mod h1:c1ez3WVRHq7T/Aj+X3TIipFBwkBaNT5iNCY8+1b83Ng=
github.com/testcontainers/testcontainers-go/modules/k3s v0.0.0-20230615142642-c175df34bd1d h1:KyYCHo9iBoQYw5AzcozD/77uNbFlRjTmMTA7QjSxHOQ=
github.com/testcontainers/testcontainers-go/modules/k3s v0.0.0-20230615142642-c175df34bd1d/go.mod h1:Pa91ahCbzRB6d9FBi6UAjurTEm7WmyBVeuklLkwAKKs=
github.com/tg123/go-htpasswd v1.2.1 h1:i4wfsX1KvvkyoMiHZzjS0VzbAPWfxzI8INcZAKtutoU=
github.com/tg123/go-htpasswd v1.2.1/go.mod h1:erHp1B86KXdwQf1X5ZrLb7erXZnWueEQezb2dql4q58=
github.com/thanos-io/thanos v0.19.1-0.20211126105533-c5505f5eaa7d/go.mod h1:sfnKJG7cDA41ixNL4gsTJEa3w9Qt8lwAjw+dqRMSDG0=
Expand Down

0 comments on commit 356d973

Please sign in to comment.