Collecting new metrics

Istio provides a simple mechanism for collecting metrics for the microservices that we developed without adding any instrumentation to them. In the following example, we will use Mixer's attribute vocabulary to define an instance of Mixer metrics that can be applied to the bookinfo microservices to generate metrics. Then, we'll collect them without having to make any code changes to the application. Let's get started:

  1. Define the configuration for the metric instance to double the request count:
# Script : 02-create-metric-instance.yaml

apiVersion: config.istio.io/v1alpha2
kind: instance
metadata:
name: doublerequestcount
spec:
compiledTemplate: metric
params:
value: "2" # count each request twice
dimensions:
reporter: conditional((context.reporter.kind | "inbound") == "outbound", "client", "server")
source: source.workload.name | "unknown"
destination: destination.workload.name | "unknown"
message: '"twice the fun!"'
monitored_resource_type: '"UNSPECIFIED"'

In the preceding example, any request originating from the source attribute (source.workload.name) to the destination attribute (destination.workload.name) or the context attribute (context.reporter.kind can be either outbound, client, or server) will be counted as 2. 

  1. Create a metric instance:
$ kubectl -n istio-system apply -f 02-create-metric-instance.yaml 
instance.config.istio.io/doublerequestcount created
  1. Define the configuration for a Prometheus handler:
# Script : 03-create-prometheus-handler.yaml

apiVersion: config.istio.io/v1alpha2
kind: handler
metadata:
name: doublehandler
spec:
compiledAdapter: prometheus
params:
metrics:
- name: double_request_count # Prometheus metric name
instance_name: doublerequestcount.instance.istio-system # Mixer instance name (fully-qualified)
kind: COUNTER
label_names:
- reporter
- source
- destination
- message
  1. Create a Prometheus handler using the double request counter we created previously:
$ kubectl -n istio-system apply -f 03-create-prometheus-handler.yaml 
handler.config.istio.io/doublehandler create

  1. Define a rule to send metric data to the Prometheus handler:
# Script : 04-create-rule-to-send-metric-to-prometheus.yaml

apiVersion: config.istio.io/v1alpha2
kind: rule
metadata:
name: doubleprom
spec:
actions:
- handler: doublehandler
instances: [ doublerequestcount ]
  1. Create the rule:
$ kubectl -n istio-system apply -f 04-create-rule-to-send-metric-to-prometheus.yaml 
rule.config.istio.io/doubleprom created
  1. Let's launch the Prometheus UI. From the browser within the VM, launch http://prometheus.istio.io. The GUI should open.
  2. Refresh http://bookinfo.istio.io in your browser a few times to generate the required metrics.
  3. Switch to the Prometheus Web UI at http://prometheus.istio.io.

In the Expression input box at the top of the web page, enter istio_double_request_count and click the Execute button.

In the Console tab, you will see the entries that were logged after we refreshed the productpage:

istio_double_request_count{destination="details-v1",instance="10.1.230.250:42422",job="istio-mesh",message="twice the fun!",reporter="client",source="productpage-v1"} 2
istio_double_request_count{destination="details-v1",instance="10.1.230.250:42422",job="istio-mesh",message="twice the fun!",reporter="server",source="productpage-v1"} 2
istio_double_request_count{destination="istio-policy",instance="10.1.230.250:42422",job="istio-mesh",message="twice the fun!",reporter="server",source="details-v1"} 2

The same metric collection logs can be seen through the Prometheus web UI:

This exercise demonstrates that Istio allows us to add our metrics with the use of instance, handler, and rule. These metrics are scrapped by Prometheus for analysis. We didn't have to add any code to the applications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.41.187