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:
Create a Google Cloud project. You can create one here.
Set up Application Default Credentials by installing gcloud and running
gcloud auth application-default login
.Setup a Google tool like Google Compute Engine (GCE) or Google Kubernetes Engine (GKE).
Run the below example in the Google tool.
Installation
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 BatchSpanProcessor
from opentelemetry.resourcedetector.gcp_resource_detector import (
GoogleCloudResourceDetector,
)
resource = 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=resource))
# Cloud Trace exporter will automatically format these resources and export
cloud_trace_exporter = CloudTraceSpanExporter(
# send all resource attributes
resource_regex=r".*"
)
trace.get_tracer_provider().add_span_processor(
BatchSpanProcessor(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 MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.sdk.resources import get_aggregated_resources
from opentelemetry.resourcedetector.gcp_resource_detector import (
GoogleCloudResourceDetector,
)
resource = get_aggregated_resources(
[GoogleCloudResourceDetector(raise_on_error=True)]
)
meter_provider = MeterProvider(
resource=resource,
metric_readers=[
PeriodicExportingMetricReader(
CloudMonitoringMetricsExporter(), export_interval_millis=5000
)
],
)
metrics.set_meter_provider(meter_provider)
meter = metrics.get_meter(__name__)
requests_counter = meter.create_counter(
name="request_counter_with_resource",
description="number of requests",
unit="1",
)
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