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

[exporter/loki] Automatically maps severity value to "level" attribute #14560

Merged
merged 1 commit into from
Nov 9, 2022
Merged

[exporter/loki] Automatically maps severity value to "level" attribute #14560

merged 1 commit into from
Nov 9, 2022

Conversation

jtama
Copy link
Contributor

@jtama jtama commented Sep 28, 2022

Description: Loki follows severity using the "level" attribute value. This allow automatic mapping from the log record "severity" value to the "attribute.level" value

Link to tracking Issue: Closes #14313

Testing: Test added in the pkg/translator/loki/convert_test.go#TestConvertAttributesAndMerge
Documentation: Not done for the moment

@jtama jtama requested a review from a team September 28, 2022 08:17
@jtama jtama requested a review from jpkrohling as a code owner September 28, 2022 08:17
@djaglowski djaglowski changed the title Automatically maps severity value to "level" attribute [exporter/loki] Automatically maps severity value to "level" attribute Sep 28, 2022
@djaglowski djaglowski added the exporter/loki Loki Exporter label Sep 28, 2022
pkg/translator/loki/convert.go Outdated Show resolved Hide resolved
pkg/translator/loki/convert.go Outdated Show resolved Hide resolved
Copy link
Member

@kovrus kovrus left a comment

Choose a reason for hiding this comment

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

Can you maybe add one sentence in the readme of the Loki exporter mentioning this automatic mapping?

