# Google Cloud Monitoring exporter example

This example shows how to use [`go.opentelemetry.io/otel`](https://pkg.go.dev/go.opentelemetry.io/otel/) to instrument a simple Go application with metrics and export the metrics to [Google Cloud Monitoring](https://cloud.google.com/monitoring/)

## Setup environment

Before sending metrics to Google Cloud Monitoring, confirm the Cloud Monitoring API is enabled for the project you will use to access the API as described in [this document](https://cloud.google.com/monitoring/api/enable-api).

You can find your current project ID using the command:

```
$ gcloud config get-value project
Your active configuration is: [example]
foo-project-214354
```

In this case, the project ID is `foo-project-214354`.

Next, set this project ID to the environment variable `GOOGLE_PROJECT_ID` using the following command:

```
export GOOGLE_PROJECT_ID=$(gcloud config get-value project)
```

### Enable experimental features
The `exponential_histogram` example can optionally export [exemplars](https://opentelemetry.io/docs/specs/otel/metrics/data-model/#exemplars). Enable experimental [exemplar sampling](https://github.com/open-telemetry/opentelemetry-go/blob/main/sdk/metric/internal/x/README.md#exemplars) support with :
```
export OTEL_GO_X_EXEMPLAR=true
export OTEL_METRICS_EXEMPLAR_FILTER=always_on
```

## Build and run the application

Once you ensure the API is enabled, then build the example application and run the executable. There are three separate examples showcasing the following - 
1. Exporting metrics via the SDK - [sdk](./sdk/) directory.
2. Exporting metrics via the OpenTelemetry Collector - [collector](./collector/).
3. Exporting histogram metrics via the SDK - [exponential_histogram](./exponential_histogram/).

Change the current directory to the example you wish to run, e.g. `cd sdk` or `cd exponential_histogram`, and then run the example using following commands:

```
$ go build -o metrics
$ ./metrics
...
```

To ensure you have enough data to create interesting charts for the exported metrics generated by the example, keep the application running for at least five minutes.

*Note: For running the collector example, you need to have a locally running OpenTelemetry Collector, configured using the provided [sample config](./collector/sample-collector-config.yaml). Instructions for running OpenTelemetry Collector on your system can be found [here](https://opentelemetry.io/docs/collector/getting-started/#local).*

## Visualize exported metrics

https://console.cloud.google.com/monitoring/dashboards?project=<your-project-id>

Once you think you have sent sufficient data, then create a dashboard. If you are learning how to use Google Cloud Monitoring, you can follow how to use charts step by step on [this document](https://cloud.google.com/monitoring/charts).

When filling in the **Find resource type and metric box**, use the metric names with the prefix "custom.googleapis.com/opentelemetry/counter-a", "custom.googleapis.com/opentelemetry/observer-a" or with prefix "workload.googleapis.com/latency_" (for example "workload.googleapis.com/latency_a").

Go to the individual example sub-directories [sdk](./sdk/) or [exponential_histogram](./exponential_histogram/) for further instructions on how to `Create a dashboard` to visualize the exported metrics.
