我有一个在谷歌应用引擎标准 python 2.7 上运行的应用程序。GAE 已经跟踪对 Datastore 和 Memcache 的调用,我想添加自己的跟踪。
由于我使用的是 python 2 google-cloud-python不适用于我的应用程序。
我使用发现 api 客户端做了一些事情来克服这个问题。它有点工作:我可以看到我的痕迹,但它们与 GAE 痕迹无关。
我对此进行了研究,检查了如何在此处获取主跨度的 ID,该 ID 涵盖了所有请求。此信息在标头 X-Cloud-Trace-Context ( source ) 中可用。
我发现我没有正确解析此标头的 spanID 部分。
知道我确实正确阅读并将其设置为我自己跨度的父级我收到 400 http 错误“无效的父级 ID!” 当我试图推动我的跨度时。
以下是我从 Stackdriver API 检索到的 GAE 已自动填充到 Stackdriver 跟踪中的示例:
{
"projectId": "my-project",
"traceId": "7043814caa66c00a3fac3555e1d1e9d2",
"spans": [
{
"spanId": "12635947902476947926",
"name": "REDACTED",
"startTime": "2018-09-18T11:44:46.666356Z",
"endTime": "2018-09-18T11:45:02.926300478Z",
"labels": {REDACTED}
},
{
"spanId": "2895282594665317964",
"name": "/remote_socket.CreateSocket",
"startTime": "2018-09-18T11:44:50.469419110Z",
"endTime": "2018-09-18T11:44:50.477561891Z",
"parentSpanId": "12635947902476947926",
"labels": {
"g.co/gae/application_error": "6",
"g.co/gae/error_message": "IP must be wildcard or be an IP returned by this RSG instance on a previous socket() call, received IP: ::1"
}
},
{
"spanId": "12635947904150929113",
"name": "REDACTED",
"startTime": "2018-09-18T11:44:49.588031056Z",
"endTime": "2018-09-18T11:45:02.922000150Z",
"parentSpanId": "12635947902476947926"
},
{
"spanId": "18119035554219197767",
"name": "/cloud_debugger.DebugletStarted",
"startTime": "2018-09-18T11:44:49.605121020Z",
"endTime": "2018-09-18T11:44:49.605863978Z",
"parentSpanId": "12635947902476947926"
},
{
"spanId": "12635947905824910300",
"name": "/remote_socket.CreateSocket",
"startTime": "2018-09-18T11:44:50.468409633Z",
"endTime": "2018-09-18T11:44:50.478480133Z",
"parentSpanId": "12635947904150929113"
},
{
"spanId": "12635947904936909686",
"name": "/urlfetch.Fetch",
"startTime": "2018-09-18T11:44:52.761299204Z",
"endTime": "2018-09-18T11:44:52.766834488Z",
"parentSpanId": "12635947904150929113"
},
{
"spanId": "4533572578254567247",
"name": "/urlfetch.Fetch",
"startTime": "2018-09-18T11:44:52.762846328Z",
"endTime": "2018-09-18T11:44:52.766059416Z",
"parentSpanId": "12635947902476947926",
"labels": {
"/http/method": "GET",
"/http/response/size": "0",
"/http/status_code": "0",
"/http/url": "http://metadata.google.internal",
"/http/host": "metadata.google.internal"
}
},
{
"spanId": "12635947904492451751",
"name": "/user.GetOAuthUser",
"startTime": "2018-09-18T11:45:02.857408315Z",
"endTime": "2018-09-18T11:45:02.862760982Z",
"parentSpanId": "12635947904150929113"
},
{
"spanId": "2960740089906396384",
"name": "/user.GetOAuthUser",
"startTime": "2018-09-18T11:45:02.858548710Z",
"endTime": "2018-09-18T11:45:02.862337765Z",
"parentSpanId": "12635947902476947926"
},
{
"spanId": "12635947905888909837",
"name": "/memcache.Get",
"startTime": "2018-09-18T11:45:02.872708349Z",
"endTime": "2018-09-18T11:45:02.875247917Z",
"parentSpanId": "12635947904150929113"
},
{
"spanId": "2806170211922774912",
"name": "/memcache.Get",
"startTime": "2018-09-18T11:45:02.873653089Z",
"endTime": "2018-09-18T11:45:02.874549693Z",
"parentSpanId": "12635947902476947926",
"labels": {
"g.co/gae/memcache/count": "1",
"g.co/gae/memcache/size": "26680"
}
},
{
"spanId": "12635947905612370518",
"name": "/memcache.Get",
"startTime": "2018-09-18T11:45:02.884082965Z",
"endTime": "2018-09-18T11:45:02.885967828Z",
"parentSpanId": "12635947904150929113"
},
{
"spanId": "1403376319062964670",
"name": "/memcache.Get",
"startTime": "2018-09-18T11:45:02.884984835Z",
"endTime": "2018-09-18T11:45:02.885727464Z",
"parentSpanId": "12635947902476947926",
"labels": {
"g.co/gae/memcache/count": "1",
"g.co/gae/memcache/size": "132"
}
},
{
"spanId": "12635947905900454375",
"name": "/urlfetch.Fetch",
"startTime": "2018-09-18T11:45:02.886823410Z",
"endTime": "2018-09-18T11:45:02.907643995Z",
"parentSpanId": "12635947904150929113"
},
{
"spanId": "12992632058664043952",
"name": "/urlfetch.Fetch",
"startTime": "2018-09-18T11:45:02.887618043Z",
"endTime": "2018-09-18T11:45:02.907269121Z",
"parentSpanId": "12635947902476947926",
"labels": {
"/http/host": "cloudtrace.googleapis.com",
"/http/method": "POST",
"/http/request/size": "590",
"/http/response/size": "165",
"/http/status_code": "400",
"/http/url": "https://cloudtrace.googleapis.com/v2/projects/lumsites-dev/traces:batchWrite?alt=json"
}
},
{
"spanId": "12635947904372387171",
"name": "/logservice.Flush",
"startTime": "2018-09-18T11:45:02.918746300Z",
"endTime": "2018-09-18T11:45:02.920277782Z",
"parentSpanId": "12635947904150929113"
},
{
"spanId": "16196356337812692515",
"name": "/logservice.Flush",
"startTime": "2018-09-18T11:45:02.919606565Z",
"endTime": "2018-09-18T11:45:02.919972451Z",
"parentSpanId": "12635947902476947926"
}
]
}
来自请求的标头:X-Cloud-Trace-Context : 7043814caa66c00a3fac3555e1d1e9d2/12635947902476947926;o=1
所以跟踪 ID 是 7043814caa66c00a3fac3555e1d1e9d2 和父跨度 ID 是 12635947902476947926 看起来像一个 64 位整数(跟踪 API V1)
这是我对https://cloudtrace.googleapis.com/v2/projects/my-project/traces:batchWrite?alt=json
(Trace api v2)的请求正文的内容,以 400 结尾
{"spans": [
{
'status': None,
'childSpanCount': 0,
'links': None,
'startTime': '2018-09-18T11: 45: 02.876711Z',
'spanId': '887188cf45b7461b',
'stackTrace': None,
'displayName': {
'truncated_byte_count': 0,
'value': 'manualSpan'
},
'name': 'projects/my-project/traces/7043814caa66c00a3fac3555e1d1e9d2/spans/887188cf45b7461b',
'parentSpanId': '12635947902476947926',
'attributes': {
'attributeMap': {
'g.co/agent': {
'string_value': {
'truncated_byte_count': 0,
'value': 'opencensus-python [
0.1.6
]'
}
}
}
},
'timeEvents': None,
'endTime': '2018-09-18T11: 45: 02.876915Z',
'sameProcessAsParentSpan': None
}
]}
我的第一个工作版本和这个版本之间的变化是 parentSpanId :之前,我使用了生成的 spanID。现在我使用标题中的那个。
如您所见,我正在使用的 parentSpanId (12635947902476947926) 确实存在于原始跟踪中。它是所有后续跨度的父级。
据我所知,该错误消息未记录在案。我没有看到对 parentSpanID 的限制,例如 parentSpanID 在我们发送之前可能需要存在并存在于堆栈驱动程序中。
也许我不能以 V1 格式混合传入的 spanID 并以 V2 格式发送我的 span。