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

[journald] Adds user-level unit filtering #11398

Merged
merged 7 commits into from
Apr 25, 2022

Conversation

ian28223
Copy link
Contributor

@ian28223 ian28223 commented Mar 22, 2022

What does this PR do?

  • Adds User-level service unit filtering support for Journald log collection via include_user_units and exclude_user_units.
  • A wildcard (*) can be used in either exclude_units or exclude_user_units
    if only a particular type of Journald log is desired.

Motivation

  • AGENT-7627
  • FRAGENT-1752

Additional Notes

  • Tested on a Ubuntu 21 vagrant box

Possible Drawbacks / Trade-offs

Describe how to test/QA your changes

### Ubuntu 21 / vagrant
### Create an example service in a non-root user's home directory

cat <<EOF > /home/vagrant/jtest.sh
#!/bin/bash

while true; do
    echo \$(date -u) - \$2.service \$1 | systemd-cat
    sleep 1;
done;
EOF
chown vagrant.vagrant /home/vagrant/jtest.sh
chmod +x ~/jtest.sh


### Create Systemd Unit files

## Creates System-level Unit files

cat << EOF > /etc/systemd/system/foo.service
[Unit]
Description=Foo
[Service]
ExecStart=/home/vagrant/jtest.sh system-level foo
[Install]
WantedBy=multi-user.target
EOF

cat << EOF > /etc/systemd/system/bar.service
[Unit]
Description=Bar
[Service]
ExecStart=/home/vagrant/jtest.sh system-level bar
[Install]
WantedBy=multi-user.target
EOF


## Creates User-level Unit files

cat << EOF > /etc/systemd/user/foo.service
[Unit]
Description=Foo
[Service]
ExecStart=/home/vagrant/jtest.sh user-level foo
[Install]
WantedBy=multi-user.target
EOF

cat << EOF > /etc/systemd/user/bar.service
[Unit]
Description=Bar
[Service]
ExecStart=/home/vagrant/jtest.sh user-level bar
[Install]
WantedBy=multi-user.target
EOF


### Reload and start system-level services

systemctl daemon-reload
systemctl restart foo.service
systemctl restart bar.service


### Reload and start user-level services

## Log on as non-root user
su - vagrant

## Set XDG_RUNTIME_DIR
export XDG_RUNTIME_DIR=/run/user/$(id -u)

## Reload and start the services
systemctl --user daemon-reload
systemctl --user restart foo.service
systemctl --user restart bar.service

systemctl --user status foo.service
systemctl --user status bar.service


### Install / configure the agent

## Set API
sudo sh -c "sed 's/api_key:.*/api_key: XXX/' /etc/datadog-agent/datadog.yaml.example > /etc/datadog-agent/datadog.yaml"
## Enable logs
sudo sh -c "sed -i 's/.*logs_enabled:.*/logs_enabled: true/' /etc/datadog-agent/datadog.yaml"


## Prep journald log collection
usermod -a -G systemd-journal dd-agent
touch /etc/datadog-agent/conf.d/logs.yaml
chown -R dd-agent.dd-agent /etc/datadog-agent/

## Test 1: include SYS foo.service and USR bar.service logs

cat << EOF > /etc/datadog-agent/conf.d/logs.yaml
logs:
- type: journald
  include_units:
   - foo.service
  include_user_units:
   - bar.service
EOF
systemctl restart datadog-agent.service && sleep 2

# Expect in message 'foo.service system-level' and 'bar.service user-level'
datadog-agent stream-logs | grep _SYSTEMD_UNIT | grep -Eo "\w+\.service \w+-level"

## Test 2: exclude SYS foo.service and USR bar.service logs

cat << EOF > /etc/datadog-agent/conf.d/logs.yaml
logs:
- type: journald
  exclude_units:
   - foo.service
  exclude_user_units:
   - bar.service
EOF
systemctl restart datadog-agent.service && sleep 2

# Expect in message 'bar.service system-level' and 'foo.service user-level'
datadog-agent stream-logs | grep _SYSTEMD_UNIT | grep -Eo "\w+\.service \w+-level"

## Test 3: exclude all SYS service

cat << EOF > /etc/datadog-agent/conf.d/logs.yaml
logs:
- type: journald
  exclude_units:
   - '*'
EOF
systemctl restart datadog-agent.service && sleep 2

# Expect in message 'foo.service user-level' and 'bar.service user-level'
datadog-agent stream-logs | grep _SYSTEMD_UNIT | grep -Eo "\w+\.service \w+-level"

## Test 4: exclude all USR service

cat << EOF > /etc/datadog-agent/conf.d/logs.yaml
logs:
- type: journald
  exclude_user_units:
   - '*'
EOF
systemctl restart datadog-agent.service && sleep 2

# Expect in message 'foo.service system-level' and 'bar.service system-level'
datadog-agent stream-logs | grep _SYSTEMD_UNIT | grep -Eo "\w+\.service \w+-level"

Reviewer's Checklist

  • If known, an appropriate milestone has been selected; otherwise the Triage milestone is set.
  • Use the major_change label if your change either has a major impact on the code base, is impacting multiple teams or is changing important well-established internals of the Agent. This label will be use during QA to make sure each team pay extra attention to the changed behavior. For any customer facing change use a releasenote.
  • A release note has been added or the changelog/no-changelog label has been applied.
  • Changed code has automated tests for its functionality.
  • Adequate QA/testing plan information is provided if the qa/skip-qa label is not applied.
  • At least one team/.. label has been applied, indicating the team(s) that should QA this change.
  • If applicable, docs team has been notified or an issue has been opened on the documentation repo.
  • If applicable, the need-change/operator and need-change/helm labels have been applied.
  • If applicable, the config template has been updated.

@ian28223 ian28223 added do-not-merge/WIP [deprecated] team/agent-core Deprecated. Use metrics-logs / shared-components labels instead.. component/logs kind/feature labels Mar 22, 2022
@ian28223 ian28223 added this to the 7.36.0 milestone Mar 22, 2022
@ian28223 ian28223 requested a review from a team as a code owner March 22, 2022 02:37
@ian28223 ian28223 force-pushed the ian.bucad/journald_filter_units branch from dcca026 to aa79ed6 Compare March 22, 2022 06:29
@ian28223 ian28223 force-pushed the ian.bucad/journald_filter_units branch from aa79ed6 to 9f456b2 Compare March 22, 2022 09:39
pkg/logs/config/integration_config.go Outdated Show resolved Hide resolved
@ian28223 ian28223 force-pushed the ian.bucad/journald_filter_units branch from ebfd374 to 92f6598 Compare March 28, 2022 12:04
@ian28223 ian28223 requested a review from a team as a code owner March 28, 2022 12:18
@ian28223 ian28223 force-pushed the ian.bucad/journald_filter_units branch from e191e84 to cc4df54 Compare March 28, 2022 23:43
Copy link
Contributor

@apigirl apigirl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 release note looks fine

Copy link
Contributor

@remeh remeh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code change LGTM, @ian28223 please provide a PR description and everything like in the other PRs, see the template if needed. (an extremely important section is the QA one)

@ian28223 ian28223 changed the title Adds user-level unit filtering [journald] Adds user-level unit filtering Apr 6, 2022
@ian28223 ian28223 merged commit 2c2bd82 into main Apr 25, 2022
@ian28223 ian28223 deleted the ian.bucad/journald_filter_units branch April 25, 2022 03:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
7.36.0-drop component/logs [deprecated] team/agent-core Deprecated. Use metrics-logs / shared-components labels instead.. kind/feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants