entities.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package metadata // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata"
  4. import (
  5. "go.opentelemetry.io/collector/pdata/pcommon"
  6. metadataPkg "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata"
  7. )
  8. // GetEntityEvents processes metadata updates and returns entity events that describe the metadata changes.
  9. func GetEntityEvents(oldMetadata, newMetadata map[metadataPkg.ResourceID]*KubernetesMetadata, timestamp pcommon.Timestamp) metadataPkg.EntityEventsSlice {
  10. out := metadataPkg.NewEntityEventsSlice()
  11. for id, oldObj := range oldMetadata {
  12. if _, ok := newMetadata[id]; !ok {
  13. // An object was present, but no longer is. Create a "delete" event.
  14. entityEvent := out.AppendEmpty()
  15. entityEvent.SetTimestamp(timestamp)
  16. entityEvent.ID().PutStr(oldObj.ResourceIDKey, string(oldObj.ResourceID))
  17. entityEvent.SetEntityDelete()
  18. }
  19. }
  20. // All "new" are current objects. Create "state" events. "old" state does not matter.
  21. for _, newObj := range newMetadata {
  22. entityEvent := out.AppendEmpty()
  23. entityEvent.SetTimestamp(timestamp)
  24. entityEvent.ID().PutStr(newObj.ResourceIDKey, string(newObj.ResourceID))
  25. state := entityEvent.SetEntityState()
  26. state.SetEntityType(newObj.EntityType)
  27. attrs := state.Attributes()
  28. for k, v := range newObj.Metadata {
  29. attrs.PutStr(k, v)
  30. }
  31. }
  32. return out
  33. }