datadog-agent icon indicating copy to clipboard operation
datadog-agent copied to clipboard

Logs with datadog agent and ECS with excluded containers

Open Rowern opened this issue 8 years ago • 33 comments

Describe what happened:

We are running tasks on ECS so on a typical machine we have at least one container named: ecs-agent from image amazon/amazon-ecs-agent:latest running at all time. We also launch the datadog agent with these options:

docker run -d --name datadog-agent \
   -e DD_API_KEY=<REDACTED> \
   -e DD_LOGS_ENABLED=true \
   -e DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL="true" \
   -e DD_AC_EXCLUDE="image:.*" \
   -e DD_AC_INCLUDE="image:<REDACTED>.dkr.ecr.eu-west-2.amazonaws.com/.*" \
   -v /var/run/docker.sock:/var/run/docker.sock:ro \
   -v /proc/:/host/proc/:ro \
   -v /opt/datadog-agent/run:/opt/datadog-agent/run:rw \
   -v /cgroup/:/host/sys/fs/cgroup:ro \
   datadog/agent:latest

We have a task where we want to collect logs for. The image for this task matches the .dkr.ecr.eu-west-2.amazonaws.com/.* regex. We use these labels for autodiscovery on the container:

com.datadoghq.ad.check_names=[]
com.datadoghq.ad.init_configs=[{}]
com.datadoghq.ad.logs=[{"source":"twitter","service":"api","log_processing_rules":{"type":"include_at_match","name":"include_only_warning_or_error","pattern":"warn|WARN|Warn|error|ERROR|Error"}}]
com.datadoghq.ad.instances=[{}]

Another fact: removing the DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL option stops the agent at startup with the line [ AGENT ] 2018-05-28 09:15:25 UTC | ERROR | (start.go:228 in StartAgent) | Could not start logs-agent: could not find any valid logs configuration even when DD_LOGS_ENABLED=true and with the correct autodiscover labels So autodiscover is not really an autodiscovery for logs.

Describe what you expected:

I expected to only see logs from my application and not from datadog-agent or the ecs-agent. I do not need these logs on datadog.

How can we exclude these logs ?

Steps to reproduce the issue:

Launch the datadog agent docker with the options specified above. Launch two container, one with the labels above and one without. You will see logs from both containers.

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

$ cat /etc/os-release
NAME="Amazon Linux AMI"
VERSION="2018.03"
ID="amzn"
ID_LIKE="rhel fedora"
VERSION_ID="2018.03"
PRETTY_NAME="Amazon Linux AMI 2018.03"
ANSI_COLOR="0;33"
CPE_NAME="cpe:/o:amazon:linux:2018.03:ga"
HOME_URL="http://aws.amazon.com/amazon-linux-ami/"
$ docker --version
Docker version 17.12.1-ce, build 3dfb8343b139d6342acfd9975d7f1068b5b1c3d3

With ecs_agent_version: 1.18.0

(but this should not change anything)

Rowern avatar May 28 '18 12:05 Rowern

Hello @Rowern ,

Thanks for reaching out and raising this issue. Your setup is indeed supposed to work as you described. Having labels on your container should collect logs from this container only.

The issue has been identified and it seems that container labels are not taken into account if the environment variable is not there (or a file configuration with the docker type).

We are working on a fix but feel free to make any suggestion.

Until this is fixed you have two potential solutions:

  1. Collect and send it all and use our Dynamic Volume Control beta to filter out unwanted logs.

  2. Mount a configuration file in the agent conf.d directory with the following config:

logs:
  - type: docker
    name: <CONTAINER_NAME>
    source: twitter
    service: api
    log_processing_rules:
      - type: include_at_match
        name: include_only_warning_or_error
        pattern: warn|WARN|Warn|error|ERROR|Error

I have created an internal ticket with this issue attached so I'll make sure we reach out once this is fixed. Thanks, Nils

NilsBunge avatar May 28 '18 14:05 NilsBunge

Another thing I just noticed, with the DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL the different rules (passed by autodicovery labels) that should be applied to the logs are simply ignored by the agent. Here is the prettify version of the rule.

    "log_processing_rules": {
        "type": "include_at_match",
        "name": "include_only_warning_or_error",
        "pattern": "warn|WARN|Warn|error|ERROR|Error"
    }

