open-telemetry / opentelemetry-collector-contrib

Contrib repository for the OpenTelemetry Collector
https://opentelemetry.io
Apache License 2.0
2.77k stars 2.19k forks source link

add EntityState entity event as log record for host #33927

Closed povilasv closed 5 days ago

povilasv commented 3 weeks ago

Component(s)

receiver/hostmetrics

Is your feature request related to a problem? Please describe.

I would like to add EntityState Entity event, similar to what we have in k8s_cluster receiver, when is configured as log consumer. We would send a event on startup and also periodically if metadata_collection_interval is configured.

This event would be empty, so that resourcedetection processor would fill in the details. Initially transform processor would be also needed to fit the Entity model.

Example pipeline:

    receivers:
      hostmetrics:
        enabled: true
        metadata_collection_interval: "5m"

    processors:
      resourcedetection/env:
        detectors: ["system", "env"]
        timeout: 2s
        override: false
        system:
          resource_attributes:
            host.id:
              enabled: true
      resourcedetection/region:
        detectors: ["gcp", "ec2", "azure"]
        timeout: 2s
        override: true
      transform/entity-event:
        log_statements:
          - context: log
            statements:
              - set(attributes["otel.entity.attributes"]["otel.entity.id"]["host.id"], resource.attributes["host.id"])
              - set(attributes["otel.entity.attributes"]["host.name"], resource.attributes["host.name"])
              - set(attributes["otel.entity.attributes"]["host.type"], resource.attributes["host.type"])
              - set(attributes["otel.entity.attributes"]["host.image.id"], resource.attributes["host.image.id"])
              - set(attributes["otel.entity.attributes"]["host.image.name"], resource.attributes["host.image.name"])
          - context: resource
            statements:
              - keep_keys(attributes, [""])
    service:
      pipelines:
        logs/entity:
          exporters:
            - otlp
          processors:
            - k8sattributes
            - resourcedetection/env
            - resourcedetection/region
            - transform/entity-event
          receivers:
            - hostmetrics

Example event we would produce:

{
  "logRecord": {
    "attributes": {
      "otel.entity.attributes": {
        "host.name": "kind-control-plane",
        "otel.entity.id": {
          "host.id": "bf57b58e587d4115a373ba1199bf4b70"
        }
      },
      "otel.entity.event.type": "entity_state",
      "otel.entity.type": "host"
    },
    "timeUnixNano": 1720168109538054222
  },
  "resource": {
    "attributes": {}
  },
  "resourceSchemaUrl": "https://opentelemetry.io/schemas/1.6.1",
  "scope": {
    "attributes": {
      "otel.entity.event_as_log": true
    }
  }
}

Example k8s_cluster_receiver entity event:

{
   "logRecord":{
      "attributes":{
         "otel.entity.attributes":{
            "app.kubernetes.io/instance":"otel-coralogix-integration",
            "app.kubernetes.io/managed-by":"Helm",
            "app.kubernetes.io/name":"opentelemetry-agent",
            "app.kubernetes.io/version":"0.104.0",
            "daemonset.creation_timestamp":"2024-07-05T07:43:47Z",
            "helm.sh/chart":"opentelemetry-agent-0.87.0",
            "k8s.workload.kind":"DaemonSet",
            "k8s.workload.name":"coralogix-opentelemetry-agent"
         },
         "otel.entity.event.type":"entity_state",
         "otel.entity.id":{
            "k8s.daemonset.uid":"9280b46e-8e45-4f5b-80dd-f078c4ca3d57"
         },
         "otel.entity.type":"k8s.daemonset"
      },
      "timeUnixNano":1720167199952089629
   },
   "scope":{
      "attributes":{
         "otel.entity.event_as_log":true
      }
   }
}

Describe the solution you'd like

-

Describe alternatives you've considered

No response

Additional context

No response

github-actions[bot] commented 3 weeks ago

Pinging code owners:

povilasv commented 3 weeks ago

Would appreciate @tigrannajaryan review, given you did all the work around Entity events.

tigrannajaryan commented 3 weeks ago

+1 for producing EntityState for the Host. This is our (Entity SIG) intent in the future, Host will be one of the entities we would like to report. As long as this is marked "alpha" since we are not yet certain about what Id and Attributes the host should include.

The event's shape looks slightly wrong to me. The "otel.entity.id" should not be contained in "otel.entity.attributes". I think this is what would be expected:

{
  "logRecord": {
    "attributes": {
      "otel.entity.attributes": {
        "host.name": "kind-control-plane",
      },
      "otel.entity.id": {
        "host.id": "bf57b58e587d4115a373ba1199bf4b70"
      }
      "otel.entity.event.type": "entity_state",
      "otel.entity.type": "host"
    },
    "timeUnixNano": 1720168109538054222
  },
  ...
}

To make this work nicely the resourcedetection processor likely needs to be reworked to gain understanding of entities so that it can put the right attributes in the "otel.entity.id".

I have a draft of what it can possibly look like in this prototype: https://github.com/tigrannajaryan/opentelemetry-collector-contrib/pull/1

povilasv commented 3 weeks ago

Thanks for the info @tigrannajaryan, so basically for now something like this can be used:

      transform/entity-event:
        log_statements:
          - context: log
            statements:
              - set(attributes["otel.entity.id"]["host.id"], resource.attributes["host.id"])
              - set(attributes["otel.entity.attributes"]["host.name"], resource.attributes["host.name"])
              - set(attributes["otel.entity.attributes"]["host.type"], resource.attributes["host.type"])
              - set(attributes["otel.entity.attributes"]["host.image.id"], resource.attributes["host.image.id"])
              - set(attributes["otel.entity.attributes"]["host.image.name"], resource.attributes["host.image.name"])
          - context: resource
            statements:
              - keep_keys(attributes, [""])

I put the logs in "development" stability in this https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/33928, would apprecaite if you could take a look / comment :bow:

I would like to do it in stages, first add capability in hostmetrics receiver, next understand and add the needed changes in resourcedetection processor.

What do you think?

tigrannajaryan commented 3 weeks ago

I would like to do it in stages, first add capability in hostmetrics receiver, next understand and add the needed changes in resourcedetection processor.

Sounds good to me. Thanks for working on this.

I just want to emphasize one more time: we are in the very early stages of designing the entities, so we should expect significant changes as we gain better understanding over time. I think it would be also great for you to attend Entities SIG meetings to both be aware of developments and also help us with any feedback you may have as you work on this.