Skip to content

Commit

Permalink
[receiver/cloudflare]: follow receiver contract for cloudflare (open-…
Browse files Browse the repository at this point in the history
…telemetry#35642)

#### Description
Follow [the
contract](https://github.com/open-telemetry/opentelemetry-collector/blob/df3c9e38a80ccc3b14705462be2e2e51c628a3b3/receiver/doc.go#L10)
for cloudflare receiver
<!-- Issue number (e.g. open-telemetry#1234) or full URL to issue, if applicable. -->
#### Link to tracking issue
Related
open-telemetry#5909

<!--Describe what testing was performed and which tests were added.-->
#### Testing

Added
  • Loading branch information
VihasMakwana authored and sbylica-splunk committed Dec 17, 2024
1 parent d6505c4 commit 088bb55
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
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

0 comments on commit 088bb55

Please sign in to comment.