Skip to content

[chore] [receiver/discovery] Simplify endpoint evaluation tests #4737

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

Merged
merged 1 commit into from
Apr 26, 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
64 changes: 63 additions & 1 deletion internal/receiver/discoveryreceiver/endpoint_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/plogtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -378,7 +379,7 @@ func FuzzEndpointToPlogs(f *testing.F) {
}

var (
t0 = time.Unix(0, 0)
t0 = time.Unix(0, 0).UTC()

podEndpoint = observer.Endpoint{
ID: observer.EndpointID("pod.endpoint.id"),
Expand Down Expand Up @@ -694,3 +695,64 @@ func (f *fakeObservable) ListAndWatch(notify observer.Notify) {
}

func (f *fakeObservable) Unsubscribe(observer.Notify) {}

func TestEntityStateEvents(t *testing.T) {
logger := zap.NewNop()
cfg := createDefaultConfig().(*Config)
cfg.Receivers = map[component.ID]ReceiverEntry{
component.MustNewIDWithName("fake_receiver", ""): {
Rule: mustNewRule(`type == "port" && pod.name == "pod.name" && port == 1`),
},
}

cStore := newCorrelationStore(logger, cfg.CorrelationTTL)
cStore.UpdateAttrs(portEndpoint.ID, map[string]string{
"attr1": "val1",
"attr2": "val2",
})

events, failed, err := entityStateEvents(component.MustNewIDWithName("observer_type", "observer.name"),
[]observer.Endpoint{portEndpoint}, cStore, t0)
require.NoError(t, err)
require.Zero(t, failed)
require.Equal(t, 1, events.Len())

event := events.At(0)
assert.Equal(t, experimentalmetricmetadata.EventTypeState, event.EventType())
assert.Equal(t, t0, event.Timestamp().AsTime())
assert.Equal(t, map[string]any{discovery.EndpointIDAttr: string(portEndpoint.ID)}, event.ID().AsRaw())
assert.Equal(t, map[string]any{
observerNameAttr: "observer.name",
observerTypeAttr: "observer_type",
"endpoint": "port.target",
"name": "port.name",
"port": int64(1),
"pod": map[string]any{
"annotations": map[string]any{
"annotation.one": "value.one",
"annotation.two": "value.two",
},
"labels": map[string]any{
"label.one": "value.one",
"label.two": "value.two",
},
"name": "pod.name",
"namespace": "namespace",
"uid": "uid",
},
"transport": "transport",
"type": "port",
"attr1": "val1",
"attr2": "val2",
}, event.EntityStateDetails().Attributes().AsRaw())
}

func TestEntityDeleteEvents(t *testing.T) {
events := entityDeleteEvents([]observer.Endpoint{portEndpoint}, t0)
require.Equal(t, 1, events.Len())

event := events.At(0)
assert.Equal(t, experimentalmetricmetadata.EventTypeDelete, event.EventType())
assert.Equal(t, t0, event.Timestamp().AsTime())
assert.Equal(t, map[string]any{discovery.EndpointIDAttr: string(portEndpoint.ID)}, event.ID().AsRaw())
}
57 changes: 16 additions & 41 deletions internal/receiver/discoveryreceiver/metric_evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ func TestMetricEvaluation(t *testing.T) {
emitCh := cStore.EmitCh()
emitWG := sync.WaitGroup{}
emitWG.Add(1)
var corr correlation
go func() {
corr = <-emitCh
<-emitCh
emitWG.Done()
}()

cStore.UpdateEndpoint(observer.Endpoint{ID: "endpoint.id"}, receiverID, observerID)
endpointID := observer.EndpointID("endpoint.id")
cStore.UpdateEndpoint(observer.Endpoint{ID: endpointID}, receiverID, observerID)

me := newMetricEvaluator(logger, cfg, cStore)

Expand Down Expand Up @@ -126,44 +126,19 @@ func TestMetricEvaluation(t *testing.T) {
// wait for the emit channel to be processed
emitWG.Wait()

entityEvents, numFailed, err := entityStateEvents(corr.observerID,
[]observer.Endpoint{corr.endpoint}, cStore, time.Now())
require.NoError(t, err)
require.Equal(t, 0, numFailed)
emitted := entityEvents.ConvertAndMoveToLogs()
rl := emitted.ResourceLogs().At(0)
require.Equal(t, 0, rl.Resource().Attributes().Len())

sLogs := rl.ScopeLogs()
require.Equal(t, 1, sLogs.Len())
sl := sLogs.At(0)
lrs := sl.LogRecords()
require.Equal(t, 1, lrs.Len())
lr := sl.LogRecords().At(0)

lrAttrs := lr.Attributes()
require.Equal(t, map[string]any{
discovery.OtelEntityIDAttr: map[string]any{
"discovery.endpoint.id": "endpoint.id",
},
discovery.OtelEntityEventTypeAttr: discovery.OtelEntityEventTypeState,
discovery.OtelEntityAttributesAttr: map[string]any{
"discovery.event.type": "metric.match",
"discovery.observer.id": "an_observer/observer.name",
"discovery.receiver.name": "receiver.name",
"discovery.receiver.rule": "a.rule",
"discovery.receiver.type": "a_receiver",
"discovery.status": string(status),
"discovery.message": "desired body content",
"metric.name": "desired.name",
"one": "one.value",
"two": "two.value",
"extra_attr": "target_resource",
"discovery.observer.name": "observer.name",
"discovery.observer.type": "an_observer",
"endpoint": "",
},
}, lrAttrs.AsRaw())
require.Equal(t, map[string]string{
"discovery.event.type": "metric.match",
"discovery.observer.id": "an_observer/observer.name",
"discovery.receiver.name": "receiver.name",
"discovery.receiver.rule": "a.rule",
"discovery.receiver.type": "a_receiver",
"discovery.status": string(status),
"discovery.message": "desired body content",
"metric.name": "desired.name",
"one": "one.value",
"two": "two.value",
"extra_attr": "target_resource",
}, cStore.Attrs(endpointID))
})
}
})
Expand Down
70 changes: 21 additions & 49 deletions internal/receiver/discoveryreceiver/statement_evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ func TestStatementEvaluation(t *testing.T) {
emitCh := cStore.EmitCh()
emitWG := sync.WaitGroup{}
emitWG.Add(1)
var corr correlation
go func() {
corr = <-emitCh
<-emitCh
emitWG.Done()
}()

receiverID := component.MustNewIDWithName("a_receiver", "receiver.name")
cStore.UpdateEndpoint(observer.Endpoint{ID: "endpoint.id"}, receiverID, observerID)
endpointID := observer.EndpointID("endpoint.id")
cStore.UpdateEndpoint(observer.Endpoint{ID: endpointID}, receiverID, observerID)

se, err := newStatementEvaluator(logger, component.MustNewID("some_type"), cfg, cStore)
require.NoError(t, err)
Expand All @@ -107,36 +107,17 @@ func TestStatementEvaluation(t *testing.T) {
// wait for the emit channel to be processed
emitWG.Wait()

entityEvents, numFailed, err := entityStateEvents(corr.observerID,
[]observer.Endpoint{corr.endpoint}, cStore, time.Now())
require.NoError(t, err)
require.Equal(t, 0, numFailed)
emitted := entityEvents.ConvertAndMoveToLogs()

require.Equal(t, 1, emitted.ResourceLogs().Len())
rl := emitted.ResourceLogs().At(0)
require.Equal(t, 0, rl.Resource().Attributes().Len())

sLogs := rl.ScopeLogs()
require.Equal(t, 1, sLogs.Len())
sl := sLogs.At(0)
lrs := sl.LogRecords()
require.Equal(t, 1, lrs.Len())
lr := sl.LogRecords().At(0)

oea, ok := lr.Attributes().Get(discovery.OtelEntityAttributesAttr)
require.True(t, ok)
entityAttrs := oea.Map()
attrs := cStore.Attrs(endpointID)

// Validate "caller" attribute
callerAttr, ok := entityAttrs.Get("caller")
callerAttr, ok := attrs["caller"]
require.True(t, ok)
_, expectedFile, _, _ := runtime.Caller(0)
// runtime doesn't use os.PathSeparator
splitPath := strings.Split(expectedFile, "/")
expectedCaller := splitPath[len(splitPath)-1]
require.Contains(t, callerAttr.Str(), expectedCaller)
entityAttrs.Remove("caller")
require.Contains(t, callerAttr, expectedCaller)
delete(attrs, "caller")

// Validate the rest of the attributes
expectedMsg := "desired body content"
Expand All @@ -147,29 +128,20 @@ func TestStatementEvaluation(t *testing.T) {
expectedMsg = fmt.Sprintf("%s (evaluated \"{\\\"field.one\\\":\\\"field.one.value\\\",\\\"field_two\\\":\\\"field.two.value\\\",\\\"message\\\":\\\"desired.statement\\\"}\")", expectedMsg)
}
}
require.Equal(t, map[string]any{
discovery.OtelEntityIDAttr: map[string]any{
"discovery.endpoint.id": "endpoint.id",
},
discovery.OtelEntityEventTypeAttr: discovery.OtelEntityEventTypeState,
discovery.OtelEntityAttributesAttr: map[string]any{
"discovery.event.type": "statement.match",
"discovery.observer.id": "an_observer/observer.name",
"discovery.receiver.name": "receiver.name",
"discovery.receiver.rule": `type == "container"`,
"discovery.receiver.type": "a_receiver",
"discovery.status": string(status),
"discovery.message": expectedMsg,
"name": `a_receiver/receiver.name/receiver_creator/rc.name/{endpoint=""}/endpoint.id`,
"attr.one": "attr.one.value",
"attr.two": "attr.two.value",
"field.one": "field.one.value",
"field_two": "field.two.value",
"discovery.observer.name": "observer.name",
"discovery.observer.type": "an_observer",
"endpoint": "",
},
}, lr.Attributes().AsRaw())
require.Equal(t, map[string]string{
"discovery.event.type": "statement.match",
"discovery.observer.id": "an_observer/observer.name",
"discovery.receiver.name": "receiver.name",
"discovery.receiver.rule": `type == "container"`,
"discovery.receiver.type": "a_receiver",
"discovery.status": string(status),
"discovery.message": expectedMsg,
"name": `a_receiver/receiver.name/receiver_creator/rc.name/{endpoint=""}/endpoint.id`,
"attr.one": "attr.one.value",
"attr.two": "attr.two.value",
"field.one": "field.one.value",
"field_two": "field.two.value",
}, attrs)
})
}
})
Expand Down