当前位置:网站首页>Promql select time series

Promql select time series

2022-07-01 03:41:00 Foxconn quality inspector zhangquandan

Instantaneous vector , Interval vector , Instantaneous query , Interval query .

Request URL: http://139.198.166.235:9090/api/v1/query?query=demo_api_http_requests_in_progress&time=1644905624.121

You can see that this is a query promql The interface of ,query The parameter is actually promql A statement of ,time In fact, it is the timestamp of the evaluation , This only contains one promql A query such as a statement and a timestamp is actually a transient query .

The result is an instantaneous vector .

This is also a transient query , Only the result of instantaneous query is an interval vector .

The result of an instantaneous query may be an instantaneous vector , It could also be an interval vector . stay table All queries in the internal view are instantaneous queries . 

 

If you switch to Graph Come here , Or the previous query , You can see that the interface has changed

 

Request URL: http://139.198.166.235:9090/api/v1/query_range?query=demo_api_http_requests_in_progress&start=1644904540.348&end=1644906340.348&step=7

query_range This is a range query , Or interval query . Because you need to draw , So it's not a point , Is within a time frame . 

stay granph All the queries are interval queries .

 

Choose a time series


In this section, we will learn how to select data in different ways , How to filter data based on tags in a single timestamp or over a period of time , And how to use mobile time to select data .

 

 

Filter indicator name


The simplest PromQL Query is to directly select the sequence with the specified indicator name , for example , The following query will return all with indicator names  demo_api_request_duration_seconds_count  Sequence :

demo_api_request_duration_seconds_count

The query will Returns a number of sequences with the same indicator name , But there are different label combinations instance、job、method、path and status etc. . The output is as follows :

 

 

Filter by tag


If we only query  demo_api_request_duration_seconds_count  There is  method="GET"  The index sequence of the tag , You can add this filter condition with braces after the indicator name :

demo_api_request_duration_seconds_count{method="GET"}



{__name__="demo_api_request_duration_seconds_count",instance="192.168.100.5:10000",method="GET",status="200"}

In addition, we You can also use commas to combine multiple label matchers

demo_api_request_duration_seconds_count{instance="demo-service-0:10000",method="GET",job="demo"}

Above will get demo Under the task  demo-service-0:10000  The instance And  method="GET"  Index series data :

It should be noted that when multiple matching conditions are combined , It is to filter the time series that all conditions are satisfied .

In addition to equal matching ,Prometheus Several other matcher types are also supported :

  • !=: It's not equal to
  • =~: Regular Expression Matching
  • !~: Regular expressions don't match

Even we can omit the indicator name completely , For example, directly query all  path  Label with  /api  All sequences at the beginning :

{path=~"/api.*"}

The query will get some sequences with different indicator names :

Be careful : Prometheus Regular expressions in always match against complete strings rather than partial strings . therefore , Match any with  /api  When opening the path , There is no need to  ^  start , But you need to add at the end  .*, This will match  path="/api"  This sequence .

We said before that in Prometheus Inside , The index name is essentially a name  __name__  The characteristic label of , So search  demo_api_request_duration_seconds_count  In fact, it is equivalent to the following query method :

{__name__="demo_api_request_duration_seconds_count"}

The selector written in the above way , You can get one Instantaneous vector , It contains a single... Of all the selected sequences Latest value .

In fact, some functions require you not to pass a single value , Instead, it passes a sequence of values over a period of time , That is, the interval vector . At this time, we can add a [< Numbers >< Company >] Duration specifier of form , Change the immediate vector selector to the range vector selector ( for example [5m] Express 5 minute ).

For example, to query the latest 5 Minutes of available memory , You can execute the following query statement :

demo_memory_usage_bytes{type="free"}[5m]

You will get the query result shown below : This is the interval vector , It contains multiple samples , Interval vectors cannot go graph To draw , That is, at the same time

The valid time units that can be used are :

  • ms - millisecond
  • s - second
  • m - minute
  • h - Hours
  • d - God
  • y - year

Sometimes we need to access past data in a time-lapse way , Usually used to compare with current data . To move past data to the current location , have access to  offset <duration>  Modifiers are added to any range or immediate sequence selector to query ( for example  my_metric offset 5m  or  my_metric[1m] offset 7d).

for example , To select the memory available one hour ago , You can use the following query statement :

demo_memory_usage_bytes{type="free"} offset 1h

At this time, the query value is the data one hour ago :

practice :

1. Build a query , Select all time series .

{job!=""}

perhaps :

{__name__=~".+"}

2. Build a query , Query all indicators whose names are  demo_api_request_duration_seconds_count  also  method  Label is not  POST  Sequence .

demo_api_request_duration_seconds_count{method!="POST"}

3. Use  demo_memory_usage_bytes  Index query one hour ago 1 Available in the minute time range Free Memory .

demo_memory_usage_bytes{type="free"}[1m] offset 1h

 

原网站

版权声明
本文为[Foxconn quality inspector zhangquandan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202160333322743.html