def __init__(self,
                 channel=None,
                 credentials=None,
                 address="spanner.googleapis.com:443"):
        """Instantiate the transport class.

        Args:
            channel (grpc.Channel): A ``Channel`` instance through
                which to make calls. This argument is mutually exclusive
                with ``credentials``; providing both will raise an exception.
            credentials (google.auth.credentials.Credentials): The
                authorization credentials to attach to requests. These
                credentials identify this application to the service. If none
                are specified, the client will attempt to ascertain the
                credentials from the environment.
            address (str): The address where the service is hosted.
        """
        # If both `channel` and `credentials` are specified, raise an
        # exception (channels come with credentials baked in already).
        if channel is not None and credentials is not None:
            raise ValueError(
                "The `channel` and `credentials` arguments are mutually "
                "exclusive.")

        # Create the channel.
        if channel is None:
            channel = self.create_channel(address=address,
                                          credentials=credentials)

        self._channel = channel

        # gRPC uses objects called "stubs" that are bound to the
        # channel and provide a basic method for each RPC.
        self._stubs = {"spanner_stub": spanner_pb2_grpc.SpannerStub(channel)}
Ejemplo n.º 2
0
def _execute_probe(api, use_extension=False):
    """Execute a probe function given certain Cloud api and probe name.

  Args:
    api: the name of the api provider, e.g. "spanner", "firestore".
    use_extension: option to use grpc-gcp extension when creating channel.

  Raises:
    NotImplementedError: An error occurred when api does not match any records.
  """
    util = StackdriverUtil(api)

    if api == 'spanner':
        channel = _get_stub_channel(_SPANNER_TARGET, use_extension)
        stub = spanner_pb2_grpc.SpannerStub(channel)
        probe_functions = spanner_probes.PROBE_FUNCTIONS
    elif api == 'firestore':
        channel = _get_stub_channel(_FIRESTORE_TARGET)
        stub = firestore_pb2_grpc.FirestoreStub(channel)
        probe_functions = firestore_probes.PROBE_FUNCTIONS
    else:
        raise NotImplementedError('gRPC prober is not implemented for %s !' %
                                  api)

    total = len(probe_functions)
    success = 0

    # Execute all probes for given api
    for probe_name in probe_functions:
        probe_function = probe_functions[probe_name]
        try:
            probe_function(stub)
            success += 1
        except Exception:  # pylint: disable=broad-except
            # report any kind of exception to Stackdriver
            util.report_error(traceback.format_exc())

    if success == total:
        util.set_success(True)

    # Summarize metrics
    util.output_metrics()

    # Fail this probe if any function fails
    if success != total:
        sys.exit(1)