fluent-bit icon indicating copy to clipboard operation
fluent-bit copied to clipboard

Using multiple filters for the same record - is it possible?

Open felipejfc opened this issue 8 years ago • 29 comments

Is it possible to use multiple filters for the same record?

I'm trying to do the following:

  filter-kubernetes.conf: |
    [FILTER]
        Name           kubernetes
        Match          kube.*
        Kube_URL       https://kubernetes.default.svc:443
        Merge_JSON_Log On
        Annotations    Off

  filter-logging.conf: |
    [FILTER]
        Name           grep
        Match          kube.*
        Regex          kubernetes.namespace_name logging

  input-kubernetes.conf: |
    [INPUT]
        Name              tail
        Tag               kube.*
        Path              /var/log/containers/*.log
        Parser            docker
        DB                /var/log/flb_kube.db
        Mem_Buf_Limit     5MB
        Skip_Long_Lines   On
        Refresh_Interval  10
  output-fluentd.conf: |
    [OUTPUT]
        Name            forward
        Match           *
        Host            ${FLUENTD_HOST}
        Port            ${FLUENTD_PORT}

  fluent-bit.conf: |
    [SERVICE]
        Flush         10
        Log_Level     info
        Daemon        off
        Parsers_File  parsers.conf

    @INCLUDE input-kubernetes.conf
    @INCLUDE filter-kubernetes.conf
    @INCLUDE filter-logging.conf
    @INCLUDE output-fluentd.conf

  parsers.conf: |
    [PARSER]
        Name        docker
        Format      json
        Time_Key    time
        Time_Format %Y-%m-%dT%H:%M:%S.%L
        Time_Keep   On

however it does not seems to be working fine, what I wanted to happen is that filter-kubernetes add kubernetes metadata into the log and then filter-logging excludes the logs coming from logging namespace.

felipejfc avatar Jan 09 '18 17:01 felipejfc

I think the missing feature is at this level:

    Regex          kubernetes.namespace_name logging

In the record, namespace likely is a map key of kubernetes, but Regex configuration in the filter assumes you are asking for a key called "kubernetes.namespace" so that may be the reason why it's not working.

edsiper avatar Jan 11 '18 20:01 edsiper

@edsiper can it be resolved on fluentd side? If yes, then I think it would be nice to have it here also.

loburm avatar Jan 18 '18 08:01 loburm

@edsiper I have a similar request. I'd like to prune some of the added kubernetes fields, for example remove the kubernetes.docker_id field via a record_modifier filter after the kubernetes filter, but it won't match due to the reason you stated. Does the filter chain pass along the full json representation of a record allowing this functionality. Or is there a better way to do this? Thanks!

wpalmeri avatar Jan 20 '18 23:01 wpalmeri

definitely this is an enhancement request.

@loburm @wpalmeri for your specific use case, do you need it only on Grep Filter ?

edsiper avatar Jan 23 '18 21:01 edsiper

@edsiper I think that should be enough.

Main use cases for me: read logs from specified namespaces only or ignore some namespaces.

loburm avatar Jan 24 '18 10:01 loburm

@edsiper I would request this enhancement for the RecordModifier filter as well

wpalmeri avatar Jan 24 '18 17:01 wpalmeri

@edsiper Having:

"kubernetes": {
        "pod_name": "***",
        "namespace_name": "logging",
        "pod_id": "216cd7ae-1c7e-11e8-bb40-000c298df552",
        "labels": {
            "app": "***",
            "pod-template-hash": "3079797978"
        },
        "host": "***",
        "container_name": "***",
        "docker_id": "370face382c7603fdd309d8c6aaaf434fd98b92421ce7c7c8aafe7697d4aa362"
    }

and

[FILTER]
        Name           grep
        Match          kube.*
        Regex          kubernetes logging

it doesn't work either. It think it might be because the value of "kubernetes" is another JSON object upon which, right now, regexp don't apply. Maybe it should be treated as a string in this case or be able to have a 'nested' field like kubernetes.labels.app on plugins that use this, like record_modifier or grep.

What do you guys think?

lebenitza avatar Feb 28 '18 16:02 lebenitza

Meanwhile, I am using fluent-bit with forward to a fluentd server configured something like this:

    <source>
      @type forward
      port 24284
    </source>
    
    <filter kube.**>
      @type grep
      <regexp>
        key $.kubernetes.namespace_name
        pattern logging
      </regexp>
    </filter>

    <match kube.**>
      @type file
      path /tmp/logging
      time_slice_format %Y%m%d
      time_slice_wait 10m
      time_format %Y%m%dT%H%M%S%z
    </match>

It's a good enough workaround for me at the moment.

lebenitza avatar Feb 28 '18 17:02 lebenitza

I believe I'm looking for the same thing here - the ability to apply regex / exclude patterns on nested keys - so I'll add my vote for this enhancement.

tweettypography avatar Mar 08 '18 19:03 tweettypography

tip: why not to take advantage of the Exclude_Path configuration property ?

In Kubernetes every logfile have the namespace name in the same log file, so you can do it there. Note that I think that the ideal solution is to add a kind of Exclude_Namespace in Kubernetes filter configuration.

edsiper avatar Mar 28 '18 17:03 edsiper

@MihaiAnei I don't think this is a workaround, I prefer the approach of just forwarding to a fluentd aggregator with minimal parsing or filtering in fluent-bit. Then make the aggregator do the grunt work as this tends to be a "bigger" resource

deploymentking avatar Apr 04 '18 15:04 deploymentking

anybody managed exclude certain namespace logs without using fluntd aggregator?

vsamidurai avatar Apr 14 '18 11:04 vsamidurai

Since the path and exclude_path variables can handle wildcards I'm able to exclude namespaces.

exclude_path /var/logs/containers/*preprod*.log

DigitalRogues avatar Apr 14 '18 13:04 DigitalRogues

I have the similar request, would like to apply the kubernetes filter to enrich my logs and then apply a grep filter to only output log with errors.

Is this already possible in the latest 0.13.8 version somehow ?

mbelchin avatar Aug 30 '18 07:08 mbelchin

I've logged the following enhancement request for filter kubernetes:

https://github.com/fluent/fluent-bit/issues/755

the other requirement I see on this thread is about improve Grep to support nested fields, no ETA for that one yet.

edsiper avatar Sep 06 '18 17:09 edsiper

I think that my pull request would help (https://github.com/fluent/fluent-bit/pull/753): it allows formatting the tag as kubernetes.<namespace>.<pod>.<container> and you can then match on specific namespace based on this tag.

yann-soubeyrand avatar Sep 08 '18 10:09 yann-soubeyrand

Same problem for me too, i want grep log coming from kubernetes by regex matching on specific annotation

[FILTER] Name grep Match kube.* Regex kubernetes.annotations.log true

it search a "kubernetes.annotations.log" field but kubernetes output is a map like "kubernetes"=>{"annotations"=>{"log"=>"true"}}

how to specify this in the Regex ???

tuxtof avatar Oct 05 '18 05:10 tuxtof

@tuxtof I think your use case is the one which the fluentbit.io/exclude annotation targets (not exactly though, as you seem to want to include some logs instead of excluding others). See this page https://docs.fluentbit.io/manual/filter/kubernetes for the documentation on how to use this annotation.

yann-soubeyrand avatar Oct 05 '18 18:10 yann-soubeyrand

@yann-soubeyrand yes this is the same idea, but i want just select few container to log among a large number of them, so i can change all my container to put exclude annotations, i want no log by default and just activate some of them

tuxtof avatar Oct 05 '18 20:10 tuxtof

Can we extract the multiple fields from single field by doing multiple filters?

For example: the field "log" has string '172.17.0.1 - - [02/Nov/2018:13:16:02 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.61.1" "-" '

I want to do one more filtering and extract the fields, such as "client", "time", "http_method", "path" and so on. is this possible with fluent-bit?

karthikeayan avatar Nov 02 '18 13:11 karthikeayan