2

I'm trying to define the E2E access availability of a service from a user PoV as the result of applying the bit-wise AND operation between two boolean instant vectors representing individual service availability.

I'm fetching the probe_success results of pinging these two services (e.g. S1 and S2) from a "user node" with the blackbox_exporter preinstalled:

            +-------+     +---------------------+     +-------+
            |S1     |     |                     |     |     S2|
            |       |     |   User node with    |     |       |
            |       +<----+  blackbox_exporter  +---->+       |
            |       | icmp|                     |icmp6|       |
            +-------+     +---------------------+     +-------+

Since the targets are probed using different modules (icmp and icmp6) I'm using two jobs and therefore getting two instant vectors:

probe_success{job="S1", user="foo"}
# AND
probe_success{job="S2", user="foo"}

As I read on Prometheus (querying) logical operators docs, that logical and is not exactly what I'm looking for. ¿Is there an easy way to perform these kind of "bit-wise" operations in PromQL?

3 Answers 3

3

Because the cardinals between probe_success{job="S1", user="foo"} and probe_success{job="S2", user="foo"} are not the same, the logical and operator will not result in a vector/scalar with your desired effect.

There is no less complex way of achieving it. I would use something like this:

(scalar(probe_success{job="S1", user="foo"}) + scalar(probe_success{job="S2", user="foo"})) == bool(2)

scalar converts the single element input vector into a scalar. probe_success handily is a single element input vector, if absolute cardinals are used. For example, the same expression will not work if used with probe_success{user="foo"} as it will result in multiple element vectors.

+ operator obviously adds the two scalars

bool() is used because Prometheus expects that the bool modifier must be provided between 2 scalars to be compared, in this case with a == operator. Refer here about this.

4
  • Awesome; this is basically what I was trying to do. However since this will result in a scalar itself; not an instant vector - I cannot apply aggregation operators like avg() to get the average SLA of the joint service. Tthe graph view shows the time series I'm trying to average i.imgur.com/0ZwTo4D.png - any way to put all these results back into an instant vector?
    – Alfageme
    Dec 3, 2019 at 14:45
  • 1
    One approach would be to use Recording rules. With this, you can add any labels as part of the rules, which will result in a vector. Not sure if that is what you're trying to achieve though.
    – droidbot
    Dec 3, 2019 at 15:07
  • It is indeed what I'm trying to achieve - but since scalar() drops all the original labels form probe_success. I'd need to define one recording rule per user. Isn't that right?
    – Alfageme
    Dec 3, 2019 at 16:26
  • Yes, that's right.. but I'm sure there are better ways around depending on your particular case
    – droidbot
    Dec 3, 2019 at 18:31
1

A subtle variation to preserve the labels:

sum(probe_success{job~"S[12]", user="foo"})by(user)== bool(2) 
-1

The and operator can be used for this task in the following way:

(probe_success{job="S1", user="foo"} == 1)
  and ignoring(job)
(probe_success{job="S2", user="foo"} == 1)

It uses ignoring() modifier for ignoring the job label when searching for pairs of time series with the same set of labels on the left and the right side of and - see these docs for details.

It has the following advantages comparing to other methods:

  • It preserves all the labels for probe_success metric.
  • A single query can be used for returning success probes across multiple users:
(probe_success{job="S1", user=~"foo|bar"} == 1)
  and ignoring(job)
(probe_success{job="S2", user=~"foo|bar"} == 1)

or across all the users:

(probe_success{job="S1"} == 1)
  and ignoring(job)
(probe_success{job="S2"} == 1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.