“无效的家长ID!”发送跟踪时

时间:2018-09-18 12:34:54

标签: google-app-engine google-app-engine-python stackdriver google-cloud-trace

我有一个运行在Google App Engine标准python 2.7上的应用程序。 GAE已经跟踪到数据存储和Memcache的调用,我想添加自己的跟踪。

因为我使用的是Python 2 google-cloud-python,所以我的应用无法使用。

我使用发现API客户端克服了这一难题。有点工作:我可以看到我的踪迹,但它们未与GAE踪迹separate traces链接。

我正在处理此问题,检查了如何在此获取主跨区的ID,其中一个覆盖了所有请求。此信息位于标题X-Cloud-Trace-Context(source)中。

我发现我没有正确解析此标头的spanID部分。

知道我正确地读取了它并将其设置为自己跨度的父级时,出现了400 http错误“无效的父级id!”。当我尝试扩大跨度时。

以下是GAE已自动填充到我从Stackdriver API检索到的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为704381​​4caa66c00a3fac3555e1d1e9d2 父范围ID为12635947902476947926,看起来像是64位整数(跟踪API V1)

这是我对https://cloudtrace.googleapis.com/v2/projects/my-project/traces:batchWrite?alt=json(Trace api v2)的请求内容的内容,

{"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格式发送我的跨度。

1 个答案:

答案 0 :(得分:1)

V2 API期望parentSpanId为16个字符的十六进制字符串,但是您仍将其作为小数发送。将其转换为十六进制(例如af5be7ab4f7b11d6),并且batchWrite调用应该可以正常工作。