google app engine - In python, communicating with stackdriver always returns success, doesn't send anything -
for inexplicable reason, google provides no stackdriver api appengine, i'm stuck implementing one. no worries - thought - have worked api builder talk bigquery, built client , started trying send events:
credentials = signedjwtassertioncredentials(stackdriver_auth_google_client_email, stackdriver_auth_google_private_key, scope='https://www.googleapis.com/auth/trace.append') http = httplib2.http() credentials.refresh(http) #working around oauth2client bug credentials = credentials.authorize(http) service = build('cloudtrace', 'v1', http=http) batch = service.new_batch_http_request() batch.add(service.projects().patchtraces( body=traces_json, projectid=stackdriver_auth_google_project_id)) print batch.execute()
i left out definition of traces_json because no matter send, service responds error. if traces_json = '{}':
{u'error': {u'code': 400, u'errors': [{u'domain': u'global', u'message': u'invalid value @ \'traces\' (type.googleapis.com/google.devtools.cloudtrace.v1.traces), "{}"', u'reason': u'badrequest'}], u'message': u'invalid value @ \'traces\' (type.googleapis.com/google.devtools.cloudtrace.v1.traces), "{}"', u'status': u'invalid_argument'}}
but if use body, crafted google documentation, still same error.
i'm running packet sniffer on machine i'm attempting this, , see communicating googleapis.com.
so question is, really, missing me sending events stackdriver?
update
here's recent iteration of i'd been working with, though using google doc example verbatim (with exception of changing project id) produces same result.
{ "traces": [ { "projectid": "projectname", "traceid": "1234123412341234aaaabb3412347890", "spans": [ { "kind": "rpc_server", "name": "trace_name", "labels": {"label1": "value1", "label2": "value2"}, "spanid": "spanid1", "starttime": "2016-06-01t05:01:23.045123456z", "endtime": "2016-06-01t05:01:23.945123456z", }, ], }, ], }
and error message comes it:
{u'error': {u'code': 400, u'errors': [{u'domain': u'global', u'message': u'invalid value @ \'traces\' (type.googleapis.com/google.devtools.cloudtrace.v1.traces), "my entire json repeated here"', u'reason': u'badrequest'}], u'message': u'invalid value @ \'traces\' (type.googleapis.com/google.devtools.cloudtrace.v1.traces), "my entire json repeated here"', u'status': u'invalid_argument'}}
second update
doing in explorer produces approximately same result. had switch numeric span_id because, despite docs' statement has unique string, errors requiring appears 64-bit integer, time provide else.
patch https://cloudtrace.googleapis.com/v1/projects/[number or name]/traces?key={your_api_key} { "traces": [ { "projectid": "[number or name]", "traceid": "1234123412341234aaaabb3412347891", "spans": [ { "kind": "rpc_server", "name": "trace_name", "labels": { "label1": "value1" }, "starttime": "2016-06-01t05:01:23.045123456z", "endtime": "2016-06-01t05:01:25.045123456z" }, { "spanid": "0" } ] } ] }
response:
{ "error": { "code": 400, "message": "request contains invalid argument.", "status": "invalid_argument" } }
the issue in format of data. can not send empty messages either. best way explore how use api go stackdriver trace api explorer, find out exact data structure send: https://cloud.google.com/trace/api/reference/rest/v1/projects/patchtraces#traces
pay special attention format of traceid. needs 32 character hex string this: 7d9d1a6e2d1f3f27484992f33d97e5cb
here working python example show how use 3 methods on stackdriver trace on github: https://github.com/qike/cloud-trace-samples-python
copy paste code below:
def list_traces(stub, project_id): """lists traces in given project.""" trace_id = none req = trace_pb2.listtracesrequest(project_id=project_id) try: resp = stub.listtraces(req, timeout) t in resp.traces: trace_id = t.trace_id print("trace is: {}".format(t.trace_id)) except networkerror, e: logging.warning('failed list traces: {}'.format(e)) sys.exit(1) return trace_id def patch_traces(stub, project_id): req = trace_pb2.patchtracesrequest(project_id=project_id) trace_id = str(uuid.uuid1()).replace('-', '') = time.time() trace = req.traces.traces.add() trace.project_id = project_id trace.trace_id = trace_id span1 = trace.spans.add() span1.span_id = 1 span1.name = "/span1.{}".format(trace_id) span1.start_time.seconds = int(now)-10 span1.end_time.seconds = int(now) span2 = trace.spans.add() span2.span_id = 2 span2.name = "/span2" span2.start_time.seconds = int(now)-8 span2.end_time.seconds = int(now)-5 try: resp = stub.patchtraces(req, timeout) print("trace added successfully.\n" "to view list of traces, go to: " "http://console.cloud.google.com/traces/traces?project={}&tr=2\n" "to view trace added, go to: " "http://console.cloud.google.com/traces/details/{}?project={}" .format(project_id, trace_id, project_id)) except networkerror, e: logging.warning('failed patch traces: {}'.format(e)) sys.exit(1) def get_trace(stub, project_id, trace_id): req = trace_pb2.gettracerequest(project_id=project_id, trace_id=trace_id) try: resp = stub.gettrace(req, timeout) print("trace retrieved: {}".format(resp)) except networkerror, e: logging.warning('failed trace: {}'.format(e)) sys.exit(1)
updated answer error received api explorer
regarding errors got using api explorer, due using 0 span_id. should 64 bit int other 0.
i observed span_id set in different span object 1 intended. make sure don't mistake clicking on "+" sign add new span object.
below successful patch request sent project through api explorer:
{ "traces": [ { "projectid": "<project id>", // used string id, not numeric number "traceid": "1234123412341234aaaabb3412347891", "spans": [ { "spanid": "1", "name": "foo", "starttime": "2016-06-01t05:01:23.045123456z", "endtime": "2016-06-01t05:01:25.045123456z" } ] } ] } response 200