Santiago Gil pushed to branch santigl/106-request-metadata at BuildGrid / buildgrid
WARNING: The push did not contain any new commits, but force pushed to delete the commits and changes below.
Deleted commits:
3 changed files:
Changes:
| ... | ... | @@ -30,7 +30,8 @@ from buildgrid.client.authentication import setup_channel | 
| 30 | 30 | 
 from buildgrid.client.cas import download, upload
 | 
| 31 | 31 | 
 from buildgrid._exceptions import InvalidArgumentError
 | 
| 32 | 32 | 
 from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2, remote_execution_pb2_grpc
 | 
| 33 | 
-from buildgrid.utils import create_digest
 | 
|
| 33 | 
+from buildgrid.settings import REQUEST_METADATA_TOOL_NAME, REQUEST_METADATA_TOOL_VERSION
 | 
|
| 34 | 
+from buildgrid.utils import create_digest, request_metadata_header_entry
 | 
|
| 34 | 35 | 
 | 
| 35 | 36 | 
 from ..cli import pass_context
 | 
| 36 | 37 | 
 | 
| ... | ... | @@ -48,8 +49,17 @@ from ..cli import pass_context | 
| 48 | 49 | 
               help="Public server certificate for TLS (PEM-encoded).")
 | 
| 49 | 50 | 
 @click.option('--instance-name', type=click.STRING, default=None, show_default=True,
 | 
| 50 | 51 | 
               help="Targeted farm instance name.")
 | 
| 52 | 
+@click.option('-t', '--tool-name', type=str, default=REQUEST_METADATA_TOOL_NAME,
 | 
|
| 53 | 
+              help='Tool name.')
 | 
|
| 54 | 
+@click.option('-n', '--tool-version', type=str, default=REQUEST_METADATA_TOOL_VERSION,
 | 
|
| 55 | 
+              help='Tool version.')
 | 
|
| 56 | 
+@click.option('-a', '--action-id', type=str, help='Action ID.')
 | 
|
| 57 | 
+@click.option('-i', '--invocation-id', type=str, help='Tool invocation ID.')
 | 
|
| 58 | 
+@click.option('-c', '--correlation-id', type=str, help='Correlated invocation ID.')
 | 
|
| 51 | 59 | 
 @pass_context
 | 
| 52 | 
-def cli(context, remote, instance_name, auth_token, client_key, client_cert, server_cert):
 | 
|
| 60 | 
+def cli(context, remote, instance_name, auth_token, client_key, client_cert,
 | 
|
| 61 | 
+        server_cert, tool_name, tool_version, action_id, invocation_id,
 | 
|
| 62 | 
+        correlation_id):
 | 
|
| 53 | 63 | 
     """Entry point for the bgd-execute CLI command group."""
 | 
| 54 | 64 | 
     try:
 | 
