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

[receiver/cloudflare]: follow receiver contract for cloudflare #35642

Merged
merged 6 commits into from
Oct 16, 2024
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
27 changes: 27 additions & 0 deletions .chloggen/cloudflare-receiver-contract.yaml
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: enhancement

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Respond 503 on non-permanent and 400 on permanent errors

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [35642]

# (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: []
5 changes: 4 additions & 1 deletion receiver/cloudflarereceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22.0

require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.111.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.111.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden v0.111.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.111.0
github.com/stretchr/testify v1.9.0
Expand Down Expand Up @@ -55,7 +56,7 @@ require (
go.opentelemetry.io/otel/trace v1.30.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/text v0.19.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd // indirect
google.golang.org/grpc v1.67.1 // indirect
google.golang.org/protobuf v1.35.1 // indirect
Expand All @@ -74,3 +75,5 @@ retract (
)

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

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal => ../../internal/coreinternal
4 changes: 2 additions & 2 deletions receiver/cloudflarereceiver/go.sum

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

3 changes: 2 additions & 1 deletion receiver/cloudflarereceiver/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
rcvr "go.opentelemetry.io/collector/receiver"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/errorutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/cloudflarereceiver/internal/metadata"
)

Expand Down Expand Up @@ -185,7 +186,7 @@ func (l *logsReceiver) handleRequest(rw http.ResponseWriter, req *http.Request)
}

if err := l.consumer.ConsumeLogs(req.Context(), l.processLogs(pcommon.NewTimestampFromTime(time.Now()), logs)); err != nil {
rw.WriteHeader(http.StatusInternalServerError)
errorutil.HTTPError(rw, err)
l.logger.Error("Failed to consumer alert as log", zap.Error(err))
return
}
Expand Down
23 changes: 22 additions & 1 deletion receiver/cloudflarereceiver/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
Expand Down Expand Up @@ -191,6 +192,7 @@ func TestHandleRequest(t *testing.T) {
expectedStatusCode int
logExpected bool
consumerFailure bool
permanentFailure bool // indicates a permanent error
}{
{
name: "No secret provided",
Expand Down Expand Up @@ -229,7 +231,22 @@ func TestHandleRequest(t *testing.T) {
},
logExpected: false,
consumerFailure: true,
expectedStatusCode: http.StatusInternalServerError,
expectedStatusCode: http.StatusServiceUnavailable,
},
{
name: "Consumer fails - permanent error",
request: &http.Request{
Method: "POST",
URL: &url.URL{},
Body: io.NopCloser(bytes.NewBufferString(`{"ClientIP": "127.0.0.1"}`)),
Header: map[string][]string{
textproto.CanonicalMIMEHeaderKey(secretHeaderName): {"abc123"},
},
},
logExpected: false,
consumerFailure: true,
permanentFailure: true,
expectedStatusCode: http.StatusBadRequest,
},
{
name: "Request succeeds",
Expand Down Expand Up @@ -298,6 +315,10 @@ func TestHandleRequest(t *testing.T) {
var consumer consumer.Logs
if tc.consumerFailure {
consumer = consumertest.NewErr(errors.New("consumer failed"))
if tc.permanentFailure {
consumer = consumertest.NewErr(consumererror.NewPermanent(errors.New("consumer failed")))

}
} else {
consumer = &consumertest.LogsSink{}
}
Expand Down