Skip to content

Instantly share code, notes, and snippets.

@sanealytics
Created June 20, 2020 16:31
Show Gist options
  • Save sanealytics/b70f1cd89dc3caf3e8e543f460fefeaf to your computer and use it in GitHub Desktop.
Save sanealytics/b70f1cd89dc3caf3e8e543f460fefeaf to your computer and use it in GitHub Desktop.

Revisions

  1. sanealytics created this gist Jun 20, 2020.
    23 changes: 23 additions & 0 deletions get_service_trace_id_example.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    from requests import post
    import json

    GET_TID_URI='https://XXXX/get_service_trace_id'
    TID_SERVICE_NAME='my_service'
    TID_SERVICE_VERSION='1.0' # Automate this in CI/CD and read it in
    TID_SERVICE_INSTANCE='default' # Get from Google environment variables
    # For example
    # TID_SERVICE_INSTANCE=f'{request.headers["X-Appengine-Queuename"]}-{request.headers["X-Appengine-Taskname"]}'

    headers = {'content-type': 'application/json'}

    data = {
    'service': TID_SERVICE_NAME,
    'instance': TID_SERVICE_INSTANCE,
    'version': TID_SERVICE_VERSION,
    'metadata': json.dumps({'prev_tids': prev_tids}) # A list of previous tids
    }

    r = post(GET_TID_URI, data=json.dumps(data), headers=headers)

    assert(r.status_code == 200)
    tid = r.json().get('tid')