consul icon indicating copy to clipboard operation
consul copied to clipboard

Consul HTTP API : List Services based on Tags

Open varundmishra opened this issue 8 years ago • 7 comments

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.

varundmishra avatar Oct 17 '18 11:10 varundmishra

@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

pierresouchay avatar Oct 17 '18 20:10 pierresouchay

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

varundmishra avatar Oct 19 '18 12:10 varundmishra

Agree this is a suitable workaround posted by @pierresouchay.

pearkes avatar Oct 26 '18 16:10 pearkes

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.

yury-kozlov avatar May 16 '19 13:05 yury-kozlov

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

Frodox avatar Aug 09 '19 15:08 Frodox

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

mstrYoda avatar Mar 05 '20 08:03 mstrYoda

@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

pierresouchay avatar Mar 06 '20 07:03 pierresouchay