Cloud Monitoring Exporter Example

These examples show how to use OpenTelemetry to send metrics data to Cloud Monitoring.

Basic Example

To use this exporter you first need to:

pip install opentelemetry-exporter-gcp-monitoring \
    opentelemetry-api \
    opentelemetry-sdk
  • Run example code

#!/usr/bin/env python3
# Copyright 2021 The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import time

from opentelemetry import metrics
from opentelemetry.exporter.cloud_monitoring import (
    CloudMonitoringMetricsExporter,
)
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.sdk.resources import Resource

metrics.set_meter_provider(
    MeterProvider(
        metric_readers=[
            PeriodicExportingMetricReader(
                CloudMonitoringMetricsExporter(), export_interval_millis=5000
            )
        ],
        resource=Resource.create(
            {
                "service.name": "basic_metrics",
                "service.namespace": "examples",
                "service.instance.id": "instance123",
            }
        ),
    )
)
meter = metrics.get_meter(__name__)

# Creates metric workload.googleapis.com/request_counter with monitored resource generic_task
requests_counter = meter.create_counter(
    name="request_counter",
    description="number of requests",
    unit="1",
)

staging_labels = {"environment": "staging"}

for i in range(20):
    requests_counter.add(25, staging_labels)
    time.sleep(5)

Viewing Output

After running the example:

  • Go to the Cloud Monitoring Metrics Explorer page.

  • In “Select a Metric” enter “workload.googleapis.com/request_counter”.

  • You can filter by labels and change the graphical output here as well.

Troubleshooting

One or more points were written more frequently than the maximum sampling period configured for the metric

Currently, Cloud Monitoring allows one write every 5 seconds for any unique tuple (metric_name, metric_label_value_1, metric_label_value_2, …). The exporter should rate limit on its own but issues arise if:

  • You are restarting the server more than once every 5 seconds.

  • You have a multiple exporters (possibly on different threads) writing to the same tuple.

For both cases, you can pass add_unique_identifier=True to the CloudMonitoringMetricsExporter constructor. This adds a UUID label_value, making the tuple unique again. For the first case, you can also choose to just wait longer than 5 seconds between restarts.