Beispiel #1
0
    def test_finding_gke_resources(self, getter):
        # The necessary env variables were set for GKE resource detection
        # to succeed. No GCE resource info should be extracted

        os.environ[NAMESPACE] = "namespace"
        os.environ[CONTAINER_NAME] = "container_name"
        os.environ[HOSTNAME] = "host_name"

        resource_finder = GoogleCloudResourceDetector()
        getter.return_value.json.return_value = GKE_RESOURCES_JSON_STRING
        found_resources = resource_finder.detect()
        self.assertEqual(getter.call_args_list[0][0][0], _GCP_METADATA_URL)
        self.assertEqual(
            found_resources,
            Resource(
                attributes={
                    "cloud.account.id": "project_id",
                    "k8s.cluster.name": "cluster_name",
                    "k8s.namespace.name": "namespace",
                    "host.id": "instance_id",
                    "k8s.pod.name": "host_name",
                    "container.name": "container_name",
                    "cloud.zone": "zone",
                    "cloud.provider": "gcp",
                    "gcp.resource_type": "gke_container",
                }
            ),
        )
        self.assertEqual(getter.call_count, 1)
Beispiel #2
0
    def test_finding_gce_resources(self, getter):
        # The necessary env variables were not set for GKE resource detection
        # to succeed. We should be falling back to detecting GCE resources
        resource_finder = GoogleCloudResourceDetector()
        getter.return_value.json.return_value = GCE_RESOURCES_JSON_STRING
        found_resources = resource_finder.detect()
        self.assertEqual(getter.call_args_list[0][0][0], _GCP_METADATA_URL)
        self.assertEqual(
            found_resources,
            Resource(
                labels={
                    "host.id": "instance_id",
                    "cloud.provider": "gcp",
                    "cloud.account.id": "project_id",
                    "cloud.zone": "zone",
                    "gcp.resource_type": "gce_instance",
                }),
        )
        self.assertEqual(getter.call_count, 1)

        # Found resources should be cached and not require another network call
        found_resources = resource_finder.detect()
        self.assertEqual(getter.call_count, 1)
        self.assertEqual(
            found_resources,
            Resource(
                labels={
                    "host.id": "instance_id",
                    "cloud.provider": "gcp",
                    "cloud.account.id": "project_id",
                    "cloud.zone": "zone",
                    "gcp.resource_type": "gce_instance",
                }),
        )
    def test_finding_resources(self, getter):
        resource_finder = GoogleCloudResourceDetector()
        getter.return_value.json.return_value = RESOURCES_JSON_STRING
        found_resources = resource_finder.detect()
        self.assertEqual(getter.call_args_list[0][0][0], _GCP_METADATA_URL)
        self.assertEqual(
            found_resources,
            Resource(
                labels={
                    "host.id": "instance_id",
                    "cloud.provider": "gcp",
                    "cloud.account.id": "project_id",
                    "cloud.zone": "zone",
                    "gcp.resource_type": "gce_instance",
                }),
        )
        self.assertEqual(getter.call_count, 1)

        # Found resources should be cached and not require another network call
        found_resources = resource_finder.detect()
        self.assertEqual(getter.call_count, 1)
        self.assertEqual(
            found_resources,
            Resource(
                labels={
                    "host.id": "instance_id",
                    "cloud.provider": "gcp",
                    "cloud.account.id": "project_id",
                    "cloud.zone": "zone",
                    "gcp.resource_type": "gce_instance",
                }),
        )
Beispiel #4
0
    def test_resource_finding_fallback(self, getter):
        # The environment variables imply its on GKE, but the metadata doesn't
        # have GKE information
        getter.return_value.json.return_value = GCE_RESOURCES_JSON_STRING
        os.environ[CONTAINER_NAME] = "container_name"

        # This detection will cause an error in get_gke_resources and should
        # swallow the error and fall back to get_gce_resources
        resource_finder = GoogleCloudResourceDetector()
        found_resources = resource_finder.detect()
        self.assertEqual(
            found_resources,
            Resource(
                labels={
                    "host.id": "instance_id",
                    "cloud.provider": "gcp",
                    "cloud.account.id": "project_id",
                    "cloud.zone": "zone",
                    "gcp.resource_type": "gce_instance",
                }),
        )
# 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_metric(
    name="request_counter_with_resource",
    description="number of requests",
    unit="1",
    value_type=int,
    metric_type=Counter,
)
Beispiel #6
0
 def test_no_resources_found(self, getter):
     # If no Google resources were found, we throw an exception
     getter.return_value.json.side_effect = Exception
     resource_finder = GoogleCloudResourceDetector()
     self.assertRaises(NoGoogleResourcesFound, resource_finder.detect)
Beispiel #7
0
from opentelemetry import trace
from opentelemetry.sdk.resources import get_aggregated_resources
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    ConsoleSpanExporter,
    SimpleExportSpanProcessor,
)
from opentelemetry.tools.resource_detector import GoogleCloudResourceDetector

resources = get_aggregated_resources([GoogleCloudResourceDetector()])

trace.set_tracer_provider(TracerProvider(resource=resources))

trace.get_tracer_provider().add_span_processor(
    SimpleExportSpanProcessor(ConsoleSpanExporter())
)
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("foo"):
    print("Hello world!")