Istio custom metrics counting request with specific header value

#1

Hi all,
I’m trying to create a custom metric that will count all the requests that contain a specific value for a specific header, let say I want to count all the request that in the header “x-my-header” contains “my-specific-value”.
So I’m creating the objects of type metric, prometheus and rule. The prometheus and rule are simple and expose a metric named myvalue_request_count. My issue is related to metric. As I understand, to count all the requests that in “x-my-header” contains “my-specific-value” I have to set the value of 1 whet this condition is true and value of 0 otherwise. But it doesn’t work for me:

...
value: conditional(".*my-specific-value.*".matches(request.headers["x-my-header"]), "1", "0") # count the android users
  dimensions:
    reporter: conditional((context.reporter.kind | "inbound") == "outbound", "client", "server")
    source: source.workload.name | "unknown"
    destination: destination.workload.name | "unknown"
  monitored_resource_type: '"UNSPECIFIED"'
...

But with this value the metric with name istio_myvalue_request_count does not apper in the Prometheus.
If I change the value: "1" then this metric appears, but all the requests are counted.

What am I doing wrong?

1 Like

#2

I have solved this problem by using the match on my rule.
So I have set in the metric value: "1"
And have added the condition to the rule:

...
spec:
  match: '".* my-specific-value.*".matches(request.headers["x-my-header"])' # match only for the android users
  actions:
...
0 Likes

#3

@BishyrS happy to hear you were able to get what you desired. The match clause is indeed a better mechanism for conditionally controlling behavior.

0 Likes