Issue / preferred way for filtering services

Hi,

I have a use case where I want to detect services based on some meta data. E.g. these meta data could state, what a service is capable of.

For that I used the Service.Meta field in the service definition file. I created two services which I can query using the following curl command:

curl -X GET localhost:8500/v1/agent/services

which gives the following result:

{
“service-1”: {
“ID”: “service-1”,
“Service”: “service-1”,
“Tags”: ,
“Meta”: {
“capability”: “do-something”
},
“Port”: 8085,
“Address”: “”,
“Weights”: {
“Passing”: 1,
“Warning”: 1
},
“EnableTagOverride”: false
},
“service-2”: {
“ID”: “service-2”,
“Service”: “service-2”,
“Tags”: [
“test-2”
],
“Meta”: {
“capability”: “do different things”
},
“Port”: 8086,
“Address”: “”,
“Weights”: {
“Passing”: 1,
“Warning”: 1
},
“EnableTagOverride”: false
}
}

I receive the exact same result when I try to apply a filter, using the following request:

curl -X GET localhost:8500/v1/agent/services --data-urlencode "filter=Meta.capability == do-something"

Am I doing something wrong? Is there a better / more suitable way to achieve what I want to do?

I used basically the example from the documentation: https://www.consul.io/api/features/filtering.html

Thanks!

Andreas

Short update:

I just noticed that the curl request is not entirely correct, sorry for that.

I now use

curl -G localhost:8500/v1/agent/services --data-urlencode "filter=Meta.capability == do-something"

but then I receive the following error:

Failed to create boolean expression evaluator: 1:28 (27): no match found, expected: “.”, “[”, [ \t\r\n], [a-zA-Z0-9_] or EOF

Ok got it working.

Apparently if the value you are filtering for contains dashes or spaces you have to put the value in quotes:

curl -G localhost:8500/v1/agent/services --data-urlencode 'filter=Meta.capability == "do-something"'

Is this behaviour intended? I can understand quotes in case of spaces but I am wondering why quotes are necessary in case of dashes…

1 Like

Hi @AndreasFetzer1,

The documentation for filter expressions states that string values should be quoted - https://www.consul.io/api/features/filtering.html#values. I would recommend always using quotes so that you avoid any unexpected or confusing filtering behavior.

1 Like

I had similar issue when defining Filter within service-resolver.
I am still finding quite confusing that the Filtering documentation stays that the strings should be quoted, while the documentation of service-resolver does not quote the “v1” and “v2” values, see https://www.consul.io/docs/agent/config-entries/service-resolver.html.

I did not test it yet, but I guess it should be something like:

kind           = "service-resolver"
name           = "web"
default_subset = "v1"
subsets = {
  "v1" = {
    filter = "Service.Meta.version == \"v1\""
  }
  "v2" = {
    filter = "Service.Meta.version == \"v2\""
  }
}

I tested it, it works with the \" :wink: