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

[translator/loki] Convert dots to underscores in labels names #19373

Merged
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: translator/loki

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Normalize label names so they follow the Prometheus label names standard

# One or more tracking issues related to the change
issues: [14113]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: |
Loki doesn't support label names containing dots. |
Users had to convert dot-separated attributes into underscore-separated attributes before promoting them to Loki labels. |
From now on users can drop relabeling from their configuration. All dots in label names will be replaced with underscores automatically before sending request to Loki.
21 changes: 6 additions & 15 deletions exporter/lokiexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,20 @@ attribute as label and the `resource` processor to give a hint to the Loki expor
processors:
attributes:
actions:
- action: insert
key: event_domain
from_attribute: event.domain
- action: insert
key: loki.attribute.labels
value: event_domain
value: event.domain

resource:
attributes:
- action: insert
key: service_name
from_attribute: service.name
- action: insert
key: loki.resource.labels
value: service_name
value: service.name
```

Currently, Loki does not support labels with dots.
Thats why to add Loki label based on `event.domain` OTLP attribute we need to specify two actions. The first one inserts a new attribute `event_domain` from the OTLP attribute `event.domain`. The second one is a hint for Loki, specifying that the `event_domain` attribute should be placed as a Loki label.
The same approach is applicable to placing Loki labels from resource attribute `service.name`.
Currently, Loki does not support label names with dots.
That's why lokiexporter normalizes label names to follow Prometheus label names standard before sending requests to Loki.
Copy link
Member

Choose a reason for hiding this comment

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

We can point to pkg/translator/prometheus/README.md#Labels for more information on labels normalization.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added one more sentence with a link to pkg/translator/prometheus/README.md#Labels

More information on label normalization could be found [here](../../pkg/translator/prometheus/README.md#Labels)

Default labels:
- `job=service.namespace/service.name`
Expand Down Expand Up @@ -98,10 +92,7 @@ processors:
attributes:
- action: insert
key: loki.tenant
value: host_name
- action: insert
key: host_name
from_attribute: host.name
value: host.name
```

In this case the value of the `host.name` resource attribute is used to group logs
Expand Down
3 changes: 3 additions & 0 deletions exporter/lokiexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus v0.74.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rs/cors v1.8.3 // indirect
go.opencensus.io v0.24.0 // indirect
Expand All @@ -65,4 +66,6 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/corei

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/loki => ../../pkg/translator/loki

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus => ../../pkg/translator/prometheus

retract v0.65.0
12 changes: 6 additions & 6 deletions exporter/lokiexporter/next_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestPushLogData(t *testing.T) {
hints: map[string]interface{}{
"loki.attribute.labels": "host.name",
},
expectedLabel: `{exporter="OTLP", host.name="guarana"}`,
expectedLabel: `{exporter="OTLP", host_name="guarana"}`,
expectedLine: `{"traceid":"01020304000000000000000000000000","attributes":{"http.status":200}}`,
},
{
Expand All @@ -63,7 +63,7 @@ func TestPushLogData(t *testing.T) {
hints: map[string]interface{}{
"loki.resource.labels": "host.name",
},
expectedLabel: `{exporter="OTLP", host.name="guarana"}`,
expectedLabel: `{exporter="OTLP", host_name="guarana"}`,
expectedLine: `{"traceid":"01020304000000000000000000000000","resources":{"region.az":"eu-west-1a"}}`,
},
}
Expand Down Expand Up @@ -169,11 +169,11 @@ func TestLogsToLokiRequestWithGroupingByTenant(t *testing.T) {
label string
}{
"1": {
label: `{exporter="OTLP", tenant.id="1"}`,
label: `{exporter="OTLP", tenant_id="1"}`,
line: `{"attributes":{"http.status":200}}`,
},
"2": {
label: `{exporter="OTLP", tenant.id="2"}`,
label: `{exporter="OTLP", tenant_id="2"}`,
line: `{"attributes":{"http.status":200}}`,
},
},
Expand Down Expand Up @@ -233,11 +233,11 @@ func TestLogsToLokiRequestWithGroupingByTenant(t *testing.T) {
label string
}{
"1": {
label: `{exporter="OTLP", tenant.id="1"}`,
label: `{exporter="OTLP", tenant_id="1"}`,
line: `{"attributes":{"http.status":200}}`,
},
"2": {
label: `{exporter="OTLP", tenant.id="2"}`,
label: `{exporter="OTLP", tenant_id="2"}`,
line: `{"attributes":{"http.status":200}}`,
},
},
Expand Down
6 changes: 6 additions & 0 deletions pkg/translator/loki/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/go-logfmt/logfmt v0.5.1
github.com/grafana/loki/pkg/push v0.0.0-20230127072203-4e8cc8d71928
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.74.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus v0.74.0
github.com/prometheus/common v0.42.0
github.com/stretchr/testify v1.8.2
go.opentelemetry.io/collector/pdata v1.0.0-rc8
Expand All @@ -21,6 +22,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
go.opentelemetry.io/collector/featuregate v0.74.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
Expand All @@ -31,6 +33,10 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus => ../../../pkg/translator/prometheus

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal => ../../../internal/coreinternal

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../../internal/common

