Skip to content

[inputs.docker_log] only persists state once at start then stalesΒ #18059

Description

@iCaotix

Relevant telegraf.conf

[agent]
  ...
  statefile = "/telegraf_state/statefile"
  ...

# Read logging output from the Docker engine
[[inputs.docker_log]]
  from_beginning = false
  container_state_include = ["created", "restarting", "running", "removing"]
  source_tag = true


[[outputs.file]]
  files = ["stdout"]
  data_format = "json"

Logs from Telegraf

telegraf-1  | id: cannot find name for group ID 988
telegraf-1  | 2025-11-25T19:27:24Z I! Loading config: /etc/telegraf/telegraf.conf
telegraf-1  | 2025-11-25T19:27:24Z I! Starting Telegraf 1.36.4 brought to you by InfluxData the makers of InfluxDB
telegraf-1  | 2025-11-25T19:27:24Z I! Available plugins: 239 inputs, 9 aggregators, 35 processors, 26 parsers, 65 outputs, 6 secret-stores
telegraf-1  | 2025-11-25T19:27:24Z I! Loaded inputs: cpu disk diskio docker docker_log internal kernel mem processes swap system temp
telegraf-1  | 2025-11-25T19:27:24Z I! Loaded aggregators:
telegraf-1  | 2025-11-25T19:27:24Z I! Loaded processors:
telegraf-1  | 2025-11-25T19:27:24Z I! Loaded secretstores:
telegraf-1  | 2025-11-25T19:27:24Z I! Loaded outputs: http
telegraf-1  | 2025-11-25T19:27:24Z I! Tags enabled: host=november
telegraf-1  | 2025-11-25T19:27:24Z I! [agent] Config: Interval:1m0s, Quiet:false, Hostname:"november", Flush Interval:1m5s
telegraf-1  | 2025-11-25T19:27:24Z W! [agent] The default value of 'skip_processors_after_aggregators' will change to 'true' with Telegraf v1.40.0! If you need the current default behavior, please explicitly set the option to 'false'!
telegraf-1  | 2025-11-25T19:27:24Z D! [agent] Initializing plugins
telegraf-1  | 2025-11-25T19:27:24Z D! [agent] Initializing plugin states
telegraf-1  | 2025-11-25T19:27:24Z D! [agent] Connecting outputs
telegraf-1  | 2025-11-25T19:27:24Z D! [agent] Attempting connection to [outputs.http]
telegraf-1  | 2025-11-25T19:27:24Z D! [agent] Successfully connected to outputs.http
telegraf-1  | 2025-11-25T19:27:24Z D! [agent] Starting service inputs
telegraf-1  | 2025-11-25T19:28:00Z D! [inputs.disk] [SystemPS] => unable to get disk usage ("/hostfs/run/docker/netns/71becc55e5ea"): permission denied
telegraf-1  | 2025-11-25T19:28:00Z D! [inputs.disk] [SystemPS] => unable to get disk usage ("/hostfs/run/docker/netns/418befc732bb"): permission denied
telegraf-1  | 2025-11-25T19:28:00Z D! [inputs.disk] [SystemPS] => unable to get disk usage ("/hostfs/run/docker/netns/5c7fd9cf95af"): permission denied
telegraf-1  | 2025-11-25T19:28:00Z D! [inputs.disk] [SystemPS] => unable to get disk usage ("/hostfs/run/docker/netns/6415ee35e679"): permission denied
telegraf-1  | 2025-11-25T19:28:00Z D! [inputs.disk] [SystemPS] => unable to get disk usage ("/hostfs/run/docker/netns/e83a954390a8"): permission denied
telegraf-1  | 2025-11-25T19:28:00Z D! [inputs.disk] [SystemPS] => unable to get disk usage ("/hostfs/run/docker/netns/a6d42e49677f"): permission denied
telegraf-1  | 2025-11-25T19:28:00Z D! [inputs.system] Reading users: open /hostfs/var/run/utmp: no such file or directory
telegraf-1  | 2025-11-25T19:28:29Z D! [outputs.http] Wrote batch of 197 metrics in 153.718499ms
telegraf-1  | 2025-11-25T19:28:29Z D! [outputs.http] Buffer fullness: 0 / 1000000 metrics

System info

Telegraf 1.36.4, Ubuntu 24.04, Docker version 29.0.4, build 3247a5a

Docker

