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

log_processing_rules ignored in ECS task definition

Open matelang opened this issue 8 years ago • 3 comments

Describe what happened: I'd like to launch an ECS task on AWS, having the following task def.

[
  {
    "name": "web-service",
    "image": "${webapp_docker_image}",
    "memoryReservation": ${ecs_memory_reservation},
    "memory": ${ecs_memory},
    "essential": true,
    "logConfiguration": {
        "logDriver": "json-file",
        "options": {
            "max-size": "20m",
            "max-file": "1"
        }
     },
     "healthCheck": {
        "command": ["CMD-SHELL", "curl -f http://localhost:$(cat /opt/app/management_port_file)/health || exit 1"],
        "interval": 10,
        "timeout": 3,
        "retries": 3,
        "startPeriod": 30
    },
    "dockerLabels": {
        "com.datadoghq.ad.logs": "[{\"source\": \"java\", \"service\": \"web-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" : [
         { "name" : "CONSUL_HOST", "value" : "${consul_host}" }
       ]
  }
]

Please note that the pattern's \ characters are escaped adding an additional \. The configuration is not taken into account at all, the other settings (source, service) are ignored as well and the service starts by having docker both for source and service (default).

If I change the pattern to the following, then the source/service name is correctly reported, but the multi-line configuration is still ignored.

"com.datadoghq.ad.logs": "[{\"source\": \"java\", \"service\": \"web-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\"}]}]"

This issue was originally reported in https://github.com/DataDog/datadog-agent/issues/1736#issuecomment-395330253

Describe what you expected: The service should have started correctly, reporting the defined source and service values, and the multi-line log parsing should work as defined in the pattern.

Steps to reproduce the issue: Define a task with the previous example ECS task definition. Run the task and observe the aforementioned anomalies in the Log Explorer.

Additional environment details (Operating System, Cloud provider, etc): Our Amazon infrastucture is managed using Terraform, so the above described ECS task def is interpolated using standard Terraform interpolation first. (Note the ${var} notation on some variables).

The pipeline in DataDog is a clone of the Standard Java pipeline with modifiing the extraction step (1st step) to have the following GROK pattern:

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

Also a sample log line from our system is as follows:

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

In the Java app, the following Spring Boot configuration (in application.yml) defines the log pattern:

logging:
  level:
    root: WARN
    liquibase: INFO
    io:
      smartup: INFO
  pattern:
    console: "%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] [%X{X-B3-TraceId}] [%logger{30}] %msg%n%ex{full}"

matelang avatar Jun 07 '18 09:06 matelang

Thanks for opening a separate issue. As discussed on the other one, we have replicated and are currently investigation why the processing rule is ignored in the container labels.

NilsBunge avatar Jun 07 '18 16:06 NilsBunge

This is now working with the following configuration:

    "logConfiguration": {
        "logDriver": "json-file",
        "options": {
            "max-size": "20m",
            "max-file": "3"
        }
    },
    "dockerLabels": {
        "com.datadoghq.ad.logs": "[{\"source\": \"java\", \"service\": \"api-gateway\", \"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\"}]}]"
    },

Another issue we faced is if you have the max-file set to 1 the agent can get stuck in tailing the log file.

Probably a longer term optimization would be to give up if the log file can not be tailed anymore and move on after a while(e.g. it was rotated by logrotate).

It makes sense after all, but maybe you guys want to add it to the docs.

Thanks for the quick fix.

matelang avatar Jun 15 '18 15:06 matelang

Thanks @matelang for testing out and your feedback, we'll have a look at this limitation. Cheers

ajacquemot avatar Jun 16 '18 07:06 ajacquemot