|
| 1 | +# OpenCensus Stackdriver Stats Exporter |
| 2 | + |
| 3 | +The *OpenCensus Stackdriver Stats Exporter* is a stats exporter that exports |
| 4 | +data to [Stackdriver Monitoring](stackdriver-monitoring). |
| 5 | + |
| 6 | +## Quickstart |
| 7 | + |
| 8 | +### Prerequisites |
| 9 | + |
| 10 | +The Stackdriver exporter can be used from any internet-connected application, |
| 11 | +whether on Google Cloud Platform, another cloud platform, or on-premise. |
| 12 | + |
| 13 | +In order to be able to push your stats to [Stackdriver Monitoring](stackdriver-monitoring), you must: |
| 14 | +1. [Create a Cloud project](https://support.google.com/cloud/answer/6251787?hl=en). |
| 15 | +2. [Enable billing](https://support.google.com/cloud/answer/6288653#new-billing). |
| 16 | +3. [Enable the Stackdriver Monitoring API](https://app.google.stackdriver.com/). |
| 17 | +4. [Make sure you have a Premium Stackdriver account](https://cloud.google.com/monitoring/accounts/tiers). |
| 18 | + |
| 19 | +These steps enable the API but don't require that your app is hosted on Google Cloud Platform. |
| 20 | + |
| 21 | +### Setup authentication |
| 22 | +The Stackdriver exporter uses gRPC, which requires a certificate |
| 23 | +(`etc/roots.pem` in the gRPC repository) copied to |
| 24 | +to `/usr/share/grpc/roots.pem`. |
| 25 | + |
| 26 | +If your application runs on Google Cloud Platform, it can automatically |
| 27 | +determine credentials to authenticate to Stackdriver from the VM environment. |
| 28 | +Otherwise, create a |
| 29 | +[Google Cloud service account](https://cloud.google.com/iam/docs/creating-managing-service-accounts) |
| 30 | +with the "Monitoring Editor" role, create and download a service account key, |
| 31 | +and set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` to the path to |
| 32 | +that key. |
| 33 | + |
| 34 | +### Register the exporter |
| 35 | + |
| 36 | +`#include opencensus/exporters/stats/stackdriver/stackdriver_exporter.h` (if |
| 37 | +using Bazel, this requires a dependency on |
| 38 | +`"@io_opencensus_cpp//exporters/stats/stackdriver:stackdriver_exporter"`). |
| 39 | +In your application's initialization code, register the exporter: |
| 40 | +```c++ |
| 41 | +const char* hostname = getenv("HOSTNAME"); |
| 42 | +if (hostname == nullptr) hostname = "hostname"; |
| 43 | +const std::string opencensus_task = |
| 44 | + absl::StrCat("cpp-", getpid(), "@", hostname); |
| 45 | +opencensus::exporters::stats::StackdriverExporter::Register( |
| 46 | + "my-stackdriver-project-id", opencensus_task); |
| 47 | +``` |
| 48 | +The `opencensus_task` may be anything, but must be unique among all exporters |
| 49 | +simultaneously exporting to Stackdriver concurrently; the format |
| 50 | +`"cpp-${PROCESS_ID}@${HOSTNAME}"` is recommended. |
| 51 | +
|
| 52 | +### Register views and record stats |
| 53 | +
|
| 54 | +Once the exporter has been registered, any stats for views registered with |
| 55 | +`ViewDescriptor::RegisterForExport()` will be exported. Views may be registered |
| 56 | +before or after the exporter, and will track stats since the registration of the |
| 57 | +view in either case. |
| 58 | +
|
| 59 | +## Data model |
| 60 | +
|
| 61 | +### View metadata |
| 62 | +
|
| 63 | +OpenCensus exports views as custom metrics under the |
| 64 | +[Global](https://cloud.google.com/monitoring/api/resources#tag_global) |
| 65 | +monitored resource. For each view, OpenCensus creates a Stackdriver metric. This |
| 66 | +metric's name will be under the path `"custom.googleapis.com/opencensus/"`, so |
| 67 | +that, for example, a view named `"example.com/client/latency"` would translate |
| 68 | +to a Stackdriver metric named |
| 69 | +`"custom.googleapis.com/opencensus/example.com/client/latency"`. |
| 70 | +
|
| 71 | +Only Cumulative views may be registered for export in OpenCensus, and created |
| 72 | +Stackdriver metrics have the `CUMULATIVE` type. |
| 73 | +
|
| 74 | +View columns translate to Stackdriver's labels. OpenCensus adds a label |
| 75 | +`"opencensus_task"` to all exported views so that exports from different |
| 76 | +processes do not conflict. |
| 77 | +
|
| 78 | +### Data |
| 79 | +
|
| 80 | +For each row of the view's data (a unique combination of tag values) OpenCensus |
| 81 | +exports a separate `TimeSeries` with label values corresponding to the row's tag |
| 82 | +values. |
| 83 | +
|
| 84 | +The Stackdriver data type depends on the |
| 85 | +view's measure type and aggregation--count aggregation to `INT64`, sum |
| 86 | +aggregation to `INT64` for `MeasureInt` and `DOUBLE` for `MeasureDouble`, and |
| 87 | +distribution aggregation to `DISTRIBUTION`. Exported distributions omit the |
| 88 | +range as it is not supported by Stackdriver. |
| 89 | +
|
0 commit comments