| 55 | 65 | 
         context.channel, _ = setup_channel(remote, auth_token=auth_token,
 | 
| ... | ... | @@ -62,6 +72,14 @@ def cli(context, remote, instance_name, auth_token, client_key, client_cert, ser | 
| 62 | 72 | 
 | 
| 63 | 73 | 
     context.instance_name = instance_name
 | 
| 64 | 74 | 
 | 
| 75 | 
+    request_metadata = request_metadata_header_entry(tool_name=tool_name,
 | 
|
| 76 | 
+                                                     tool_version=tool_version,
 | 
|
| 77 | 
+                                                     action_id=action_id,
 | 
|
| 78 | 
+                                                     tool_invocation_id=invocation_id,
 | 
|
| 79 | 
+                                                     correlated_invocations_id=correlation_id)
 | 
|
| 80 | 
+  | 
|
| 81 | 
+    context.request_metadata = request_metadata
 | 
|
| 82 | 
+  | 
|
| 65 | 83 | 
 | 
| 66 | 84 | 
 @cli.command('request-dummy', short_help="Send a dummy action.")
 | 
| 67 | 85 | 
 @click.option('--number', type=click.INT, default=1, show_default=True,
 | 
| ... | ... | @@ -85,12 +103,12 @@ def request_dummy(context, number, wait_for_completion): | 
| 85 | 103 | 
                                                   action_digest=action_digest,
 | 
| 86 | 104 | 
                                                   skip_cache_lookup=True)
 | 
| 87 | 105 | 
 | 
| 106 | 
+    print(context.request_metadata)
 | 
|
| 88 | 107 | 
     responses = []
 | 
| 89 | 108 | 
     for _ in range(0, number):
 | 
| 90 | 
-        responses.append(stub.Execute(request))
 | 
|
| 109 | 
+        responses.append(stub.Execute(request, metadata=context.request_metadata))
 | 
|
| 91 | 110 | 
 | 
| 92 | 111 | 
     for response in responses:
 | 
| 93 | 
-  | 
|
| 94 | 112 | 
         if wait_for_completion:
 | 
| 95 | 113 | 
             result = None
 | 
| 96 | 114 | 
             for stream in response:
 | 
| ... | ... | @@ -121,6 +139,7 @@ def run_command(context, input_root, commands, output_file, output_directory, | 
| 121 | 139 | 
     stub = remote_execution_pb2_grpc.ExecutionStub(context.channel)
 | 
| 122 | 140 | 
 | 
| 123 | 141 | 
     output_executables = []
 | 
| 142 | 
+  | 
|
| 124 | 143 | 
     with upload(context.channel, instance=context.instance_name) as uploader:
 | 
| 125 | 144 | 
         command = remote_execution_pb2.Command()
 | 
| 126 | 145 | 
 | 
| ... | ... | @@ -157,7 +176,8 @@ def run_command(context, input_root, commands, output_file, output_directory, | 
| 157 | 176 | 
     request = remote_execution_pb2.ExecuteRequest(instance_name=context.instance_name,
 | 
| 158 | 177 | 
                                                   action_digest=action_digest,
 | 
| 159 | 178 | 
                                                   skip_cache_lookup=True)
 | 
| 160 | 
-    response = stub.Execute(request)
 | 
|
| 179 | 
+  | 
|
| 180 | 
+    response = stub.Execute(request, metadata=context.request_metadata)
 | 
|
| 161 | 181 | 
 | 
| 162 | 182 | 
     stream = None
 | 
| 163 | 183 | 
     for stream in response:
 | 
| ... | ... | @@ -14,6 +14,7 @@ | 
| 14 | 14 | 
 | 
| 15 | 15 | 
 | 
| 16 | 16 | 
 import hashlib
 | 
| 17 | 
+from _version import __version__
 | 
|
| 17 | 18 | 
 | 
| 18 | 19 | 
 | 
| 19 | 20 | 
 # Hash function used for computing digests:
 | 
| ... | ... | @@ -43,3 +44,13 @@ BROWSER_URL_FORMAT = '%(type)s/%(instance)s/%(hash)s/%(sizebytes)s/' | 
| 43 | 44 | 
 #  type       - Type of CAS object, eg. 'action_result', 'command'...
 | 
| 44 | 45 | 
 #  hash       - Object's digest hash.
 | 
| 45 | 46 | 
 #  sizebytes  - Object's digest size in bytes.
 | 
| 47 | 
+  | 
|
| 48 | 
+  | 
|
| 49 | 
+# Name of the header key to attach optional `RequestMetadata`values.
 | 
|
| 50 | 
+# (This is defined in the REAPI specification.)
 | 
|
| 51 | 
+REQUEST_METADATA_HEADER_NAME = 'requestmetadata-bin'
 | 
|
| 52 | 
+  | 
|
| 53 | 
+# 'RequestMetadata' header values. These values will be used when
 | 
|
| 54 | 
+# attaching optional metadata to a gRPC request's header:
 | 
|
| 55 | 
+REQUEST_METADATA_TOOL_NAME = 'buildgrid'
 | 
|
| 56 | 
+REQUEST_METADATA_TOOL_VERSION = __version__
 | 
| ... | ... | @@ -18,7 +18,7 @@ from operator import attrgetter | 
| 18 | 18 | 
 import os
 | 
| 19 | 19 | 
 import socket
 | 
| 20 | 20 | 
 | 
| 21 | 
-from buildgrid.settings import HASH, HASH_LENGTH, BROWSER_URL_FORMAT
 | 
|
| 21 | 
+from buildgrid.settings import HASH, HASH_LENGTH, BROWSER_URL_FORMAT, REQUEST_METADATA_HEADER_NAME
 | 
|
| 22 | 22 | 
 from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2
 | 
| 23 | 23 | 
 | 
| 24 | 24 | 
 | 
| ... | ... | @@ -284,3 +284,25 @@ def output_directory_maker(directory_path, working_path, tree_digest): | 
| 284 | 284 | 
     output_directory.path = os.path.relpath(directory_path, start=working_path)
 | 
| 285 | 285 | 
 | 
| 286 | 286 | 
     return output_directory
 | 
| 287 | 
+  | 
|
| 288 | 
+  | 
|
| 289 | 
+def request_metadata_header_entry(tool_name=None, tool_version=None,
 | 
|
| 290 | 
+                                  action_id=None, tool_invocation_id=None,
 | 
|
| 291 | 
+                                  correlated_invocations_id=None):
 | 
|
| 292 | 
+    """Creates a serialized RequestMetadata entry to attach to a gRPC
 | 
|
| 293 | 
+    call header. Arguments should be of type str or None.
 | 
|
| 294 | 
+    """
 | 
|
| 295 | 
+    request_metadata = remote_execution_pb2.RequestMetadata()
 | 
|
| 296 | 
+    if action_id:
 | 
|
| 297 | 
+        request_metadata.action_id = action_id
 | 
|
| 298 | 
+    if tool_invocation_id:
 | 
|
| 299 | 
+        request_metadata.tool_invocation_id = tool_invocation_id
 | 
|
| 300 | 
+    if correlated_invocations_id:
 | 
|
| 301 | 
+        request_metadata.correlated_invocations_id = correlated_invocations_id
 | 
|
| 302 | 
+    if tool_name:
 | 
|
| 303 | 
+        request_metadata.tool_details.tool_name = tool_name
 | 
|
| 304 | 
+    if tool_version:
 | 
|
| 305 | 
+        request_metadata.tool_details.tool_version = tool_version
 | 
|
| 306 | 
+  | 
|
| 307 | 
+    return ((REQUEST_METADATA_HEADER_NAME,
 | 
|
| 308 | 
+             request_metadata.SerializeToString()),)
 | 
