Newbie having trouble with regex escaping in blackbox exporter

25 views
Skip to first unread message

TommyG

unread,
Mar 7, 2020, 11:49:20 AM3/7/20
to Prometheus Users
(repost because I made a mess of the formatting last time)

Hi folks

New to Prometheus but liking it so far.

I'm trying to use the blackbox exporter to test an API. I want to check the contents of the response body but I'm having trouble with regex that may or may not be related to escaping the | character.

I'm sending in HL7 messages and getting a HL7 response, they make extensive use of the pipe character.

My blackbox.yaml 

  http_post_2xx:
    prober
: http
    http
:
      preferred_ip_protocol
: "ip4"
      method
: POST
  http_post_json
:
    prober
: http
    timeout
: 5s
    http
:
      method
: POST
      preferred_ip_protocol
: "ip4"
      headers
:
       
Content-Type: application/json
      body
: "MSH|^~\\&|MLEMS|TEST|MLEMS|TEST|20200102101206||ADT^A31|123456|P|2.4\n <deletia>"
      fail_if_body_not_matches_regexp
:
       
- .*MSA|AE|123456$


The actual response body is plaintext as shown below
 
MSH|^~\&|EMS|RA9|PAS|RA9|20200307152450.723+0000||ACK^A31|3352|P|2.4
MSA
|AA|123456


I'm only interested in the last line, specifically MSA|AA|123456

My problem is that this regex passes (which is fine) but if I change the |AA| to |AE| i.e. 

fail_if_body_not_matches_regexp:
       
- .*MSA|AE|123456$


A successful response i.e. one ending MSA|AA|123456 still passes????

I've tried escaping the | characters i.e. 

fail_if_body_not_matches_regexp:
       
- .*MSA\|AE\|123456$


But then all messages fail.

I've tried various combinations of using double and single quotes but haven't had any success although there's a fair chance I missed the right combo.
 
Any help much appreciated

Harald Koch

unread,
Mar 7, 2020, 12:02:51 PM3/7/20
to Prometheus Users
On Sat, Mar 7, 2020, at 11:49, TommyG wrote:

fail_if_body_not_matches_regexp:
       
- .*MSA|AE|123456$

This matches anything containing "MSA" or "AE" or "123456", which is why it's matching the AA response also.


fail_if_body_not_matches_regexp:
       
- .*MSA\|AE\|123456$

My first suspicion would be that CR is used by most systems as a segment terminator, so there's a CR at the end of the MSA segment. Either remove the $ or add a ".*" before it?


(Also it's a tiny thing, but in your headers the Content-Type: should be x-application/hl7-v2+er7 :)

hth,

--
Harald

TommyG

unread,
Mar 7, 2020, 12:48:40 PM3/7/20
to Prometheus Users
That was exactly what it was.

Many thanks for the answer and the explanation.


On Saturday, 7 March 2020 17:02:51 UTC, Harald Koch wrote:

fail_if_body_not_matches_regexp:
       
- .*MSA\|AE\|123456$

Harald Koch

unread,
Mar 7, 2020, 1:02:29 PM3/7/20
to Prometheus Users


On Sat, Mar 7, 2020, at 12:48, TommyG wrote:
That was exactly what it was.

Many thanks for the answer and the explanation.

Your timing was perfect - I've spent the last couple of weeks replacing the ACK generator in our engine, so it's all fresh in my brain!

--
Harald

Reply all
Reply to author
Forward
0 new messages