async def initial_check():
    async with init_gcp_client_session(
    ) as gcp_session, init_dt_client_session() as dt_session:
        token = await create_token(logging_context, gcp_session)
        if token:
            fast_check_result = await FastCheck(
                gcp_session=gcp_session,
                dt_session=dt_session,
                token=token,
                logging_context=logging_context)
            if fast_check_result.projects:
                logging_context.log(
                    f'Monitoring enabled for the following projects: {fast_check_result}'
                )
                instance_metadata = await InstanceMetadata(
                    gcp_session, token, logging_context)
                if instance_metadata:
                    logging_context.log(f'GCP: {instance_metadata}')
                loop.create_task(scheduling_loop(fast_check_result.projects))
            else:
                logging_context.log(
                    "Monitoring disabled. Check your project(s) settings.")
        else:
            logging_context.log(
                f'Monitoring disabled. Unable to acquire authorization token.')
    await gcp_session.close()
    await dt_session.close()
async def try_configure_dynatrace():
    async with init_gcp_client_session(
    ) as gcp_session, init_dt_client_session() as dt_session:
        dashboards_result = await ConfigureDynatrace(
            gcp_session=gcp_session,
            dt_session=dt_session,
            logging_context=logging_context)
async def _loop_single_period(self_monitoring: LogSelfMonitoring,
                              sfm_queue: Queue, context: LoggingContext,
                              instance_metadata: InstanceMetadata):
    try:
        sfm_list = _pull_sfm(sfm_queue)
        if sfm_list:
            async with init_gcp_client_session() as gcp_session:
                context = await _create_sfm_logs_context(
                    sfm_queue, context, gcp_session, instance_metadata)
                self_monitoring = aggregate_self_monitoring_metrics(
                    self_monitoring, sfm_list)
                _log_self_monitoring_data(self_monitoring, context)
                if context.self_monitoring_enabled:
                    if context.token is None:
                        context.log(
                            "Cannot proceed without authorization token, failed to send log self monitoring"
                        )
                        return
                    if not isinstance(context.token, str):
                        context.log(
                            f"Failed to fetch access token, got non string value: {context.token}"
                        )
                        return
                    time_series = create_self_monitoring_time_series(
                        self_monitoring, context)
                    await push_self_monitoring_time_series(
                        context, time_series)
                for _ in sfm_list:
                    sfm_queue.task_done()
    except Exception:
        context.exception("Log SFM Loop Exception:")
async def run_instance_metadata_check() -> Optional[InstanceMetadata]:
    async with init_gcp_client_session() as gcp_session:
        token = await create_token(logging_context, gcp_session)
        if token:
            return await InstanceMetadataCheck(gcp_session, token,
                                               logging_context).execute()
        else:
            logging_context.log(
                f'Instance metadata check skipped. Unable to acquire authorization token.'
            )

    return None
async def import_self_monitoring_dashboards(metadata: InstanceMetadata):
    if metadata:
        async with init_gcp_client_session() as gcp_session:
            token = await create_token(logging_context, gcp_session)
            if token:
                sfm_dashboards_context = SfmDashboardsContext(
                    project_id_owner=get_project_id_from_environment(),
                    token=token,
                    gcp_session=gcp_session,
                    operation_mode=OPERATION_MODE,
                    scheduled_execution_id=None)
                await import_self_monitoring_dashboard(
                    context=sfm_dashboards_context)
async def metrics_pre_launch_check() -> Optional[PreLaunchCheckResult]:
    async with init_gcp_client_session(
    ) as gcp_session, init_dt_client_session() as dt_session:
        token = await create_token(logging_context, gcp_session)
        if not token:
            logging_context.log(
                f'Monitoring disabled. Unable to acquire authorization token.')
        fast_check_result = await metrics_initial_check(
            gcp_session, dt_session, token) if token else None
        extensions_fetch_result = await extensions_fetch(
            gcp_session, dt_session, token) if fast_check_result else None
    return PreLaunchCheckResult(
        projects=fast_check_result.projects,
        services=extensions_fetch_result.services
    ) if fast_check_result and extensions_fetch_result else None
