0

I am running a Java application on Google App Engine standard environment.

Looking at the logs (in particular request_log), I can see a couple of interesting IDs such as trace_id and request_id for each request.

Couple of questions:

  • How are they different? Are they unique?
  • How to obtain them in application code (for the current request)? I would like to log them to a different Stackdriver dataset and be able to correlate the data
Nikola Mihajlović
  • 2,286
  • 2
  • 19
  • 23

3 Answers3

1

In the request handler you can access the headers and pull out X-Cloud-Trace-Context form the HttpServletRequest

@WebServlet(name = "requests", description = "Requests: Trivial request", urlPatterns = "/requests")
public class RequestsServlet extends HttpServlet {
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    req.getHeader("X-Cloud-Trace-Context")
  }
}
Kirk Kelsey
  • 4,259
  • 1
  • 23
  • 26
  • I want to include documentation for completeness: https://cloud.google.com/appengine/docs/flexible/java/reference/request-headers. This document does not exist for standard environment, but it is pretty much the same – Nikola Mihajlović Jul 28 '18 at 01:37
0

You can set a trace ID when you're doing the HTTP call with the X-Cloud-Trace-Context header, while the request id is the way GCP has to uniquely identify each request.

All requests have an ID, while only some have traces. Only the traced requests will send their info to the Stackdriver Trace API.

As for the second question, I don't fully understand it since the Stackdriver Trace API will return all the information of the traced requests, including the request ID. The Stackdriver Logging APi will return the full log for each request, meaning that if they're traced, you'll receive both the request ID and the trace ID. There is no need to correlate those two fields, as you'll always get them together (if both exist).

Jofre
  • 3,718
  • 1
  • 23
  • 31
  • Sorry if I was not clear. I would like to log additional data (to a different Stackdriver dataset) from my web application and would like to get request ID and trace ID for the current request programmatically in code – Nikola Mihajlović Jun 20 '18 at 19:39
  • Yes, I didn't understand you. I know that [Go](https://godoc.org/google.golang.org/appengine#RequestID) has a way to get the request ID for sure (that's the language I use), so I'm assuming the same option should be available for Java. – Jofre Jun 20 '18 at 20:08
0

The TraceID is simply a trace identifier for a specific request to your App Engine Application, while the RequestID is a globally unique identifier for a request. The RequestID of a Stackdriver log depends on the start time of the request. Meanwhile the TraceID identifies the associated Stackdriver trace to a Specific request Log entry See here.

You can find details about the terminologies used for a Specific Stackdriver request log in the Link.

The RequestID is a unique identifier and it is globally unique. The TraceID on the other hand is unique to the Stackdriver Trace — read more here.

With the Stackdriver Trace API, you can use the GetTraceRequest method within the Package google.devtools.cloudtrace.v1 to retrieve the Trace ID and the Request ID.

oakinlaja
  • 826
  • 6
  • 10