Consul: Consul HTTP API : List Services based on Tags

Created on 17 Oct 2018  路  7Comments  路  Source: hashicorp/consul

Hi,

This might sound as a very basic question but I am trying to find a way to list all the services registered in Consul via HTTP API having a specific tag.
I am able to list all services using http://consul:port/v1/catalog/services, however when I try to call http://consul:port/v1/catalog/services/?tag="xyz" there is no output.
Some of my registered services also have tags like "category=animal", can I list down these as well if need be?
Any pointers would be appriciated.

Thanks,
V.

Most helpful comment

@pearkes but it is only workaround..
how about supporting it in API itself?

All 7 comments

@varundmishra http://consul:port/v1/catalog/services?tag="xyz" might work in a perfect world, but is not implemented.

Currently, the API does not filter using tags, meaning, you have to filter yourself within service, for instance, using jq in command line, you can easily retrieve all services with tag "xyz":

curl http://localhost:8500/v1/catalog/services | jq 'to_entries|.[]|select(.value[]|select(.=="xyz") ).key'

will output all services having at least one instance having tag "xyz".

For this specific reason, I implemented this exact filtering in Consul-Templaterb since it is a very common use-case, see TemplateAPI.md for details

Many Thanks @pierresouchay for the info, I will check the links and the sample curl command.

Agree this is a suitable workaround posted by @pierresouchay.

I'd be glad to see this feature supported in the services API. The provided solution to get all services, parse them and then filter is only a workaround.

@pearkes but it is only workaround..
how about supporting it in API itself?

Hi, what is the current status of this issue? I am volunteer to contribute to add such a feature to api. @pearkes @pierresouchay

@mstrYoda You can try implement it if you want, this is often asked by people (even if the performance gain is low most of the time I think), so I don't think HashiCorp's will reject it.
There are a few things to consider about blocking queries:

  • be sure to wake-up caller only when a service with given tag is modified
  • be sure to wake-up caller when a service witgh given tag disappaears
Was this page helpful?
0 / 5 - 0 ratings

Related issues

lmb picture lmb  路  4Comments

philsttr picture philsttr  路  3Comments

powerman picture powerman  路  3Comments

wargamez picture wargamez  路  4Comments

eshujiushiwo picture eshujiushiwo  路  3Comments