Rowern avatar May 30 '18 08:05 Rowern

I just saw the PR #1747. If I created a conf file:

logs:
  - type: docker

Would the agent automatically assign source, service and log_processing_rules value based on the labels from the docker when collecting the logs? Or does DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL emulate exactly this and it would not change anything?

Rowern avatar May 30 '18 08:05 Rowern

Hi @Rowern,

DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL emulates this config:

logs:
  - type: docker
    service: docker
    source: docker

see here for more details.

I expect the processing rules attached to your container label to be parsed correctly and used by the agent at processing.

I think your config is invalid and the parsing fails because we expect an array of processing rules and you provided a single value, for more details see:

  • https://github.com/DataDog/datadog-agent/blob/master/pkg/logs/config/integration_config.go#L76
  • https://github.com/DataDog/datadog-agent/blob/master/pkg/logs/input/container/container.go#L146

Can you try to replace:

com.datadoghq.ad.logs=[{"source":"twitter","service":"api","log_processing_rules":{"type":"include_at_match","name":"include_only_warning_or_error","pattern":"warn|WARN|Warn|error|ERROR|Error"}}]

with:

com.datadoghq.ad.logs=[{"source":"twitter","service":"api","log_processing_rules":[{"type":"include_at_match","name":"include_only_warning_or_error","pattern":"warn|WARN|Warn|error|ERROR|Error"}]}]

Cheers

ajacquemot avatar May 30 '18 09:05 ajacquemot

Thanks @ajacquemot, it does work correctly with the array instead of the single value.

Still, do you plan on supporting container exclusion from log collection on the agent side? Maybe by using the already defined DD_AC_EXCLUDE DD_AC_INCLUDE variables?

If this is being added to the agent our need for log collection would be totally covered!

Rowern avatar May 31 '18 08:05 Rowern

Hello @Rowern,

Just to make sure I understand properly the question here. You want to collect logs from only a subset of containers, is that correct?

If yes, we are working on reading container labels config without the collect all environment variable. That would let you define only the subset of containers you want to collect logs from.

Would that cover your use case?

NilsBunge avatar May 31 '18 08:05 NilsBunge

Yes it would be perfect!

I saw the related PR was closed so I was worried the feature was dropped.

Rowern avatar May 31 '18 09:05 Rowern

The related PR was closed because it was not handling all the edge cases. We will work with the team to make sure we cover this properly but it is definitely planned to be supported and hopefully for the next agent version.

I'll keep this issue open until we can link the PR for this. Thanks.

NilsBunge avatar May 31 '18 09:05 NilsBunge

Hi.

I have the following ECS task definition template configuration which is ignored by datadog.

[
  {
    "name": "a-service",
    "image": "${a_docker_image}",
    "memoryReservation": "${ecs_memory_reservation}",
    "memory": "${ecs_memory}",
    "essential": true,
    "logConfiguration": {
        "logDriver": "json-file",
        "options": {
            "max-size": "20m",
            "max-file": "1"
        }
     },
    "dockerLabels": {
        "com.datadoghq.ad.logs": "[{\"source\": \"java\", \"service\": \"a-service\", \"log_processing_rules\": [{\"type\": \"multi_line\", \"pattern\": \"\\d{4}\\-(0?[1-9]|1[012])\\-(0?[1-9]|[12][0-9]|3[01])\", \"name\": \"new_log_start_with_date\"}]}]"
    },
    "command": [],
    "links": [],
    "mountPoints": [],
    "volumesFrom": [],
    "environment" : []
  }
]

I enabled the DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL and the above configuration seems to be ignored.

Do you think it's related to this issue?

matelang avatar Jun 06 '18 15:06 matelang

Hello @matelang ,

This should not be related to that issue. The problem here was that the labels were not taken into account unless the DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL flag was set to true.

So in your case the logs should be collected and they should have the service set to a-service and the source set to java as well as the multiline setup.

I'm just concerned about the quote escaping and will double check if this can have an impact.

That said, could you confirm that logs are properly collected? And what value of service and source you see for them?

Thanks.

NilsBunge avatar Jun 06 '18 15:06 NilsBunge

Hi @NBParis ,

The above configuration posted by me unfortunately does not work. (Logs are being collected by docker/docker as source and service, which is the default). Probably it has to do with the escaping of either the " or the \.

With the following config it works but I lose the multi-line log lines (e.g. stack traces).

"com.datadoghq.ad.logs": "[{\"source\": \"java\", \"service\": \"a-service\"}]"

matelang avatar Jun 06 '18 16:06 matelang

Therefore it could mean there is an issue with the processing rule section. It seems correct at first sight so I'll do some test on my side as well and come back to you with the outcome.

NilsBunge avatar Jun 06 '18 16:06 NilsBunge

I need this too! Also only want to get logs from a subset of containers. Have been banging my head against this for ages trying to work out why autodiscover was ignoring my log configuration.

Eagerly awaiting this update!!

em0ney avatar Jun 07 '18 05:06 em0ney

Hi @matelang,

Could you try to escape all \ in your pattern please ? i.e. change:

"com.datadoghq.ad.logs": "[{\"source\": \"java\", \"service\": \"a-service\", \"log_processing_rules\": [{\"type\": \"multi_line\", \"pattern\": \"\\d{4}\\-(0?[1-9]|1[012])\\-(0?[1-9]|[12][0-9]|3[01])\", \"name\": \"new_log_start_with_date\"}]}]"

to:

"com.datadoghq.ad.logs": "[{\"source\": \"java\", \"service\": \"a-service\", \"log_processing_rules\": [{\"type\": \"multi_line\", \"pattern\": \"\\\\d{4}\\\\-(0?[1-9]|1[012])\\\\-(0?[1-9]|[12][0-9]|3[01])\", \"name\": \"new_log_start_with_date\"}]}]"

Thanks

ajacquemot avatar Jun 07 '18 07:06 ajacquemot

Hi @ajacquemot,

I'll try that now.

FYI: The original pattern is \d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01]) so I already escaped the \ once because it's in a string instead of YAML as documented on https://docs.datadoghq.com/logs/log_collection/#multi-line-aggregation.

Cheers.

matelang avatar Jun 07 '18 07:06 matelang

It's unfortunately not working, although now the service name and source remained correct, but our stacktraces are still cut into multiple lines.

I have the following GROK pattern in DataDog - I cloned the standard Java Pipeline and changed the first step's GROK, the rest is untouched

java_smartup %{_date_slf4j}\s+%{_status}\s+\[%{_thread_name}\]\s+\[%{_request_id}\]\s+\[%{_logger_name}\]\s+%{data:message}((\n|\t)%{data:error.stack})?

Also our log messages are in the following format:

2018-06-07 10:58:29.266 ERROR [main] [] [o.s.boot.SpringApplication] Application startup failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:735)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:716)
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:703)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
    at io.smartup.cloud.DiscussionServiceApplication.main(DiscussionServiceApplication.java:16)
Caused by: java.lang.RuntimeException: Stacktraced exception
    at io.smartup.cloud.DiscussionServiceApplication.lambda$clr$0(DiscussionServiceApplication.java:22)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732)
    ... 6 common frames omitted

matelang avatar Jun 07 '18 08:06 matelang

@matelang We have replicated your issue and will troubleshoot further.

For clarity purpose, would it be possible to open either a new issue about this or a support ticket to [email protected] (with a link to that issue) as this thread was originally to have autodiscovery label taken into account without the DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL env variable set to true.

Thanks

NilsBunge avatar Jun 07 '18 08:06 NilsBunge

Hi @NBParis, The suggestion you have given to mount a configuration file in the agent conf.d directory is also not working. Agent is sending logs for all the containers :( and it would be very nice if you can give some estimate on how soon this will be fixed as we have to integrate this with our system as soon as possible. FYI I tried with image name and with container name as well.

amundra2016 avatar Jun 07 '18 08:06 amundra2016

Hello @amundra2016,

Sorry to hear that. Could you confirm that you did remove (or set to false) the DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL env variable?

As the environment variable behaves like a configuration file that ask to collect logs from all containers.

If you did, would it be possible to have more information about the configuration you tried?

NilsBunge avatar Jun 07 '18 09:06 NilsBunge

Thanks for the quick response @NBParis No, I kept it as true as I thought it would filter based on the yaml file in config, And yeah that worked perfectly alright!! Thanks for the help 🙇 .

amundra2016 avatar Jun 07 '18 09:06 amundra2016