Exemplo n.º 1
0
 def _logging_service_client(self) -> LoggingServiceV2Client:
     """The Cloud logging service v2 client."""
     credentials, _ = self._credentials_and_project
     client = LoggingServiceV2Client(
         credentials=credentials,
         client_info=CLIENT_INFO,
     )
     return client
Exemplo n.º 2
0
 def _logging_service_client(self) -> LoggingServiceV2Client:
     """The Cloud logging service v2 client."""
     credentials, _ = self._credentials_and_project
     client = LoggingServiceV2Client(
         credentials=credentials,
         client_info=ClientInfo(client_library_version='airflow_v' + version.version),
     )
     return client
Exemplo n.º 3
0
def test_end_to_end(service_url_auth_token, deployed_service):
    service_url, auth_token = service_url_auth_token

    # Test that the service is responding
    retry_strategy = Retry(
        total=3,
        status_forcelist=[400, 401, 403, 500, 502, 503, 504],
        allowed_methods=["GET", "POST"],
        backoff_factor=3,
    )
    adapter = HTTPAdapter(max_retries=retry_strategy)

    client = requests.session()
    client.mount("https://", adapter)

    response = client.get(
        service_url,
        headers={
            "Authorization": f"Bearer {auth_token}",
            "X-Cloud-Trace-Context": "foo/bar",
        },
    )
    assert response.status_code == 200
    assert "Hello Logger!" in response.content.decode("UTF-8")

    # Test that the logs are writing properly to stackdriver
    time.sleep(10)  # Slight delay writing to stackdriver
    client = LoggingServiceV2Client()
    resource_names = [f"projects/{PROJECT}"]
    # We add timestamp for making the query faster.
    now = datetime.datetime.now(datetime.timezone.utc)
    filter_date = now - datetime.timedelta(minutes=3)
    filters = (f"timestamp>=\"{filter_date.isoformat('T')}\" "
               "resource.type=cloud_run_revision "
               "AND severity=NOTICE "
               f"AND resource.labels.service_name={deployed_service} "
               "AND jsonPayload.component=arbitrary-property")

    # Retry a maximum number of 10 times to find results in stackdriver
    found = False
    for x in range(10):
        iterator = client.list_log_entries({
            "resource_names": resource_names,
            "filter": filters
        })
        for entry in iterator:
            found = True
            # If there are any results, exit loop
            break
        # When message found, exit loop
        if found:
            break
        # Linear backoff
        time.sleep(3 * x)

    assert found
Exemplo n.º 4
0
def make_logging_api(client):
    """Create an instance of the Logging API adapter.

    Args:
        client (~logging_v2.client.Client): The client
            that holds configuration details.

    Returns:
        _LoggingAPI: A metrics API instance with the proper credentials.
    """
    generated = LoggingServiceV2Client(
        credentials=client._credentials,
        client_info=client._client_info,
        client_options=client._client_options,
    )
    return _LoggingAPI(generated, client)
Exemplo n.º 5
0
def test_end_to_end(pubsub_topic):
    # Post the message "Runner" to the topic
    publisher = pubsub_v1.PublisherClient()
    topic_path = publisher.topic_path(PROJECT, pubsub_topic)
    message = "Runner"
    data = message.encode("utf-8")
    # When you publish a message, the client returns a future.
    future = publisher.publish(topic_path, data)
    future.result()

    # Check the logs for "Hello Runner"
    time.sleep(20)  # Slight delay writing to stackdriver
    client = LoggingServiceV2Client()
    resource_names = [f"projects/{PROJECT}"]

    # We add timestamp for making the query faster.
    now = datetime.datetime.now(datetime.timezone.utc)
    filter_date = now - datetime.timedelta(minutes=1)
    filters = (f"timestamp>=\"{filter_date.isoformat('T')}\" "
               "resource.type=cloud_run_revision "
               f"AND resource.labels.service_name={CLOUD_RUN_SERVICE} ")

    # Retry a maximum number of 10 times to find results in stackdriver
    found = False
    for x in range(10):
        iterator = client.list_log_entries({
            "resource_names": resource_names,
            "filter": filters
        })
        for entry in iterator:
            if entry.text_payload == "Hello Runner!":
                found = True
                break
        # When message found, exit loop
        if found is True:
            break
        time.sleep(5)  # Slight delay before retry

    assert found
Exemplo n.º 6
0
 def test_ctor(self):
     gapic_client = LoggingServiceV2Client()
     api = _gapic._LoggingAPI(gapic_client, mock.sentinel.client)
     assert api._gapic_api is gapic_client
     assert api._client is mock.sentinel.client
Exemplo n.º 7
0
 def make_logging_api():
     gapic_client = LoggingServiceV2Client()
     handwritten_client = mock.Mock()
     api = _gapic._LoggingAPI(gapic_client, handwritten_client)
     return api