# Set Per-route Sidecar Filters

The Grey Matter Route object offers the ability to configure [filters](../usage/fabric/filters) on specific routes of a domain. Read the reference documentation on per route filter configuration [here](../../usage/fabric/traffic_control/routing#per-route-filter-configuration).

This guide will walk through the steps of setting two different configurations of the Grey Matter [observables filter](../usage/fabric/filters/http/gm-observables) on different routes. To demonstrate this, [step 1](#1-deploy-and-configure-a-fibonacci-service) provides the files to deploy a fibonacci service and configure it into the mesh for testing. For a step by step guide on service deployment, see the [Quickstart Launch Service to Kubernetes](launch-service-k8s) or the [Service Deployment guide](https://github.com/DecipherNow/gm-gitbook-sync/tree/a805f02c4b27423541dedfee0ac43f1c1f4b534f/guides/grey-matter-service-deployment-training.md). To test the per filter route configuration for observables on a service already existing in the mesh, skip to [step 2](#2-enable-the-grey-matter-observables-filter).

## Prerequisites

1. An existing Grey Matter deployment following the [Grey Matter Quickstart Install with Helm/Kubernetes](installation-kubernetes) guide.
   1. This guide assumes you've followed the step to configure a load balancer for ingress access to the mesh. If so, you should have a URL to access the Grey Matter Intelligence 360 application (e.g. `a2832d300724811eaac960a7ca83e992-749721369.us-east-1.elb.amazonaws.com:10808`).
   2. Make note of your load balancer ingress URL and use it wherever this guide references `{your-gm-ingress-url}`.
2. [greymatter](commands-cli) setup with a running Fabric mesh.

## Overview

1. Deploy and configure a Fibonacci service
2. Enable the Grey Matter observables filter
3. Enable per filter route configurations on a specific route

## Steps

### 1. Deploy and configure a fibonacci service

Deploy and configure a fibonacci service following the [Quickstart Launch Service to Kubernetes Guide](launch-service-k8s).

> If you are using a non-SPIFFE/SPIRE deployment, follow [this guide](https://github.com/DecipherNow/gm-gitbook-sync/tree/b88085226e88844739eb923e6a3b5b82bf4294b6/guides/launch-svc-k8s-no-spire.md) instead.

Before you move on to the next step of this guide, you should be able to see `Fibonacci` on the dashboard and successfully connect to the service. The fibonacci service route will be at `https:///{your-gm-ingress-url}:30000/services/fibonacci/`. You should see `Alive` when you navigate to this route in a browser. Try `https:///{your-gm-ingress-url}:30000/services/fibonacci/fibonacci/37` to get the 37th fibonacci in response, `24157817`.

### 2. Enable the Grey Matter observables filter

Now we will turn on the observables filter for the Fibonacci service. The configuration that we add to the `fibonacci-listener` object will enabled observables on all of the routes from it's domain. Make sure that the [greymatter CLI](https://github.com/DecipherNow/gm-gitbook-sync/tree/b88085226e88844739eb923e6a3b5b82bf4294b6/guides/commands-cli/README.md) is set up.

```bash
greymatter edit listener fibonacci-listener
```

Add the following configuration

```javascript
  "active_http_filters": [
    "gm.observables"
  ],
  "http_filters": {
    "gm_observables": {
      "topic": "fibonacci-topic",
      "eventTopic": "fibonacci-event-topic",
      "logLevel": "debug"
    }
  }
```

Check the logs `kubectl logs deployment/fibonacci sidecar -f` and hit the fibonacci endpoint `https://{your-gm-ingress-url}/services/fibonacci/` in a browser, you should see something that looks like the following:

```bash
6:05PM DBG Message publishing to STDOUT; emitFullResponse = false
 Encryption= EncryptionKeyID=0 Filter=Observables Topic=fibonacci-topic
```

followed by the observable string of JSON.

### 3. Configure and test two different route filter configurations

Now we will configure and test per route filter configurations. To do this, we'll add a route from the fibonacci sidecar to the fibonacci service.

The route looks for the `/fibonacci/37` path, so any request into the mesh at `https://{your-gm-ingress-url}/services/fibonacci/fibonacci/37`, looking for exactly the 37th fibonacci number, will use this route.

Per route filter configurations are configured by the [`filter_metadata`](../../usage/fabric/api/route#filter-metadata) field of the route object. The filter configuration we will make for this route specifically is to turn `emitFullResponse` in the observables filter to true. This means that for any request to `https://{your-gm-ingress-url}/services/fibonacci/fibonacci/37`, the full response body will be printed in the observable.

Save this route in the /mesh directory as `fibonacci-route-37.json`

```javascript
{
  "zone_key": "zone-default-zone",
  "domain_key": "fibonacci-domain",
  "route_key": "fibonacci-route-37",
  "route_match": {
    "path": "/fibonacci/37",
    "match_type": "exact"
  },
  "filter_metadata": {
    "gm.observables": [
      {
        "key": "emitFullResponse",
        "value": "true"
      }
    ]
  },
  "prefix_rewrite": "",
  "shared_rules_key": "fibonacci-rules"
}
```

```bash
greymatter create route < mesh/fibonacci-route-37.json
```

Follow the logs again `kubectl logs deployment/fibonacci sidecar -f`.

First, hit any endpoint that isn't the 37th fibonacci number, try `https://{your-gm-ingress-url}/services/fibonacci/`, or `https://{your-gm-ingress-url}/services/fibonacci/fibonacci/18`. Since neither of those requests have path exactly equal to `/fibonacci/37`, neither will correspond to the `fibonacci-route-37` route. The observable printed for both of those requests in the fibonacci logs should be preceded with:

```bash
6:46PM DBG Message publishing to STDOUT; emitFullResponse = false
 Encryption= EncryptionKeyID=0 Filter=Observables Topic=fibonacci-topic
```

Now, watch the logs and make a request to the `fibonacci-route-37` route - `https://{your-gm-ingress-url}/services/fibonacci/fibonacci/37`. The observable JSON string should this time be preceded with:

```bash
6:47PM DBG DecodeHeaders: route based config: changing emitFullResponse from false to true Encryption= EncryptionKeyID=0 Filter=Observables Topic=fibonacci-topic
6:47PM DBG Message publishing to STDOUT; emitFullResponse = true
 Encryption= EncryptionKeyID=0 Filter=Observables Topic=fibonacci-topic
```

Notice the `route based config` log, as well as the `emitFullResponse = true` for this route only. If you inspect the JSON of the observable you will also see thar it contains a field `"body"`, with value `"24157817\n"`, the full body of the response.