async def metrics_initial_check() -> Optional[FastCheckResult]:
    async with init_gcp_client_session(
    ) as gcp_session, init_dt_client_session() as dt_session:
        token = await create_token(logging_context, gcp_session)
        if token:
            fast_check_result = await MetricsFastCheck(
                gcp_session=gcp_session,
                dt_session=dt_session,
                token=token,
                logging_context=logging_context).execute()
            if fast_check_result.projects:
                logging_context.log(
                    f'Monitoring enabled for the following projects: {fast_check_result}'
                )
                return fast_check_result
            else:
                logging_context.log(
                    "Monitoring disabled. Check your project(s) settings.")
        else:
            logging_context.log(
                f'Monitoring disabled. Unable to acquire authorization token.')

        return None
async def handle_event(event: Dict,
                       event_context,
                       project_id_owner: Optional[str],
                       projects_ids: Optional[List[str]] = None):
    if isinstance(event_context, Dict):
        context = LoggingContext(event_context.get("execution_id", None))
    else:
        context = LoggingContext(None)

    selected_services = None
    if "GCP_SERVICES" in os.environ:
        selected_services_string = os.environ.get("GCP_SERVICES", "")
        selected_services = selected_services_string.split(
            ",") if selected_services_string else []
        #set default featureset if featureset not present in env variable
        for i, service in enumerate(selected_services):
            if "/" not in service:
                selected_services[i] = f"{service}/default"

    services = load_supported_services(context, selected_services)

    async with init_gcp_client_session(
    ) as gcp_session, init_dt_client_session() as dt_session:
        setup_start_time = time.time()
        token = await create_token(context, gcp_session)

        if token is None:
            context.log(
                "Cannot proceed without authorization token, stopping the execution"
            )
            return
        if not isinstance(token, str):
            raise Exception(
                f"Failed to fetch access token, got non string value: {token}")

        context.log("Successfully obtained access token")

        if not project_id_owner:
            project_id_owner = get_project_id_from_environment()

        dynatrace_api_key = await fetch_dynatrace_api_key(
            gcp_session=gcp_session, project_id=project_id_owner, token=token)
        dynatrace_url = await fetch_dynatrace_url(gcp_session=gcp_session,
                                                  project_id=project_id_owner,
                                                  token=token)

        print_metric_ingest_input = \
            "PRINT_METRIC_INGEST_INPUT" in os.environ and os.environ["PRINT_METRIC_INGEST_INPUT"].upper() == "TRUE"

        self_monitoring_enabled = os.environ.get('SELF_MONITORING_ENABLED',
                                                 "False").upper() == "TRUE"

        context = MetricsContext(
            gcp_session=gcp_session,
            dt_session=dt_session,
            project_id_owner=project_id_owner,
            token=token,
            execution_time=datetime.utcnow(),
            execution_interval_seconds=60 * 1,
            dynatrace_api_key=dynatrace_api_key,
            dynatrace_url=dynatrace_url,
            print_metric_ingest_input=print_metric_ingest_input,
            self_monitoring_enabled=self_monitoring_enabled,
            scheduled_execution_id=context.scheduled_execution_id)

        if not projects_ids:
            projects_ids = await get_all_accessible_projects(
                context, gcp_session, token)

        setup_time = (time.time() - setup_start_time)
        context.setup_execution_time = {
            project_id: setup_time
            for project_id in projects_ids
        }

        context.start_processing_timestamp = time.time()

        process_project_metrics_tasks = [
            process_project_metrics(context, project_id, services)
            for project_id in projects_ids
        ]
        await asyncio.gather(*process_project_metrics_tasks,
                             return_exceptions=True)
        context.log(
            f"Fetched and pushed GCP data in {time.time() - context.start_processing_timestamp} s"
        )

        log_self_monitoring_data(context)
        if context.self_monitoring_enabled:
            await push_self_monitoring(context)

        await gcp_session.close()
        await dt_session.close()
