Skip to content

Commit

Permalink
fix bungled merge
Browse files Browse the repository at this point in the history
Signed-off-by: alexgreenbank <[email protected]>
  • Loading branch information
alexgreenbank committed Sep 3, 2024
1 parent 00a3d64 commit dd4b846
Showing 1 changed file with 148 additions and 2 deletions.
150 changes: 148 additions & 2 deletions receiver/datadogreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ import (
"testing"

"github.com/DataDog/agent-payload/v5/gogen"
pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/trace"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver/receivertest"
"go.uber.org/multierr"
"google.golang.org/protobuf/proto"
)

func TestDatadogTracesReceiver_Lifecycle(t *testing.T) {
Expand Down Expand Up @@ -84,6 +87,12 @@ func TestDatadogServer(t *testing.T) {
expectCode: http.StatusBadRequest,
expectContent: "Unable to unmarshal reqs\n",
},
{
name: "Fake featuresdiscovery",
op: nil, // Content-length: 0.
expectCode: http.StatusBadRequest,
expectContent: "Fake featuresdiscovery\n",
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -108,6 +117,143 @@ func TestDatadogServer(t *testing.T) {
}
}

func TestDatadogInfoEndpoint(t *testing.T) {
for _, tc := range []struct {
name string
tracesConsumer consumer.Traces
metricsConsumer consumer.Metrics

expectContent string
}{
{
name: "No consumers",
tracesConsumer: nil,
metricsConsumer: nil,
expectContent: `{
"version": "datadogreceiver-otelcol-latest",
"endpoints": [
"/"
],
"client_drop_p0s": false,
"span_meta_structs": false,
"long_running_spans": false,
"config": null
}`,
},
{
name: "Traces consumer only",
tracesConsumer: consumertest.NewNop(),
metricsConsumer: nil,
expectContent: `{
"version": "datadogreceiver-otelcol-latest",
"endpoints": [
"/",
"/v0.3/traces",
"/v0.4/traces",
"/v0.5/traces",
"/v0.7/traces",
"/api/v0.2/traces"
],
"client_drop_p0s": false,
"span_meta_structs": false,
"long_running_spans": false,
"config": null
}`,
},
{
name: "Metrics consumer only",
tracesConsumer: nil,
metricsConsumer: consumertest.NewNop(),
expectContent: `{
"version": "datadogreceiver-otelcol-latest",
"endpoints": [
"/",
"/api/v1/series",
"/api/v2/series",
"/api/v1/check_run",
"/api/v1/sketches",
"/api/beta/sketches",
"/intake",
"/api/v1/distribution_points",
"/v0.6/stats"
],
"client_drop_p0s": false,
"span_meta_structs": false,
"long_running_spans": false,
"config": null
}`,
},
{
name: "Both consumers",
tracesConsumer: consumertest.NewNop(),
metricsConsumer: consumertest.NewNop(),
expectContent: `{
"version": "datadogreceiver-otelcol-latest",
"endpoints": [
"/",
"/v0.3/traces",
"/v0.4/traces",
"/v0.5/traces",
"/v0.7/traces",
"/api/v0.2/traces",
"/api/v1/series",
"/api/v2/series",
"/api/v1/check_run",
"/api/v1/sketches",
"/api/beta/sketches",
"/intake",
"/api/v1/distribution_points",
"/v0.6/stats"
],
"client_drop_p0s": false,
"span_meta_structs": false,
"long_running_spans": false,
"config": null
}`,
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

cfg := createDefaultConfig().(*Config)
cfg.Endpoint = "localhost:0" // Using a randomly assigned address

dd, err := newDataDogReceiver(
cfg,
receivertest.NewNopSettings(),
)
require.NoError(t, err, "Must not error when creating receiver")

dd.(*datadogReceiver).nextTracesConsumer = tc.tracesConsumer
dd.(*datadogReceiver).nextMetricsConsumer = tc.metricsConsumer

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")
})

req, err := http.NewRequest(
http.MethodPost,
fmt.Sprintf("http://%s/info", dd.(*datadogReceiver).address),
nil,
)
require.NoError(t, err, "Must not error when creating request")

resp, err := http.DefaultClient.Do(req)
require.NoError(t, err, "Must not error performing request")

body, err := io.ReadAll(resp.Body)
require.NoError(t, multierr.Combine(err, resp.Body.Close()), "Must not error when reading body")
require.Equal(t, tc.expectContent, string(body), "Expected response to be '%s', got %s", tc.expectContent, string(body))
require.Equal(t, http.StatusOK, resp.StatusCode)
})
}
}

func TestDatadogMetricsV1_EndToEnd(t *testing.T) {
cfg := createDefaultConfig().(*Config)
cfg.Endpoint = "localhost:0" // Using a randomly assigned address
Expand Down Expand Up @@ -163,7 +309,7 @@ func TestDatadogMetricsV1_EndToEnd(t *testing.T) {
assert.Equal(t, pmetric.MetricTypeSum, metric.Type())
assert.Equal(t, "system.load.1", metric.Name())
assert.Equal(t, pmetric.AggregationTemporalityDelta, metric.Sum().AggregationTemporality())
assert.Equal(t, false, metric.Sum().IsMonotonic())
assert.False(t, metric.Sum().IsMonotonic())
assert.Equal(t, pcommon.Timestamp(1636629071*1_000_000_000), metric.Sum().DataPoints().At(0).Timestamp())
assert.Equal(t, 0.7, metric.Sum().DataPoints().At(0).DoubleValue())
expectedEnvironment, _ := metric.Sum().DataPoints().At(0).Attributes().Get("environment")
Expand Down Expand Up @@ -241,7 +387,7 @@ func TestDatadogMetricsV2_EndToEnd(t *testing.T) {
assert.Equal(t, pmetric.MetricTypeSum, metric.Type())
assert.Equal(t, "system.load.1", metric.Name())
assert.Equal(t, pmetric.AggregationTemporalityDelta, metric.Sum().AggregationTemporality())
assert.Equal(t, false, metric.Sum().IsMonotonic())
assert.False(t, metric.Sum().IsMonotonic())
assert.Equal(t, pcommon.Timestamp(1636629071*1_000_000_000), metric.Sum().DataPoints().At(0).Timestamp())
assert.Equal(t, 1.5, metric.Sum().DataPoints().At(0).DoubleValue())
assert.Equal(t, pcommon.Timestamp(0), metric.Sum().DataPoints().At(0).StartTimestamp())
Expand Down

0 comments on commit dd4b846

Please sign in to comment.