pkg/translator/loki/logs_to_loki.go Outdated Show resolved Hide resolved
pkg/translator/loki/logs_to_loki.go Show resolved Hide resolved
pkg/translator/loki/logs_to_loki.go Outdated Show resolved Hide resolved
@@ -59,7 +58,15 @@ func convertAttributesToLabels(attributes pcommon.Map, attrsToSelect pcommon.Val
attr = strings.TrimSpace(attr)
av, ok := attributes.Get(attr) // do we need to trim this?
if ok {
out[model.LabelName(attr)] = model.LabelValue(av.AsString())
if attr == levelAttributeName {
Copy link
Member

Choose a reason for hiding this comment

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

lets revert this changes and do the conversion in logs_to_loki#handleSeverity method, smth like

                level, _ := severityNumberToLevel[log.SeverityNumber().String()]
                ...
		log.Attributes().PutStr(levelAttributeName, level)

Copy link
Member

Choose a reason for hiding this comment

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

linting error: coz of _

Copy link
Member

@kovrus kovrus left a comment

Choose a reason for hiding this comment

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

lgtm

@kovrus
Copy link
Member

kovrus commented Oct 6, 2022

@jpkrohling do you want to take another look at the PR?

@jpkrohling
Copy link
Member

I'll take another look.

@jtama
Copy link
Contributor Author

jtama commented Oct 18, 2022

I'll take another look.

@jpkrohling Does it looks good to you ?

exporter/lokiexporter/README.md Outdated Show resolved Hide resolved
pkg/translator/loki/logs_to_loki.go Show resolved Hide resolved
@jpkrohling
Copy link
Member

jpkrohling commented Oct 20, 2022

Could you please fix the conflicts? This PR will then be ready to be merged. Thank you for your patience during this PR!

@jpkrohling
Copy link
Member

If you prefer to have the simple version of the code (the original one you wrote), that's fine as well. We can optimize it later if we find it to be a problem.

@jtama
Copy link
Contributor Author

jtama commented Oct 20, 2022

If you prefer to have the simple version of the code (the original one you wrote), that's fine as well. We can optimize it later if we find it to be a problem.

Done. Also backported the code to LogsToLokiRequests

@jpkrohling
Copy link
Member

The tests seem to be failing:

=== RUN   TestLogsToLokiRequestWithoutTenant/with_severity_to_label
    logs_to_loki_test.go:329: 
        	Error Trace:	/home/runner/work/opentelemetry-collector-contrib/opentelemetry-collector-contrib/pkg/translator/loki/logs_to_loki_test.go:329
        	Error:      	Not equal: 
        	            	expected: "{exporter=\"OTLP\", level=\"DEBUG4\"}"
        	            	actual  : "{exporter=\"OTLP\"}"
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-{exporter="OTLP", level="DEBUG4"}
        	            	+{exporter="OTLP"}
        	Test:       	TestLogsToLokiRequestWithoutTenant/with_severity_to_label
    logs_to_loki_test.go:333: 
        	Error Trace:	/home/runner/work/opentelemetry-collector-contrib/opentelemetry-collector-contrib/pkg/translator/loki/logs_to_loki_test.go:333
        	Error:      	Not equal: 
        	            	expected: "{\"traceid\":\"01020304000000000000000000000000\"}"
        	            	actual  : "{\"traceid\":\"01000000000000000000000000000000\"}"
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-{"traceid":"01020304000000000000000000000000"}
        	            	+{"traceid":"01000000000000000000000000000000"}
        	Test:       	TestLogsToLokiRequestWithoutTenant/with_severity_to_label
    logs_to_loki_test.go:333: 
        	Error Trace:	/home/runner/work/opentelemetry-collector-contrib/opentelemetry-collector-contrib/pkg/translator/loki/logs_to_loki_test.go:333
        	Error:      	Not equal: 
        	            	expected: "{\"traceid\":\"01020304050000000000000000000000\"}"
        	            	actual  : "{\"traceid\":\"02000000000000000000000000000000\"}"
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-{"traceid":"01020304050000000000000000000000"}
        	            	+{"traceid":"02000000000000000000000000000000"}
        	Test:       	TestLogsToLokiRequestWithoutTenant/with_severity_to_label
    logs_to_loki_test.go:333: 
        	Error Trace:	/home/runner/work/opentelemetry-collector-contrib/opentelemetry-collector-contrib/pkg/translator/loki/logs_to_loki_test.go:333
        	Error:      	Not equal: 
        	            	expected: "{\"traceid\":\"01020304050600000000000000000000\"}"
        	            	actual  : "{\"traceid\":\"03000000000000000000000000000000\"}"
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-{"traceid":"01020304050600000000000000000000"}
        	            	+{"traceid":"03000000000000000000000000000000"}
        	Test:       	TestLogsToLokiRequestWithoutTenant/with_severity_to_label
=== RUN   TestLogsToLokiRequestWithoutTenant/with_severity,_already_existing_level
--- FAIL: TestLogsToLokiRequestWithoutTenant (0.00s)
    --- PASS: TestLogsToLokiRequestWithoutTenant/with_attribute_to_label_and_regular_attribute (0.00s)
    --- PASS: TestLogsToLokiRequestWithoutTenant/with_resource_to_label_and_regular_resource (0.00s)
    --- FAIL: TestLogsToLokiRequestWithoutTenant/with_severity_to_label (0.00s)
    --- FAIL: TestLogsToLokiRequestWithoutTenant/with_severity,_already_existing_level (0.00s)
panic: runtime error: index out of range [1] with length 1 [recovered]
	panic: runtime error: index out of range [1] with length 1

goroutine 51 [running]:
testing.tRunner.func1.2({0x12af040, 0xc0000e8dc8})
	/opt/hostedtoolcache/go/1.18.7/x64/src/testing/testing.go:1389 +0x366
testing.tRunner.func1()
	/opt/hostedtoolcache/go/1.18.7/x64/src/testing/testing.go:1392 +0x5d2
panic({0x12af040, 0xc0000e8dc8})
	/opt/hostedtoolcache/go/1.18.7/x64/src/runtime/panic.go:844 +0x258
go.opentelemetry.io/collector/pdata/plog.LogRecordSlice.At(...)
	/home/runner/go/pkg/mod/go.opentelemetry.io/collector/pdata@v0.62.2-0.20221017171445-6313054b642c/plog/generated_logs.go:469
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/loki.TestLogsToLokiRequestWithoutTenant.func1(0x0?)
	/home/runner/work/opentelemetry-collector-contrib/opentelemetry-collector-contrib/pkg/translator/loki/logs_to_loki_test.go:298 +0x14a9
testing.tRunner(0xc0002cd040, 0xc0002da8e0)
	/opt/hostedtoolcache/go/1.18.7/x64/src/testing/testing.go:1439 +0x214
created by testing.(*T).Run
	/opt/hostedtoolcache/go/1.18.7/x64/src/testing/testing.go:1486 +0x725
FAIL	github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/loki	0.062s
FAIL
make[2]: *** [../../../Makefile.Common:57: test] Error 1

@jtama
Copy link
Contributor Author

jtama commented Nov 4, 2022

@jpkrohling, @kovrus Is it good for you ?

@jtama
Copy link
Contributor Author

jtama commented Nov 9, 2022

Why is this blocked ? It would be much simpler for me to have this feature ;)

@kovrus
Copy link
Member

kovrus commented Nov 9, 2022

@jtama I think it is good to be merged.

@jpkrohling can you please merge it?

@djaglowski djaglowski merged commit e3fccb7 into open-telemetry:main Nov 9, 2022
shalper2 pushed a commit to shalper2/opentelemetry-collector-contrib that referenced this pull request Dec 6, 2022
@plantfansam plantfansam mentioned this pull request Jul 21, 2023
@lieberlois
Copy link

@jtama Is there any guidance on how to extract the severity field into a log attribute using the OTEL Collector?

@jtama
Copy link
Contributor Author

jtama commented Nov 12, 2024

Hi,
I just saw your comment on the grafana project. Do you want to achieve the same only with configuration ? I don't know if this is even possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exporter/loki Loki Exporter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[exporter/loki] Make it easier to map OpenTelemetry Severity to Loki level label
5 participants