Skip to content

Commit

Permalink
otelcol: decouple otel/alloy component IDs (#882)
Browse files Browse the repository at this point in the history
Signed-off-by: Paschalis Tsilias <[email protected]>
  • Loading branch information
tpaschalis authored May 23, 2024
1 parent 3df2cd0 commit d018e6e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ Main (unreleased)

- Fix error on boot when using IPv6 advertise addresses without explicitly
specifying a port. (@matthewpi)

- Fix an issue where having long component labels (>63 chars) on otelcol.auth
components lead to a panic. (@tpaschalis)

### Other changes

Expand Down
20 changes: 19 additions & 1 deletion internal/component/otelcol/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package auth

import (
"context"
"fmt"
"hash/fnv"
"os"
"strings"

Expand Down Expand Up @@ -168,7 +170,7 @@ func (a *Auth) Update(args component.Arguments) error {
components = append(components, ext)
}

cTypeStr := strings.ReplaceAll(strings.ReplaceAll(a.opts.ID, ".", "_"), "/", "_")
cTypeStr := NormalizeType(a.opts.ID)

// Inform listeners that our handler changed.
a.opts.OnStateChange(Exports{
Expand All @@ -187,3 +189,19 @@ func (a *Auth) Update(args component.Arguments) error {
func (a *Auth) CurrentHealth() component.Health {
return a.sched.CurrentHealth()
}

func getHash(in string) string {
fnvHash := fnv.New32()
fnvHash.Write([]byte(in))
return fmt.Sprintf("%x", fnvHash.Sum(nil))
}

func NormalizeType(in string) string {
res := strings.ReplaceAll(strings.ReplaceAll(in, ".", "_"), "/", "_")

if len(res) > 63 {
res = res[:40] + getHash(res)
}

return res
}
30 changes: 30 additions & 0 deletions internal/component/otelcol/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auth_test

import (
"context"
"regexp"
"testing"
"time"

Expand Down Expand Up @@ -93,3 +94,32 @@ func (fa fakeAuthArgs) Extensions() map[otelcomponent.ID]otelextension.Extension
func (fa fakeAuthArgs) Exporters() map[otelcomponent.DataType]map[otelcomponent.ID]otelcomponent.Component {
return nil
}

func TestNormalizeType(t *testing.T) {
type tc struct {
input string
expected string
}
testcases := []tc{
{"foo", "foo"},
{"foo1", "foo1"},
{"fooBar", "fooBar"},

{"foo.bar", "foo_bar"},
{"foo/bar", "foo_bar"},
{"foo.bar/baz", "foo_bar_baz"},
{"foo/bar_baz", "foo_bar_baz"},

{"thisStringsConstructedSoThatItsLengthIsSetToBeAtSixtyThreeChars", "thisStringsConstructedSoThatItsLengthIsSetToBeAtSixtyThreeChars"},
{"whileThisOneHeresConstructedSoThatItsSizeIsEqualToSixtyFourChars", "whileThisOneHeresConstructedSoThatItsSiz2d7fa5d2"},
}

// https://github.com/open-telemetry/opentelemetry-collector/blob/e09b25f7d1b4090a9b7b73ef7d3c514592331554/component/config.go#L127-L131
var typeRegexp = regexp.MustCompile(`^[a-zA-Z][0-9a-zA-Z_]{0,62}$`)

for _, tt := range testcases {
actual := auth.NormalizeType(tt.input)
require.Equal(t, tt.expected, actual)
require.True(t, typeRegexp.MatchString(actual))
}
}
6 changes: 0 additions & 6 deletions internal/component/otelcol/receiver/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"os"
"regexp"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -117,12 +116,7 @@ func (c *Component) Update(newConfig component.Arguments) error {
gcInterval = 5 * time.Minute
)

cTypeStr := strings.ReplaceAll(strings.ReplaceAll(c.opts.ID, ".", "_"), "/", "_")

settings := otelreceiver.CreateSettings{

ID: otelcomponent.NewID(otelcomponent.MustNewType(cTypeStr)),

TelemetrySettings: otelcomponent.TelemetrySettings{
Logger: zapadapter.New(c.opts.Logger),

Expand Down

0 comments on commit d018e6e

Please sign in to comment.