HEX
Server: Apache/2.4.65 (Ubuntu)
System: Linux ielts-store-v2 6.8.0-1036-gcp #38~22.04.1-Ubuntu SMP Thu Aug 14 01:19:18 UTC 2025 x86_64
User: root (0)
PHP: 7.2.34-54+ubuntu20.04.1+deb.sury.org+1
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: //snap/google-cloud-cli/396/lib/googlecloudsdk/core/metrics_reporter.py
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. All Rights Reserved.
#
# 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.

"""Script for reporting gcloud metrics."""

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import os
import pickle
import sys

# This file is unique because it is executed as a separate process on metrics
# teardown. Because of this, googlecloudsdk.core is on the Python Path (because
# the directory of the main script is always first on the Path). This happens
# not to matter on Python2, but on Python3, there is a module named http.client
# that is used by httplib2. When core is on the path, our http.py shadows this
# module and the import fails. On Python2, this module doesn't exist so we just
# hadn't hit this issue. Here, we remove the script's directory from the Path
# because we never want to import anything from here as an absolute import (on
# either Python2 or Python3).
sys.path.pop(0)

# pylint: disable=g-import-not-at-top
from googlecloudsdk.core import argv_utils
from googlecloudsdk.core.util import files

try:
  from googlecloudsdk.core import requests
except ImportError:
  # Do nothing if we can't import the lib.
  sys.exit(0)

# If outgoing packets are getting dropped, httplib2 will not respond
# indefinitely while waiting for a response.
TIMEOUT_IN_SEC = 10


def ReportMetrics(metrics_file_path):
  """Sends the specified anonymous usage event to the given analytics endpoint.

  Args:
      metrics_file_path: str, File with pickled metrics (list of tuples).
  """
  with files.BinaryFileReader(metrics_file_path) as metrics_file:
    metrics = pickle.load(metrics_file)
  os.remove(metrics_file_path)

  session = requests.Session()

  for metric in metrics:
    session.request(metric[1], metric[0], data=metric[2], headers=metric[3],
                    timeout=TIMEOUT_IN_SEC)

if __name__ == '__main__':
  try:
    ReportMetrics(argv_utils.GetDecodedArgv()[1])
  # pylint: disable=bare-except, Never fail or output a stacktrace here.
  except:
    pass