title: "Invalid parent id!" when sending trace
body: <p>I have an app running on google app engine standard, python 2.7.
  GAE already trace calls to the Datastore and to Memcache and I want to add my own traces.</p>
  
  <p>Since I'm in python 2 <a href="https://github.com/GoogleCloudPlatform/google-cloud-python" rel="nofollow noreferrer">google-cloud-python</a> is not available for my app.</p>
  
  <p>I made something to overcome that using discovery api client. It kinda work : I can see my traces but they are not linked with GAE traces <a href="https://i.stack.imgur.com/E7WMp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E7WMp.png" alt="separate traces"></a>.</p>
  
  <p>I work on that, checked how I can get the ID of the main span here, the one covering all the request. This information is available in the header X-Cloud-Trace-Context (<a href="https://cloud.google.com/trace/docs/troubleshooting#how_do_i_force_a_request_to_be_traced" rel="nofollow noreferrer">source</a>).</p>
  
  <p>I found that I do not correctly parse the spanID part of this header. </p>
  
  <p>Know that I do read it correctly and set it as the parent of my own span I got a 400 http error "Invalid parent id!" when I try to push my span. </p>
  
  <p>Here is an example of what GAE already fill automatically in Stackdriver trace that I retrieve from Stackdriver API: </p>
  
  <pre><code>{
    "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"
      }
    ]
  }
  </code></pre>
  
  <p>The header coming from the request : <code>X-Cloud-Trace-Context : 7043814caa66c00a3fac3555e1d1e9d2/12635947902476947926;o=1</code>
  So trace ID is 7043814caa66c00a3fac3555e1d1e9d2
  and parent span ID is 12635947902476947926 which looks like a  64-bit integer (trace API V1)</p>
  
  <p>Here is the content of body of my request to <code>https://cloudtrace.googleapis.com/v2/projects/my-project/traces:batchWrite?alt=json</code> (Trace api v2) which end in 400</p>
  
  <pre><code>{"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
      }
  ]}
  </code></pre>
  
  <p>What change between my first working version and this one is the parentSpanId : before, I used a generated spanID. Now I use the one present in the header.</p>
  
  <p>As you can see the parentSpanId I'm using (12635947902476947926) DOES exist in the original trace. It is the parent of all subsequent spans.</p>
  
  <p>The error message is not documented, as far as I could search.
  I did not see limitations on parentSpanID like maybe the parentSpanID needs to exists and be present in stackdriver BEFORE we send it.</p>
  
  <p>Maybe I cannot mix incoming spanID in V1 format and sending my spans in V2 format.</p>