forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle OTLPJSON unmarshal error (open-telemetry#34784)
**Description:** <Describe what has changed.> <!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> Handles the error that the Unmarshaler can return in case an invalid OTLPJSON is provided, this avoids sending a nil signal to the corresponding consumer. The fix logs the error and continues the execution: ```go t, err := tracesUnmarshaler.UnmarshalTraces([]byte(token.AsString())) if err != nil { c.logger.Error("could extract traces from otlp json", zap.Error(err)) continue } ``` **Link to tracking Issue:** <Issue number if applicable> open-telemetry#34782 **Testing:** <Describe what testing was performed and which tests were added.> Factory tests moved to connector tests using the `golden` + compare testing packages. Testdata includes a file with an invalid json for each signal. **Documentation:** <Describe the documentation added.> NA
- Loading branch information
Showing
18 changed files
with
406 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: 'bug_fix' | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: otlpjsonconnector | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Handle OTLPJSON unmarshal error | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [34782] | ||
|
||
# (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: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package otlpjsonconnector | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/component/componenttest" | ||
"go.opentelemetry.io/collector/connector/connectortest" | ||
"go.opentelemetry.io/collector/consumer/consumertest" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/plogtest" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/ptracetest" | ||
) | ||
|
||
func TestLogsToLogs2(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
inputFile string | ||
expectedFile string | ||
expectedLogs int | ||
}{ | ||
{ | ||
name: "correct log metric", | ||
inputFile: "input-log.yaml", | ||
expectedFile: "output-log.yaml", | ||
expectedLogs: 1, | ||
}, | ||
{ | ||
name: "invalid log", | ||
inputFile: "input-invalid-log.yaml", | ||
expectedLogs: 0, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
factory := NewFactory() | ||
sink := &consumertest.LogsSink{} | ||
conn, err := factory.CreateLogsToLogs(context.Background(), | ||
|
||
connectortest.NewNopSettings(), createDefaultConfig(), sink) | ||
require.NoError(t, err) | ||
require.NotNil(t, conn) | ||
assert.False(t, conn.Capabilities().MutatesData) | ||
|
||
require.NoError(t, conn.Start(context.Background(), componenttest.NewNopHost())) | ||
defer func() { | ||
assert.NoError(t, conn.Shutdown(context.Background())) | ||
}() | ||
|
||
testLogs, err := golden.ReadLogs(filepath.Join("testdata", "logsToLogs", tc.inputFile)) | ||
assert.NoError(t, err) | ||
assert.NoError(t, conn.ConsumeLogs(context.Background(), testLogs)) | ||
|
||
allLogs := sink.AllLogs() | ||
assert.Len(t, allLogs, tc.expectedLogs) | ||
|
||
if tc.expectedLogs > 0 { | ||
// golden.WriteLogs(t, filepath.Join("testdata", "logsToLogs", tc.expectedFile), allLogs[0]) | ||
expected, err := golden.ReadLogs(filepath.Join("testdata", "logsToLogs", tc.expectedFile)) | ||
assert.NoError(t, err) | ||
assert.NoError(t, plogtest.CompareLogs(expected, allLogs[0])) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestLogsToMetrics(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
inputFile string | ||
expectedFile string | ||
expectedMetrics int | ||
}{ | ||
{ | ||
name: "correct log metric", | ||
inputFile: "input-metric.yaml", | ||
expectedFile: "output-metric.yaml", | ||
expectedMetrics: 1, | ||
}, | ||
{ | ||
name: "invalid metric", | ||
inputFile: "input-invalid-metric.yaml", | ||
expectedMetrics: 0, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
factory := NewFactory() | ||
sink := &consumertest.MetricsSink{} | ||
conn, err := factory.CreateLogsToMetrics(context.Background(), | ||
|
||
connectortest.NewNopSettings(), createDefaultConfig(), sink) | ||
require.NoError(t, err) | ||
require.NotNil(t, conn) | ||
assert.False(t, conn.Capabilities().MutatesData) | ||
|
||
require.NoError(t, conn.Start(context.Background(), componenttest.NewNopHost())) | ||
defer func() { | ||
assert.NoError(t, conn.Shutdown(context.Background())) | ||
}() | ||
|
||
testLogs, err := golden.ReadLogs(filepath.Join("testdata", "logsToMetrics", tc.inputFile)) | ||
assert.NoError(t, err) | ||
assert.NoError(t, conn.ConsumeLogs(context.Background(), testLogs)) | ||
|
||
allMetrics := sink.AllMetrics() | ||
assert.Len(t, allMetrics, tc.expectedMetrics) | ||
|
||
if tc.expectedMetrics > 0 { | ||
// golden.WriteMetrics(t, filepath.Join("testdata", "logsToMetrics", tc.expectedFile), allMetrics[0]) | ||
expected, err := golden.ReadMetrics(filepath.Join("testdata", "logsToMetrics", tc.expectedFile)) | ||
assert.NoError(t, err) | ||
assert.NoError(t, pmetrictest.CompareMetrics(expected, allMetrics[0])) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestLogsToTraces(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
inputFile string | ||
expectedFile string | ||
expectedTraces int | ||
}{ | ||
{ | ||
name: "correct log trace", | ||
inputFile: "input-trace.yaml", | ||
expectedFile: "output-trace.yaml", | ||
expectedTraces: 1, | ||
}, | ||
{ | ||
name: "invalid trace", | ||
inputFile: "input-invalid-trace.yaml", | ||
expectedTraces: 0, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
factory := NewFactory() | ||
sink := &consumertest.TracesSink{} | ||
conn, err := factory.CreateLogsToTraces(context.Background(), | ||
|
||
connectortest.NewNopSettings(), createDefaultConfig(), sink) | ||
require.NoError(t, err) | ||
require.NotNil(t, conn) | ||
assert.False(t, conn.Capabilities().MutatesData) | ||
|
||
require.NoError(t, conn.Start(context.Background(), componenttest.NewNopHost())) | ||
defer func() { | ||
assert.NoError(t, conn.Shutdown(context.Background())) | ||
}() | ||
|
||
testLogs, err := golden.ReadLogs(filepath.Join("testdata", "logsToTraces", tc.inputFile)) | ||
assert.NoError(t, err) | ||
assert.NoError(t, conn.ConsumeLogs(context.Background(), testLogs)) | ||
|
||
allMetrics := sink.AllTraces() | ||
assert.Len(t, allMetrics, tc.expectedTraces) | ||
|
||
if tc.expectedTraces > 0 { | ||
// golden.WriteTraces(t, filepath.Join("testdata", "logsToTraces", tc.expectedFile), allMetrics[0]) | ||
expected, err := golden.ReadTraces(filepath.Join("testdata", "logsToTraces", tc.expectedFile)) | ||
assert.NoError(t, err) | ||
assert.NoError(t, ptracetest.CompareTraces(expected, allMetrics[0])) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.