This document describes how to configure your Google Kubernetes Engine deployment
so that you can use Google Cloud Managed Service for Prometheus to collect metrics from the
HTTP requests served by your application. This document describes
how to do the following:

- [Set up your application to export HTTP metrics in a predefined format.](https://docs.cloud.google.com/stackdriver/docs/managed-prometheus/exporters/server/http.md.txt#setup)
- [Configure a PodMonitoring resource](https://docs.cloud.google.com/stackdriver/docs/managed-prometheus/exporters/server/http.md.txt#podmonitoring) for Managed Service for Prometheus to collect the exported metrics.
- [View the **Application** dashboard](https://docs.cloud.google.com/stackdriver/docs/managed-prometheus/exporters/server/http.md.txt#view-dashboard) for your GKE Deployment.

<br />

These instructions apply only if you are using [managed collection](https://docs.cloud.google.com/stackdriver/docs/managed-prometheus/setup-managed.md.txt)
with Managed Service for Prometheus.

## Set up

To collect metrics from your HTTP server that can be displayed on
the **Application** dashboard, you must export the following metrics as
described:

- `http_requests_total` of type `counter`, with the labels `code` and `method`.
- `http_request_duration_seconds_bucket` of type `histogram`, with the label `method`.

<br />

For an example that collects these metrics by using the Prometheus Go
client, see [prometheus-example-app](https://github.com/brancz/prometheus-example-app).

To verify that your HTTP server is emitting metrics on the expected
endpoints, do the following:

1. Set up port forwarding by using the following command:

   ```
   kubectl -n NAMESPACE_NAME port-forward deploy/HTTP_SERVER_DEPLOYMENT_NAME PROMETHEUS_PORT_NUMBER
     
   ```
2. Access `localhost:PROMETHEUS_PORT_NUMBER` by using the browser or the `curl` utility in another terminal session.

<br />

## Define a PodMonitoring resource

For target discovery, the Managed Service for Prometheus Operator
requires a PodMonitoring resource that corresponds to the
HTTP server in the same namespace.

You can use the following PodMonitoring configuration:

```
apiVersion: monitoring.googleapis.com/v1
kind: PodMonitoring
metadata:
  name: my-prometheus-http-app
  labels:
    app.kubernetes.io/name: my-prometheus-http-app
    app.kubernetes.io/part-of: google-cloud-managed-prometheus
spec:
  endpoints:
  - port: web
    scheme: http
    interval: 30s
    path: /metrics
  selector:
    matchLabels:
      app.kubernetes.io/name: prometheus-example-app
```

Ensure that the values of the `port` and `MatchLabels`
fields match those of the HTTP application that you want to
monitor.

To apply configuration changes from a local file, run the following command:

```
kubectl apply -n NAMESPACE_NAME -f FILE_NAME
```

<br />

You can also
[use Terraform](https://docs.cloud.google.com/stackdriver/docs/managed-prometheus/setup-managed.md.txt#terraform-scrape)
to manage your configurations.

## View application metrics

To view request, error-rate, and latency metrics from your HTTP
application, do the following:

1. In the Google Cloud console, go to the **Workloads** page:

   [Go to **Workloads**](https://console.cloud.google.com/kubernetes/workload/overview)

   <br />

   If you use the search bar to find this page, then select the result whose subheading is
   **Kubernetes Engine**.
2. Click a Deployment in the list of workloads. The **Type** column in the list indicates the type of the workload.
3. On the **Deployment details** page, click the **Observability** tab.
4. Select **Application** in the dashboard selector.

<br />

For more information, see [Use application performance metrics](https://docs.cloud.google.com/stackdriver/docs/solutions/gke/app-performance-metrics).

## Troubleshooting

For information about troubleshooting metric ingestion problems
in Managed Service for Prometheus, see
[Problems with collection from exporters](https://docs.cloud.google.com/stackdriver/docs/managed-prometheus/troubleshooting.md.txt#exporter-problems) in [Troubleshooting ingestion-side problems](https://docs.cloud.google.com/stackdriver/docs/managed-prometheus/troubleshooting.md.txt#ingest-problems).