prometheus icon indicating copy to clipboard operation
prometheus copied to clipboard

GCP filters in gce_sd_configs doesnt accept CLI filters

Open jwcmd opened this issue 7 years ago • 7 comments

Proposal

The filter field in gce_sd_configs does not accept the same syntax that the gcloud command line tool accepts, causing confusion for users. The filter field should accept the same syntax as the command line tool to follow the principle of least surprise.

Bug Report

Tried to setup prometheus with the gce_sd_configs configuration with a filter syntax that was copy/pasted from the command line. When that filter syntax didn't work, I tried googling for the correct syntax and when that failed too I resorted to trial and error, but have not been able to figure out the filter syntax the gce_sd_configs wants.

What did you expect to see? Using the filter string from the command line in gce_sd_configs should reveal the same hosts as the command line tool does.

What did you see instead? Under which circumstances? An error message from prometheus in the syslogs:

Apr 23 23:08:58 HOSTNAME prometheus[27494]: level=error ts=2019-04-23T23:08:58.217374002Z caller=gce.go:179 component="discovery manager scrape" discovery=gce msg="Refresh failed" err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items:(mytag)'. Invalid list filter expression., invalid"

Environment GCP

  • System information: Linux 4.15.0-1027-gcp x86_64

  • Prometheus version: prometheus, version 2.2.1 (branch: HEAD, revision: bc6058c81272a8d938c05e75607371284236aadc) build user: root@149e5b3f0829 build date: 20180314-14:15:45 go version: go1.10

  • Prometheus configuration file:

  global:
    scrape_interval: 15s

  scrape_configs:
    - job_name: 'prometheus'
      scrape_interval: 5s
      static_configs:
        - targets: ['localhost:9090']

    - job_name: 'group1'
      scrape_interval: 5s
      static_configs:
        - targets: ['localhost:9101',
                    'host1:9101',
                    'host2:9101'
                    ]

    - job_name: 'group2'
      scrape_interval: 5s
      static_configs:
        - targets: ['host10:8888',
                    'host11:8888']

    - job_name: 'asg'
      scrape_interval: 5s
      gce_sd_configs:
        - project: "myproject"
          zone: "us-west1-a"
          filter: "tags.items:(mytag)"
  • Logs:
Apr 23 23:09:58 HOSTNAME prometheus[27494]: level=error ts=2019-04-23T23:09:58.217499782Z caller=gce.go:179 component="discovery manager scrape" discovery=gce msg="Refresh failed" err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items:(mytag)'. Invalid list filter expression., invalid"

Thanks kindly to everyone who works on Prometheus! If I can provide any more info just let me know!

jwcmd avatar Apr 23 '19 23:04 jwcmd

So I was trying to s/asg-collector-staging/mytags/ but forget that it's not worth it and that string isn't really a secret.

Some examples of filter strings that I tried and their (identical) error messages:

err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'items[*].tags=['asg-collector-staging']'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'items[*]tags=['asg-collector-staging']'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'name eq *collector*'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags = \"asg-collector-staging\"'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags = asg-collector-staging'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags = asg-collector-staging''. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags eq 'asg-collector-staging''. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags eq \"asg-collector-staging\"'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags eq *collector*'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items='asg-collector-staging''. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items=('asg-collector-staging')'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items=(asg-collector-staging)'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items=['asg-collector-staging']'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items=[asg-collector-staging]'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items=\"asg-collector-staging\"'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items=asg-collector-staging'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items:'asg-collector-staging''. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items:('asg-collector-staging')'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items:(asg-collector-staging)'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items['asg-collector-staging']'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items[asg-collector-staging]'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items eq [\"asg-collector-staging\"]'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items eq [\\\"asg-collector-staging\\\"]'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.items eq \"asg-collector-staging\"'. Invalid list filter expression., invalid"
err="error retrieving refresh targets from gce: googleapi: Error 400: Invalid value for field 'filter': 'tags.itemssss[asg-collector-staging]'. Invalid list filter expression., invalid"

I've triple-checked that my instances do contain the network tag 'asg-collector-staging' (as well as other network tags), but none of these matched. Does anyone know the right incantation to filter by tags?

As a work-around I've filtered by names, but I'd prefer to match based on network tag because it's less ambiguous for our infrastructure.

Thanks again!

jwcmd avatar Apr 24 '19 00:04 jwcmd

Prometheus uses the Go GCE client and the documentation says:

A filter expression that filters resources listed in the response. The expression must specify the field name, a comparison operator, and the value that you want to use for filtering. The value must be a string, a number, or a boolean. The comparison operator must be either =, !=, >, or <.

For example, if you are filtering Compute Engine instances, you can exclude instances named example-instance by specifying name != example-instance.

You can also filter nested fields. For example, you could specify scheduling.automaticRestart = false to include instances only if they are not scheduled for automatic restarts. You can use filtering on nested fields to filter based on resource labels.

To filter on multiple expressions, provide each separate expression within parentheses. For example, (scheduling.automaticRestart = true) (cpuPlatform = "Intel Skylake"). By default, each expression is an AND expression. However, you can include AND and OR expressions explicitly. For example, (cpuPlatform = "Intel Skylake") OR (cpuPlatform = "Intel Broadwell") AND (scheduling.automaticRestart = true).

This is similar to what is described at the https://cloud.google.com/compute/docs/reference/rest/v1/instances/list page which is referenced in the Prometheus documentation.

Given that we can only expose what the Go GCE client offers, I don't see how we can achieve using the same syntax as the GCE CLI.

That being said and reading the GCE API documentation, I'm not sure how/if filters work with array-based fields like tags (StackOverflow seems to say no)...

simonpasquier avatar Apr 24 '19 08:04 simonpasquier

Found this issue which indicates that it is a limitation of the GCE API: https://issuetracker.google.com/120255780

simonpasquier avatar Apr 24 '19 08:04 simonpasquier

You can filter with this API for custom metadata: http://metadata.google.internal/computeMetadata/v1/instance/attibutes

aslimacc avatar Jun 12 '19 13:06 aslimacc

Hello,

Any update on this bug, please ? It's a big problem when we use Prometheus to monitor GCP resources.

Error 400: Invalid value for field 'filter': 'tags.items=couchbase'. Invalid list filter expression., invalid"

Thanks.

Best regards.

MohamedKhelifi avatar Mar 17 '20 13:03 MohamedKhelifi

Hello,

I found a filter working: filter: name="vmtest*" you can also use '!='

It work with the attribute NAME of :

gcloud compute instances list --filter='name:(vmtest*)' --project='pj-test-00000'
NAME         ZONE            MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP    EXTERNAL_IP  STATUS
vmtest01  europe-west1-b  n1-standard-4               ****************               RUNNING
vmtest02  europe-west1-c  n1-standard-4               ****************               RUNNING
vmtest03  europe-west1-d  n1-standard-4               ****************               RUNNING

You can test, the request API here: https://cloud.google.com/compute/docs/reference/rest/v1/instances/list

If the request it success the filter prometheus will be works.

Best regards.

MohamedKhelifi avatar Mar 18 '20 13:03 MohamedKhelifi

Hi, we've gone through this in the bugscrub and decided to close this as a limitation of the GCP library as explained here: https://github.com/prometheus/prometheus/issues/5499#issuecomment-486114953

If the upstream go library fixes this, we'll fix this as well when we upgrade.

gouthamve avatar Apr 27 '20 14:04 gouthamve