retract v0.65.0
2 changes: 2 additions & 0 deletions pkg/translator/loki/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion pkg/translator/loki/logs_to_loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/prometheus/common/model"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"

prometheustranslator "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus"
)

type PushRequest struct {
Expand Down Expand Up @@ -90,8 +92,16 @@ func LogsToLokiRequests(ld plog.Logs) map[string]PushRequest {

group.report.NumSubmitted++

processed := model.LabelSet{}
for label := range entry.Labels {
// Loki doesn't support dots in label names
// labelName is normalized label name to follow Prometheus label names standard
labelName := prometheustranslator.NormalizeLabel(string(label))
processed[model.LabelName(labelName)] = entry.Labels[label]
}

// create the stream name based on the labels
labels := entry.Labels.String()
labels := processed.String()
if stream, ok := group.streams[labels]; ok {
stream.Entries = append(stream.Entries, *entry.Entry)
continue
Expand Down
18 changes: 9 additions & 9 deletions pkg/translator/loki/logs_to_loki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestLogsToLokiRequestWithGroupingByTenant(t *testing.T) {
PushRequest: &push.PushRequest{
Streams: []push.Stream{
{
Labels: `{exporter="OTLP", tenant.id="1"}`,
Labels: `{exporter="OTLP", tenant_id="1"}`,
Entries: []push.Entry{
{
Line: `{"attributes":{"http.status":200}}`,
Expand All @@ -71,7 +71,7 @@ func TestLogsToLokiRequestWithGroupingByTenant(t *testing.T) {
PushRequest: &push.PushRequest{
Streams: []push.Stream{
{
Labels: `{exporter="OTLP", tenant.id="2"}`,
Labels: `{exporter="OTLP", tenant_id="2"}`,
Entries: []push.Entry{
{
Line: `{"attributes":{"http.status":200}}`,
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestLogsToLokiRequestWithGroupingByTenant(t *testing.T) {
PushRequest: &push.PushRequest{
Streams: []push.Stream{
{
Labels: `{exporter="OTLP", tenant.id="11"}`,
Labels: `{exporter="OTLP", tenant_id="11"}`,
Entries: []push.Entry{
{
Line: `{"attributes":{"http.status":200}}`,
Expand All @@ -123,7 +123,7 @@ func TestLogsToLokiRequestWithGroupingByTenant(t *testing.T) {
PushRequest: &push.PushRequest{
Streams: []push.Stream{
{
Labels: `{exporter="OTLP", tenant.id="12"}`,
Labels: `{exporter="OTLP", tenant_id="12"}`,
Entries: []push.Entry{
{
Line: `{"attributes":{"http.status":200}}`,
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestLogsToLokiRequestWithGroupingByTenant(t *testing.T) {
PushRequest: &push.PushRequest{
Streams: []push.Stream{
{
Labels: `{exporter="OTLP", tenant.id="21"}`,
Labels: `{exporter="OTLP", tenant_id="21"}`,
Entries: []push.Entry{
{
Line: `{"attributes":{"http.status":200}}`,
Expand All @@ -209,7 +209,7 @@ func TestLogsToLokiRequestWithGroupingByTenant(t *testing.T) {
PushRequest: &push.PushRequest{
Streams: []push.Stream{
{
Labels: `{exporter="OTLP", tenant.id="22"}`,
Labels: `{exporter="OTLP", tenant_id="22"}`,
Entries: []push.Entry{
{
Line: `{"attributes":{"http.status":200}}`,
Expand Down Expand Up @@ -265,7 +265,7 @@ func TestLogsToLokiRequestWithoutTenant(t *testing.T) {
hints: map[string]interface{}{
hintAttributes: "host.name",
},
expectedLabel: `{exporter="OTLP", host.name="guarana"}`,
expectedLabel: `{exporter="OTLP", host_name="guarana"}`,
expectedLines: []string{
`{"traceid":"01000000000000000000000000000000","attributes":{"http.status":200}}`,
`{"traceid":"02000000000000000000000000000000","attributes":{"http.status":200}}`,
Expand All @@ -281,7 +281,7 @@ func TestLogsToLokiRequestWithoutTenant(t *testing.T) {
hints: map[string]interface{}{
hintResources: "host.name",
},
expectedLabel: `{exporter="OTLP", host.name="guarana"}`,
expectedLabel: `{exporter="OTLP", host_name="guarana"}`,
expectedLines: []string{
`{"traceid":"01000000000000000000000000000000","resources":{"region.az":"eu-west-1a"}}`,
`{"traceid":"02000000000000000000000000000000","resources":{"region.az":"eu-west-1a"}}`,
Expand All @@ -298,7 +298,7 @@ func TestLogsToLokiRequestWithoutTenant(t *testing.T) {
hintAttributes: "host.name",
hintFormat: formatLogfmt,
},
expectedLabel: `{exporter="OTLP", host.name="guarana"}`,
expectedLabel: `{exporter="OTLP", host_name="guarana"}`,
expectedLines: []string{
`traceID=01000000000000000000000000000000 attribute_http.status=200`,
`traceID=02000000000000000000000000000000 attribute_http.status=200`,
Expand Down