Exemplo n.º 9
0
async def handle_event(event: Dict,
                       event_context,
                       projects_ids: Optional[List[str]] = None,
                       services: Optional[List[GCPService]] = None):
    if isinstance(event_context, Dict):
        # for k8s installation
        context = LoggingContext(event_context.get("execution_id", None))
    else:
        context = LoggingContext(None)

    if not services:
        # load services for GCP Function
        services = load_supported_services(context)

    async with init_gcp_client_session(
    ) as gcp_session, init_dt_client_session() as dt_session:
        setup_start_time = time.time()
        token = await create_token(context, gcp_session)

        if token is None:
            context.log(
                "Cannot proceed without authorization token, stopping the execution"
            )
            return
        if not isinstance(token, str):
            raise Exception(
                f"Failed to fetch access token, got non string value: {token}")

        context.log("Successfully obtained access token")

        project_id_owner = get_project_id_from_environment()

        dynatrace_api_key = await fetch_dynatrace_api_key(
            gcp_session=gcp_session, project_id=project_id_owner, token=token)
        dynatrace_url = await fetch_dynatrace_url(gcp_session=gcp_session,
                                                  project_id=project_id_owner,
                                                  token=token)
        check_version(logging_context=context)
        await check_dynatrace(logging_context=context,
                              project_id=project_id_owner,
                              dt_session=dt_session,
                              dynatrace_url=dynatrace_url,
                              dynatrace_access_key=dynatrace_api_key)
        query_interval_min = get_query_interval_minutes()

        print_metric_ingest_input = os.environ.get(
            "PRINT_METRIC_INGEST_INPUT", "FALSE").upper() in ["TRUE", "YES"]
        self_monitoring_enabled = os.environ.get(
            'SELF_MONITORING_ENABLED', "FALSE").upper() in ["TRUE", "YES"]

        context = MetricsContext(
            gcp_session=gcp_session,
            dt_session=dt_session,
            project_id_owner=project_id_owner,
            token=token,
            execution_time=datetime.utcnow(),
            execution_interval_seconds=60 * query_interval_min,
            dynatrace_api_key=dynatrace_api_key,
            dynatrace_url=dynatrace_url,
            print_metric_ingest_input=print_metric_ingest_input,
            self_monitoring_enabled=self_monitoring_enabled,
            scheduled_execution_id=context.scheduled_execution_id)

        if not projects_ids:
            projects_ids = await get_all_accessible_projects(
                context, gcp_session, token)

        disabled_apis = {}
        disabled_projects = []
        for project_id in projects_ids:
            await check_x_goog_user_project_header_permissions(
                context, project_id)
            disabled_apis = {
                project_id: await get_all_disabled_apis(context, project_id)
            }
            if 'monitoring.googleapis.com' in disabled_apis[project_id]:
                disabled_projects.append(project_id)

        if disabled_projects:
            context.log(
                f"monitoring.googleapis.com API disabled in the projects: " +
                ", ".join(disabled_projects) +
                ", that projects will not be monitored")
            for disabled_project in disabled_projects:
                projects_ids.remove(disabled_project)

        setup_time = (time.time() - setup_start_time)
        context.setup_execution_time = {
            project_id: setup_time
            for project_id in projects_ids
        }

        context.start_processing_timestamp = time.time()

        process_project_metrics_tasks = [
            process_project_metrics(context, project_id, services,
                                    disabled_apis.get(project_id, set()))
            for project_id in projects_ids
        ]
        await asyncio.gather(*process_project_metrics_tasks,
                             return_exceptions=True)
        context.log(
            f"Fetched and pushed GCP data in {time.time() - context.start_processing_timestamp} s"
        )

        log_self_monitoring_data(context)
        if context.self_monitoring_enabled:
            await push_self_monitoring(context)

        await gcp_session.close()
        await dt_session.close()