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/datadog]: follow receiver contract #35975

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion receiver/datadogreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/DataDog/datadog-agent/pkg/trace v0.58.0
github.com/DataDog/datadog-api-client-go/v2 v2.31.0
github.com/DataDog/sketches-go v1.4.6
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.112.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics v0.112.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/sharedcomponent v0.112.0
github.com/stretchr/testify v1.9.0
Expand Down Expand Up @@ -72,7 +73,7 @@ require (
github.com/power-devops/perfstat v0.0.0-20220216144756-c35f1ee13d7c // indirect
github.com/rs/cors v1.11.1 // indirect
github.com/secure-systems-lab/go-securesystemslib v0.7.0 // indirect
github.com/shirou/gopsutil/v3 v3.24.4 // indirect
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
Expand Down Expand Up @@ -128,3 +129,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest
replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden

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

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

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

13 changes: 7 additions & 6 deletions receiver/datadogreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"go.opentelemetry.io/collector/receiver/receiverhelper"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/errorutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/datadogreceiver/internal/translator"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/datadogreceiver/internal/translator/header"
)
Expand Down Expand Up @@ -237,7 +238,7 @@ func (ddr *datadogReceiver) handleTraces(w http.ResponseWriter, req *http.Reques
spanCount = otelTraces.SpanCount()
err = ddr.nextTracesConsumer.ConsumeTraces(obsCtx, otelTraces)
if err != nil {
http.Error(w, "Trace consumer errored out", http.StatusInternalServerError)
errorutil.HTTPError(w, err)
ddr.params.Logger.Error("Trace consumer errored out", zap.Error(err))
return
}
Expand Down Expand Up @@ -277,7 +278,7 @@ func (ddr *datadogReceiver) handleV1Series(w http.ResponseWriter, req *http.Requ

err = ddr.nextMetricsConsumer.ConsumeMetrics(obsCtx, metrics)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
errorutil.HTTPError(w, err)
ddr.params.Logger.Error("metrics consumer errored out", zap.Error(err))
return
}
Expand Down Expand Up @@ -311,7 +312,7 @@ func (ddr *datadogReceiver) handleV2Series(w http.ResponseWriter, req *http.Requ

err = ddr.nextMetricsConsumer.ConsumeMetrics(obsCtx, metrics)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
errorutil.HTTPError(w, err)
ddr.params.Logger.Error("metrics consumer errored out", zap.Error(err))
return
}
Expand Down Expand Up @@ -355,7 +356,7 @@ func (ddr *datadogReceiver) handleCheckRun(w http.ResponseWriter, req *http.Requ

err = ddr.nextMetricsConsumer.ConsumeMetrics(obsCtx, metrics)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
errorutil.HTTPError(w, err)
ddr.params.Logger.Error("metrics consumer errored out", zap.Error(err))
return
}
Expand Down Expand Up @@ -386,7 +387,7 @@ func (ddr *datadogReceiver) handleSketches(w http.ResponseWriter, req *http.Requ

err = ddr.nextMetricsConsumer.ConsumeMetrics(obsCtx, metrics)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
errorutil.HTTPError(w, err)
ddr.params.Logger.Error("metrics consumer errored out", zap.Error(err))
return
}
Expand Down Expand Up @@ -455,7 +456,7 @@ func (ddr *datadogReceiver) handleStats(w http.ResponseWriter, req *http.Request
err = ddr.nextMetricsConsumer.ConsumeMetrics(obsCtx, metrics)
if err != nil {
ddr.params.Logger.Error("Metrics consumer errored out", zap.Error(err))
http.Error(w, "Metrics consumer errored out", http.StatusInternalServerError)
errorutil.HTTPError(w, err)
return
}

Expand Down
50 changes: 50 additions & 0 deletions receiver/datadogreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"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/pmetric"
Expand Down Expand Up @@ -116,6 +117,55 @@ func TestDatadogServer(t *testing.T) {
}
}

func TestDatadogResponse(t *testing.T) {
tests := []struct {
name string
err error
expectedStatus int
}{
{
name: "non-permanent error",
err: errors.New("non-permanenet error"),
expectedStatus: http.StatusServiceUnavailable,
},
{
name: "permanent error",
err: consumererror.NewPermanent(errors.New("non-permanenet error")),
expectedStatus: http.StatusBadRequest,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cfg := createDefaultConfig().(*Config)
cfg.Endpoint = "localhost:0" // Using a randomly assigned address
dd, err := newDataDogReceiver(
cfg,
receivertest.NewNopSettings(),
)
dd.(*datadogReceiver).nextTracesConsumer = consumertest.NewErr(tc.err)
VihasMakwana marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err, "Must not error when creating receiver")

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

require.NoError(t, dd.Start(ctx, componenttest.NewNopHost()))
t.Cleanup(func() {
require.NoError(t, dd.Shutdown(ctx), "Must not error shutting down")
})

apiPayload := pb.TracerPayload{}
var reqBytes []byte
bytez, _ := apiPayload.MarshalMsg(reqBytes)
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://%s/v0.7/traces", dd.(*datadogReceiver).address), io.NopCloser(bytes.NewReader(bytez)))
VihasMakwana marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err, "Must not error creating request")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err, "Must not error performing request")
require.Equal(t, tc.expectedStatus, resp.StatusCode)
})
}

}

func TestDatadogInfoEndpoint(t *testing.T) {
for _, tc := range []struct {
name string
Expand Down
Loading