services:
  telegraf:
    image: "telegraf:1.36.4"
    group_add:
      - "988"  # docker group to query docker socket
    privileged: true
    restart: unless-stopped
    volumes:
      - ./telegraf.conf:/etc/telegraf/telegraf.conf:ro
      - ./telegraf_state:/telegraf_state
      - /:/hostfs:ro
      - /run/udev:/run/udev:ro  # -> diskio
      - /var/run/docker.sock:/var/run/docker.sock:ro  # -> docker_log
    environment:
      HOST_ETC: /hostfs/etc
      HOST_PROC: /hostfs/proc
      HOST_SYS: /hostfs/sys
      HOST_VAR: /hostfs/var
      HOST_RUN: /hostfs/run
      HOST_MOUNT_PREFIX: /hostfs
      HOSTNAME: "november"
    command: ["telegraf", "--config", "/etc/telegraf/telegraf.conf"]

Steps to reproduce

  1. Deploy a server with docker installed
  2. Create the compose file as described above
  3. Create a folder "telegraf_state" in the directory where the compose.yml resides and give it group and owner of 999 (telegraf inside the container)
  4. Create the "statefile" with the content of "{}" and same permission as the folder
  5. Start the stack

Expected behavior

  1. After the first start you will see no logging of telegraf about a missing statefile which means it could read it and start successfully.
  2. After the first container starts it will write the initial offset in the statefile
  3. Now produce logs on a different container
  4. The statefile should after every submitted line update the statefile to the last read offset (in the case of this plugin the timestamp)

Actual behavior

It does not update the timestamp and after every restart it sends all logs of the container again from this once written timestamp on.
Metrics arrive successfully and no collection errors are logged, just the persistence seems to be broken.

Additional info

It is not connected to docker 29 because I fight with this behavior already for months.

Restarts produce these kinds of spikes for restarts in this graph you can see nine restarts which afterwards always send logs from containers running for multiple days.

Image

Activity

  1. added
    bug
    on Nov 25, 2025
  2. changed the title [-]inputs.docker_log only persists state once at start then stales[/-] [+][inputs.docker_log] only persists state once at start then stales[/+] on Nov 25, 2025
  3. srebhan commented on Dec 4, 2025

    @srebhan
    Member

    @iCaotix states are written on shutdown of Telegraf so you should see the update in the file after stopping Telegraf in the container. Can you please post the (redacted) log output of the Telegraf shutdown?

  4. self-assigned this
    on Dec 4, 2025
  5. iCaotix commented on Dec 4, 2025

    @iCaotix
    ContributorAuthor

    @srebhan
    The log says that the states persisted without logging an error but the statefile still is untouched

    .....
    2025-12-04T10:39:50Z D! [agent] Stopping service inputs
    2025-12-04T10:39:50Z D! [agent] Input channel closed
    2025-12-04T10:39:50Z I! [agent] Hang on, flushing any cached metrics before shutdown
    2025-12-04T10:39:50Z D! [outputs.http] Buffer fullness: 0 / 1000000 metrics
    2025-12-04T10:39:50Z I! [agent] Stopping running outputs
    2025-12-04T10:39:50Z D! [agent] Persisting plugin states
    2025-12-04T10:39:50Z D! [agent] Stopped Successfully
    
  6. iCaotix commented on Jan 7, 2026

    @iCaotix
    ContributorAuthor

    @srebhan
    After further experimenting I found out that the statefile gets written when I change the way the container is restarted / updated.

    Usually I did normal docker compose up -d or docker compose down to restart with a new version. In that case the statefile is not written at all. Probably the compose down has a race condition with persisting plugin state.

    So I tried a docker compose stop first and after that telegraf writes something to the file and also the file modified time is updated. But the content is somehow offset. Sometimes only by a couple hours, but some containers are dated back by multiple days. And it's not the case that the container didn't log something in that time. After the restart multiple log lines are sent again which were already sent before the restart.

    I tried adding

    stop_grace_period: 60s
    stop_signal: SIGTERM
    

    to the compose file which now consistently updates the statefile (even with compose down without prior explicit stopping) but the content issue still exists.

    Probably a fix would be to allow starting from the current timestamp if persistence is disabled instead of always from unix timestamp 0
    See code in

    since := time.Time{}.Format(time.RFC3339Nano)
    if !d.FromBeginning {
    d.lastRecordMtx.Lock()
    if ts, ok := d.lastRecord[cntnr.ID]; ok {
    since = ts.Format(time.RFC3339Nano)
    }
    d.lastRecordMtx.Unlock()
    }

    Which could be changed to start from "now" if there is no state to load or the load fails.

  7. reopened this on Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions