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

[BUG] Including hostname in json logs drops kube_cluster_name tag #14860

Closed
ryanartecona opened this issue Dec 27, 2022 · 3 comments
Closed

[BUG] Including hostname in json logs drops kube_cluster_name tag #14860

ryanartecona opened this issue Dec 27, 2022 · 3 comments

Comments

@ryanartecona
Copy link

Agent Environment

> agent version
Agent 7.39.2 - Commit: f6f7bc9 - Serialization version: v5.0.26 - Go version: go1.17.11

Running the agent as a k8s daemonset via the helm chart using mostly default values. Logs are enabled with container_collect_all: true.

Describe what happened:

I have an app printing json logs that are collected by the agent. If a log line includes hostname as a top level key in the json object, that value will override what datadog shows in the Host field for that line, causing it to lose association with the server host the log came from. Also, kube_cluster_name will not be added as a tag to that log line. This is a similar problem to the one in #14099.

Describe what you expected:

Normal log lines behave as expected. Specifically, if a log line does not include hostname in the json, datadog will show it as coming from the correct server and will show kube_cluster_name tag which is automatically added by the agent.

Steps to reproduce the issue:

  1. Run datadog-agent as a daemonset in k8s with logs enabled, a cluster name set, and container_collect_all: true.
  2. Start an app pod whose container runs the following bash script:
    while true; do 
      echo '{"message":"this line works properly"}'; 
      echo '{"message":"this line is buggy","hostname":"nonsense"}'; 
      sleep 10; 
    done;
    

Additional environment details (Operating System, Cloud provider, etc):

@vickenty
Copy link
Contributor

vickenty commented Jan 5, 2023

Hi @ryanartecona, thanks for using Datadog.

This issue queue is primarily intended for tracking features, bugs and work items associated with the datadog-agent open source project. For your request, please contact our support.

@vickenty vickenty closed this as completed Jan 5, 2023
@nicolas-vivot
Copy link

I confirm this bug. It drops all tags added by the kubernetes datadog agent when the hostname attribute is present inside json formatted logs.

@ryanartecona did you get any reply from the support ?

@nicolas-vivot
Copy link

After contacting the support, here is the explanation.

As described here, by default hostname is considered by the json preprocessing configuration to remap the host name.

Therefore the hostname attribute won't have the same value as expected. And this has the consequence of dropping all host related tags because tags are coupled with the hostname, as further explained by the support (screenshot of the complete answer below)

(tags are associated to hostname and not sent with logs every time for performance reason, therefore this drop effect happens when the hostname attribute is overriden on logs with a value that does not match the real host where Datadog agents are running on.

How to resolve that: get rid of the hostname attribute coming from your json logs, or pass the real host value to a HOSTNAME environment variable to your container to get the correct one.

Support answer:
image

sausage-todd pushed a commit to CrowdDotDev/crowd.dev that referenced this issue Jan 23, 2024
- Bunyan, when writing logs in json, always include `hostname` as a
  field
- Bunyan takes the value for that field from the os (you can check it
  either in `HOSTNAME` env variable, or running the `hostname` command)
- On kubernetes hostname in a container resolves to the pod name
- Datadog, when receiving logs, tries to match `hostname` field to a
  name of any host it knows about. If `hostname` field isn't present in
  logs, it tries to derive it from a kubernetes node, which these logs
  came from
- When `hostname` in logs doesn't match any of the hosts Datadog knows
  about, Datadog won't assign any extra tags, which those hosts already
  have
- This was creating a problem, when we assigned tags such as
  `account:crowd` and `cluster_name:crowd-kube-production` to nodes,
  which weren't propagating to logs coming from these nodes
- Without this tag propagation it's impossible to tell which kubernetes
  cluster the logs came from
- To fix that, we just make Bunyan to stop populating `hostname` field
  in json logs, allowing Datadog to do its magic

Similar issue here: DataDog/datadog-agent#14860
sausage-todd pushed a commit to CrowdDotDev/crowd.dev that referenced this issue Jan 23, 2024
- Bunyan, when writing logs in json, always includes `hostname` as a
  field
- Bunyan takes the value for that field from the os (you can check it
  either in `HOSTNAME` env variable, or running the `hostname` command)
- On kubernetes hostname in a container resolves to the pod name
- Datadog, when receiving logs, tries to match `hostname` field to a
  name of any host it knows about. If `hostname` field isn't present in
  logs, it tries to derive it from a kubernetes node, which these logs
  came from
- When `hostname` in logs doesn't match any of the hosts Datadog knows
  about, Datadog won't assign any extra tags, which those hosts already
  have
- This was creating a problem, when we assigned tags such as
  `account:crowd` and `cluster_name:crowd-kube-production` to nodes,
  which weren't propagating to logs coming from these nodes
- Without this tag propagation it's impossible to tell which kubernetes
  cluster the logs came from
- To fix that, we just make Bunyan to stop populating `hostname` field
  in json logs, allowing Datadog to do its magic

Similar issue here: DataDog/datadog-agent#14860
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants