Cloud Resources Detector Example

These examples show how to use OpenTelemetry to detect resource information and send it to Cloud Trace or Cloud Monitoring.

Basic Example

To use this feature you first need to:

pip install opentelemetry-api \
  opentelemetry-sdk \
  opentelemetry-exporter-gcp-trace \
  opentelemetry-exporter-gcp-monitoring \
  opentelemetry-resourcedetector-gcp
  • Run an example on the Google tool of your choice

#!/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.

from opentelemetry import trace
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
from opentelemetry.sdk.resources import get_aggregated_resources
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.tools.resource_detector import GoogleCloudResourceDetector

# MUST be run on a Google tool!
# Detect resources from the environment
resources = get_aggregated_resources(
    [GoogleCloudResourceDetector(raise_on_error=True)]
)

# Pass the detected resources to the provider, which will in turn pass it to all
# created spans
trace.set_tracer_provider(TracerProvider(resource=resources))

# Cloud Trace exporter will automatically format these resources and export
cloud_trace_exporter = CloudTraceSpanExporter()
trace.get_tracer_provider().add_span_processor(
    SimpleSpanProcessor(cloud_trace_exporter)
)
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("foo"):
    print("Hello world!")
#!/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 Counter, MeterProvider
from opentelemetry.sdk.resources import get_aggregated_resources
from opentelemetry.tools.resource_detector import GoogleCloudResourceDetector

# MUST be run on a Google tool!
# Detect resources from the environment
resources = get_aggregated_resources(
    [GoogleCloudResourceDetector(raise_on_error=True)]
)

metrics.set_meter_provider(MeterProvider(resource=resources))
meter = metrics.get_meter(__name__)
metrics.get_meter_provider().start_pipeline(
    meter, CloudMonitoringMetricsExporter(), 5
)

requests_counter = meter.create_counter(
    name="request_counter_with_resource",
    description="number of requests",
    unit="1",
    value_type=int,
)

staging_labels = {"environment": "staging"}

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

Checking Output

After running the metrics example:

  • Go to the Cloud Monitoring Metrics Explorer page.

  • In “Find resource type and metric” enter “OpenTelemetry/request_counter_with_resource”.

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

Or, if you ran the tracing example, you can go to Cloud Trace overview to see the results.

Troubleshooting

gke_container resources are not being detected or exported:

You need to manually pass in some information via the Downward API to enable GKE resource detection. Your kubernetes config file should look something like this (passing in NAMESPACE, CONTAINER_NAME, POD_NAME)

apiVersion: "apps/v1"
kind: "Deployment"
metadata:
  name: "food-find"
spec:
  replicas: 3
  selector:
    matchLabels:
      app: "food-find"
  template:
    metadata:
      labels:
        app: "food-find"
    spec:
      terminationGracePeriodSeconds: 30
      containers:
      - name: "food-finder"
        image: "gcr.io/aaxue-gke/food-finder:v1"
        imagePullPolicy: "Always"
        env:
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: CONTAINER_NAME
              value: "food-finder"
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
        ports:
        - containerPort: 8080
      hostNetwork: true
      dnsPolicy: Default