VMware Modern Apps Community
AbhishekSK
Hot Shot
Hot Shot
Jump to solution

How can I do multiple queries with the /chart/api endpoint?

All is in the question

0 Kudos
1 Solution

Accepted Solutions
AbhishekSK
Hot Shot
Hot Shot
Jump to solution

Hi Chris,

The chart/api takes in single query expression for the q parameter. So if you want to run multiple queries you will have to make separate rest calls for each query.

You may have seen the documentation already under Gear Icon (top right hand corner) → API Documentation.

The swagger documentation allows you to experiment with APIs via the UI though I often prefer curl. To invoke an API via curl (or your code) you need a valid API token. To get one just go to Gear Icon → API Access and generate from there.

Here is one example of such curl command

current_epochseconds=`date +%s`

two_hours_ago=`expr $current_epochseconds - 7200`

curl -G "https://<YOUR_INSTANCE>.wavefront.com/chart/api?&s=$two_hours_ago&e=$current_epochseconds&g=s&i=false&strict=true" -H 'X-AUTH-TOKEN: <TOKEN>' -data-urlencode 'q=sum(rate(ts(~collector.points.valid)))' | python -mjson.tool

replacing <TOKEN> with one copy/pasted from the Wavefront UI.

The --data-urlencode option for curl is helpful because you can copy/paste queries from the Wavefront UI with spaces, “ characters and so on and have it “just work”.

In the above example I am passing a few options that I think make sense when using the API. Some are required but I’ll go through all of them:

  • s — start time of the query in epoch seconds, required.
  • e — end time of the query in epoch seconds, will default to current though more often than not I supply it.
  • i=false — The wavefront UI can pull data in from outside the window of chart. Usually this isn’t wanted with a API query so I always set this to false
  • strict=true — similar to above, when the Wavefront UI runs queries the backend can return some points to the left and right hand side of the chart so that the lines can be interpolated into and out of the chart. This is good for a UI, but usually not what you want in an automated query. I therefore almost always set strict=true to make sure all returned points are within the bounds of s and e.
  • g=s — Over a large time window, there will be usually be more real data points than there are pixels on the users screen. The Wavefront UI therefore wants only the right number of “pixels” worth of data to be returned from a query. The Wavefront UI actually uses the p=NUMBER option to control exactly how many points to return based on the size of the users screen. When using the API though I find the g= option to be much simpler, g=s will return the raw second level data. If looking at a wide time period though you may want g=m, g=h or even g=d to reduce the amount of network traffic. If you use a value of g higher than s, like m for minutes for instance, the raw points of the returned data will be grouped together/summarized according to the summarization=option which defaults to MEAN. With g=s you will get the raw data anyway without summarization.

If you use Ruby and want to make api calls from your ruby code we do have a Wavefront client ruby gem that you can use.

It also has a command line client that you can use once you install this gem.

eg:

wavefront-client '<TS_EXPRESSION>' --minutes --token <YOUR_TOKEN > # <TS_EXPRESSION> : Placeholder for a valid Wavefront ts() query , <YOUR_TOKEN> : Placeholder for your Wavefront token 

We should be releasing a similar python library as well soon.

Hope this is helpful.

Thanks,

Salil D

View solution in original post

0 Kudos
2 Replies
AbhishekSK
Hot Shot
Hot Shot
Jump to solution

Hi Chris,

The chart/api takes in single query expression for the q parameter. So if you want to run multiple queries you will have to make separate rest calls for each query.

You may have seen the documentation already under Gear Icon (top right hand corner) → API Documentation.

The swagger documentation allows you to experiment with APIs via the UI though I often prefer curl. To invoke an API via curl (or your code) you need a valid API token. To get one just go to Gear Icon → API Access and generate from there.

Here is one example of such curl command

current_epochseconds=`date +%s`

two_hours_ago=`expr $current_epochseconds - 7200`

curl -G "https://<YOUR_INSTANCE>.wavefront.com/chart/api?&s=$two_hours_ago&e=$current_epochseconds&g=s&i=false&strict=true" -H 'X-AUTH-TOKEN: <TOKEN>' -data-urlencode 'q=sum(rate(ts(~collector.points.valid)))' | python -mjson.tool

replacing <TOKEN> with one copy/pasted from the Wavefront UI.

The --data-urlencode option for curl is helpful because you can copy/paste queries from the Wavefront UI with spaces, “ characters and so on and have it “just work”.

In the above example I am passing a few options that I think make sense when using the API. Some are required but I’ll go through all of them:

  • s — start time of the query in epoch seconds, required.
  • e — end time of the query in epoch seconds, will default to current though more often than not I supply it.
  • i=false — The wavefront UI can pull data in from outside the window of chart. Usually this isn’t wanted with a API query so I always set this to false
  • strict=true — similar to above, when the Wavefront UI runs queries the backend can return some points to the left and right hand side of the chart so that the lines can be interpolated into and out of the chart. This is good for a UI, but usually not what you want in an automated query. I therefore almost always set strict=true to make sure all returned points are within the bounds of s and e.
  • g=s — Over a large time window, there will be usually be more real data points than there are pixels on the users screen. The Wavefront UI therefore wants only the right number of “pixels” worth of data to be returned from a query. The Wavefront UI actually uses the p=NUMBER option to control exactly how many points to return based on the size of the users screen. When using the API though I find the g= option to be much simpler, g=s will return the raw second level data. If looking at a wide time period though you may want g=m, g=h or even g=d to reduce the amount of network traffic. If you use a value of g higher than s, like m for minutes for instance, the raw points of the returned data will be grouped together/summarized according to the summarization=option which defaults to MEAN. With g=s you will get the raw data anyway without summarization.

If you use Ruby and want to make api calls from your ruby code we do have a Wavefront client ruby gem that you can use.

It also has a command line client that you can use once you install this gem.

eg:

wavefront-client '<TS_EXPRESSION>' --minutes --token <YOUR_TOKEN > # <TS_EXPRESSION> : Placeholder for a valid Wavefront ts() query , <YOUR_TOKEN> : Placeholder for your Wavefront token 

We should be releasing a similar python library as well soon.

Hope this is helpful.

Thanks,

Salil D

0 Kudos
AbhishekSK
Hot Shot
Hot Shot
Jump to solution

Hi Salil,

Thanks for the answer. So I'll have to do more requests on your API

Have a nice day,

0 Kudos