|
| 1 | +# (c) Copyright IBM Corp. 2021 |
| 2 | +# (c) Copyright Instana Inc. 2021 |
| 3 | + |
| 4 | +""" |
| 5 | +Google Cloud Run Collector: Manages the periodic collection of metrics & snapshot data |
| 6 | +""" |
| 7 | +import os |
| 8 | +from time import time |
| 9 | +import requests |
| 10 | + |
| 11 | +from instana.log import logger |
| 12 | +from instana.collector.base import BaseCollector |
| 13 | +from instana.util import DictionaryOfStan, validate_url |
| 14 | +from instana.collector.helpers.google_cloud_run.process import GCRProcessHelper |
| 15 | +from instana.collector.helpers.google_cloud_run.instance_entity import InstanceEntityHelper |
| 16 | + |
| 17 | + |
| 18 | +class GCRCollector(BaseCollector): |
| 19 | + """ Collector for Google Cloud Run """ |
| 20 | + |
| 21 | + def __init__(self, agent, service, configuration, revision): |
| 22 | + super(GCRCollector, self).__init__(agent) |
| 23 | + logger.debug("Loading Google Cloud Run Collector") |
| 24 | + |
| 25 | + # Indicates if this Collector has all requirements to run successfully |
| 26 | + self.ready_to_start = True |
| 27 | + |
| 28 | + self.revision = revision |
| 29 | + self.service = service |
| 30 | + self.configuration = configuration |
| 31 | + # Prepare the URLS that we will collect data from |
| 32 | + self._gcr_md_uri = os.environ.get("GOOGLE_CLOUD_RUN_METADATA_ENDPOINT", "http://metadata.google.internal") |
| 33 | + |
| 34 | + if self._gcr_md_uri == "" or validate_url(self._gcr_md_uri) is False: |
| 35 | + logger.warning("GCRCollector: GOOGLE_CLOUD_RUN_METADATA_ENDPOINT not in environment or invalid URL. " |
| 36 | + "Instana will not be able to monitor this environment") |
| 37 | + self.ready_to_start = False |
| 38 | + |
| 39 | + self._gcr_md_project_uri = self._gcr_md_uri + '/computeMetadata/v1/project/?recursive=true' |
| 40 | + self._gcr_md_instance_uri = self._gcr_md_uri + '/computeMetadata/v1/instance/?recursive=true' |
| 41 | + |
| 42 | + # Timestamp in seconds of the last time we fetched all GCR metadata |
| 43 | + self.__last_gcr_md_full_fetch = 0 |
| 44 | + |
| 45 | + # How often to do a full fetch of GCR metadata |
| 46 | + self.__gcr_md_full_fetch_interval = 300 |
| 47 | + |
| 48 | + # HTTP client with keep-alive |
| 49 | + self._http_client = requests.Session() |
| 50 | + |
| 51 | + # The fully qualified ARN for this process |
| 52 | + self._gcp_arn = None |
| 53 | + |
| 54 | + # Response from the last call to |
| 55 | + # Instance URI |
| 56 | + self.instance_metadata = None |
| 57 | + |
| 58 | + # Response from the last call to |
| 59 | + # Project URI |
| 60 | + self.project_metadata = None |
| 61 | + |
| 62 | + # Populate the collection helpers |
| 63 | + self.helpers.append(GCRProcessHelper(self)) |
| 64 | + self.helpers.append(InstanceEntityHelper(self)) |
| 65 | + |
| 66 | + def start(self): |
| 67 | + if self.ready_to_start is False: |
| 68 | + logger.warning("Google Cloud Run Collector is missing requirements and cannot monitor this environment.") |
| 69 | + return |
| 70 | + |
| 71 | + super(GCRCollector, self).start() |
| 72 | + |
| 73 | + def __get_project_instance_metadata(self): |
| 74 | + """ |
| 75 | + Get the latest data from the service revision instance entity metadata and store in the class |
| 76 | + @return: Boolean |
| 77 | + """ |
| 78 | + try: |
| 79 | + # Refetch the GCR snapshot data |
| 80 | + self.__last_gcr_md_full_fetch = int(time()) |
| 81 | + headers = {"Metadata-Flavor": "Google"} |
| 82 | + # Response from the last call to |
| 83 | + # ${GOOGLE_CLOUD_RUN_METADATA_ENDPOINT}/computeMetadata/v1/project/?recursive=true |
| 84 | + self.project_metadata = self._http_client.get(self._gcr_md_project_uri, timeout=1, |
| 85 | + headers=headers).json() |
| 86 | + |
| 87 | + # Response from the last call to |
| 88 | + # ${GOOGLE_CLOUD_RUN_METADATA_ENDPOINT}/computeMetadata/v1/instance/?recursive=true |
| 89 | + self.instance_metadata = self._http_client.get(self._gcr_md_instance_uri, timeout=1, |
| 90 | + headers=headers).json() |
| 91 | + except Exception: |
| 92 | + logger.debug("GoogleCloudRunCollector.get_project_instance_metadata", exc_info=True) |
| 93 | + |
| 94 | + def should_send_snapshot_data(self): |
| 95 | + return int(time()) - self.snapshot_data_last_sent > self.snapshot_data_interval |
| 96 | + |
| 97 | + def prepare_payload(self): |
| 98 | + payload = DictionaryOfStan() |
| 99 | + payload["spans"] = [] |
| 100 | + payload["metrics"]["plugins"] = [] |
| 101 | + |
| 102 | + try: |
| 103 | + |
| 104 | + if not self.span_queue.empty(): |
| 105 | + payload["spans"] = self.queued_spans() |
| 106 | + |
| 107 | + self.fetching_start_time = int(time()) |
| 108 | + delta = self.fetching_start_time - self.__last_gcr_md_full_fetch |
| 109 | + if delta < self.__gcr_md_full_fetch_interval: |
| 110 | + return payload |
| 111 | + |
| 112 | + with_snapshot = self.should_send_snapshot_data() |
| 113 | + |
| 114 | + # Fetch the latest metrics |
| 115 | + self.__get_project_instance_metadata() |
| 116 | + if self.instance_metadata is None and self.project_metadata is None: |
| 117 | + return payload |
| 118 | + |
| 119 | + plugins = [] |
| 120 | + for helper in self.helpers: |
| 121 | + plugins.extend( |
| 122 | + helper.collect_metrics(with_snapshot=with_snapshot, instance_metadata=self.instance_metadata, |
| 123 | + project_metadata=self.project_metadata)) |
| 124 | + |
| 125 | + payload["metrics"]["plugins"] = plugins |
| 126 | + |
| 127 | + if with_snapshot: |
| 128 | + self.snapshot_data_last_sent = int(time()) |
| 129 | + except Exception: |
| 130 | + logger.debug("collect_snapshot error", exc_info=True) |
| 131 | + |
| 132 | + return payload |
| 133 | + |
| 134 | + def get_instance_id(self): |
| 135 | + try: |
| 136 | + if self.instance_metadata: |
| 137 | + return self.instance_metadata.get("id") |
| 138 | + except Exception: |
| 139 | + logger.debug("get_instance_id error", exc_info=True) |
| 140 | + return None |
0 commit comments