Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow slashes in application name #137

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/flameql/flameql.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func IsTagKeyRuneAllowed(r rune) bool {
}

func IsAppNameRuneAllowed(r rune) bool {
return r == '-' || r == '.' || IsTagKeyRuneAllowed(r)
return r == '-' || r == '.' || r == '/' || IsTagKeyRuneAllowed(r)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we want to get rid of the internal/flameql and unify the way we handle labels. I propose to explore a way where we use the exact same parsing and validation as in the Push API and treat labels as Prometheus ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I'll create an issue to explore this further. I had to replicate this change in three places (Pyroscope, Alloy, and here), so it's definitely not the ideal approach 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I had already created it when I needed to duplicate the logic in Alloy https://github.com/grafana/pyroscope-squad/issues/257

}

func IsTagKeyReserved(k string) bool {
Expand Down
150 changes: 150 additions & 0 deletions internal/flameql/flameql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package flameql

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestParseKey(t *testing.T) {
tests := []struct {
name string
input string
expected *Key
wantErr bool
}{
{
name: "basic app name",
input: "simple-app",
expected: &Key{
labels: map[string]string{
"__name__": "simple-app",
},
},
},
{
name: "app name with slashes and tags",
input: "my/service/name{environment=prod,version=1.0}",
expected: &Key{
labels: map[string]string{
"__name__": "my/service/name",
"environment": "prod",
"version": "1.0",
},
},
},
{
name: "multiple slashes and special characters",
input: "app/service/v1.0-beta/component{region=us-west}",
expected: &Key{
labels: map[string]string{
"__name__": "app/service/v1.0-beta/component",
"region": "us-west",
},
},
},
{
name: "empty app name",
input: "{}",
wantErr: true,
},
{
name: "invalid characters in tag key",
input: "my/service/name{invalid@key=value}",
wantErr: true,
},
{
name: "whitespace handling",
input: "my/service/name{ tag1 = value1 , tag2 = value2 }",
expected: &Key{
labels: map[string]string{
"__name__": "my/service/name",
"tag1": "value1",
"tag2": "value2",
},
},
},
{
name: "dots in service name",
input: "my/service.name/v1.0{environment=prod}",
expected: &Key{
labels: map[string]string{
"__name__": "my/service.name/v1.0",
"environment": "prod",
},
},
},
{
name: "app name with slashes",
input: "my/service/name{}",
expected: &Key{
labels: map[string]string{
"__name__": "my/service/name",
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseKey(tt.input)

if tt.wantErr {
assert.Error(t, err)
return
}

require.NoError(t, err)
assert.Equal(t, tt.expected, got)
})
}
}

func TestKey_Normalized(t *testing.T) {
tests := []struct {
name string
key *Key
expected string
}{
{
name: "simple normalization",
key: &Key{
labels: map[string]string{
"__name__": "my/service/name",
},
},
expected: "my/service/name{}",
},
{
name: "normalization with tags",
key: &Key{
labels: map[string]string{
"__name__": "my/service/name",
"environment": "prod",
"version": "1.0",
},
},
expected: "my/service/name{environment=prod,version=1.0}",
},
{
name: "tags should be sorted",
key: &Key{
labels: map[string]string{
"__name__": "my/service/name",
"c": "3",
"b": "2",
"a": "1",
},
},
expected: "my/service/name{a=1,b=2,c=3}",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.key.Normalized()
assert.Equal(t, tt.expected, got)
})
}
}
Loading