Skip to content

Conversation

@sususu98
Copy link
Contributor

Summary

  • Fix REQUEST INFO timestamp to capture when request first arrives (not at log write time)
  • Add API RESPONSE timestamp when upstream response arrives
  • Enable accurate measurement of backend response latency in error logs

Fixes #1299

Changes

File Change
request_logging.go Add Timestamp field to RequestInfo, set at middleware initialization
response_writer.go Add extractAPIResponseTimestamp(), pass timestamps through logging chain
request_logger.go Update LogRequestWithOptions() and writeAPISection() to accept/output timestamps
handlers.go Set API_RESPONSE_TIMESTAMP in appendAPIResponse() on first response
gemini-cli_handlers.go Set API_RESPONSE_TIMESTAMP in Gemini CLI handler

Example Output

=== REQUEST INFO ===
Timestamp: 2026-01-27T18:18:30.366575728+08:00
...

=== API RESPONSE ===
Timestamp: 2026-01-27T18:19:45.123456789+08:00
{...response...}

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @sususu98, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the logging system by introducing more accurate timestamping for both incoming requests and their corresponding API responses. By capturing these events closer to their actual occurrence, the system gains the ability to precisely measure and analyze the latency of backend responses, which is crucial for debugging and performance monitoring, especially in error scenarios.

Highlights

  • Timestamp Accuracy: The request timestamp is now captured precisely when the request first arrives, rather than at the time of log writing.
  • API Response Timestamp: A new timestamp is added to record when the upstream API response is received.
  • Latency Measurement: These changes enable more accurate measurement of backend response latency, particularly useful for error logs.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively adds timestamps to request and API response logs, which is a great step for monitoring backend latency. The implementation correctly captures the request timestamp at the beginning of the middleware chain. However, there are a couple of issues that undermine the feature's accuracy. For streaming responses, the API response timestamp is recorded at the end of the request, not when the first response chunk arrives. Additionally, for some logging paths, the request timestamp falls back to the log write time instead of using the correctly captured arrival time. I've added specific comments with suggestions on how to address these points.

Comment on lines +507 to +519
if requestTimestamp.IsZero() {
requestTimestamp = time.Now()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This fallback to time.Now() for a zero requestTimestamp contradicts one of the main goals of this PR, which is to 'Fix REQUEST INFO timestamp to capture when request first arrives (not at log write time)'. The LogRequest method calls this with a zero timestamp, causing the log to record the log write time instead of the request arrival time. To ensure the request arrival time is always used, you should consider updating the LogRequest method in the RequestLogger interface to accept a requestTimestamp and update its callers accordingly, or deprecate it in favor of LogRequestWithOptions.

return nil
}
w.apiResponse = bytes.Clone(apiResponse)
w.apiResponseTimestamp = time.Now()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

For streaming responses, this sets the apiResponseTimestamp to the time when the entire response has been processed and Finalize is called, not when the first chunk of the upstream API response arrived. This is inconsistent with the non-streaming implementation and the goal of measuring backend latency.

The timestamp is correctly captured for the first chunk in sdk/api/handlers/handlers.go's appendAPIResponse function and stored in the gin.Context. However, it's not being utilized here.

To fix this, you should:

  1. Modify the StreamingLogWriter interface's WriteAPIResponse method to accept a time.Time parameter: WriteAPIResponse(apiResponse []byte, timestamp time.Time) error.
  2. Update this FileStreamingLogWriter.WriteAPIResponse implementation to use the passed timestamp instead of time.Now().
  3. Update NoOpStreamingLogWriter.WriteAPIResponse to match the new interface.
  4. In internal/api/middleware/response_writer.go's Finalize method, extract the timestamp from the context using w.extractAPIResponseTimestamp(c) and pass it when calling w.streamWriter.WriteAPIResponse().

Comment on lines +343 to +361
ts, isExist := c.Get("API_RESPONSE_TIMESTAMP")
if !isExist {
return time.Time{}
}
if t, ok := ts.(time.Time); ok {
return t
}
return time.Time{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This function can be simplified. A type assertion in Go returns the zero value of the type if it fails. You can leverage this to make the code more concise.

	ts, _ := c.Get("API_RESPONSE_TIMESTAMP")
	t, _ := ts.(time.Time)
	return t

…ming

Previously:
- REQUEST INFO timestamp was captured at log write time (not request arrival)
- API RESPONSE had NO timestamp at all

This fix:
- Captures REQUEST INFO timestamp when request first arrives
- Adds API RESPONSE timestamp when upstream response arrives

Changes:
- Add Timestamp field to RequestInfo, set at middleware initialization
- Set API_RESPONSE_TIMESTAMP in appendAPIResponse() and gemini handler
- Pass timestamps through logging chain to writeNonStreamingLog()
- Add timestamp output to API RESPONSE section

This enables accurate measurement of backend response latency in error logs.
@sususu98 sususu98 force-pushed the feat/log-api-response-timestamp branch from 3b0238a to d87c704 Compare January 29, 2026 14:22
…ps required

- Add firstChunkTimestamp field to ResponseWriterWrapper for sync capture
- Capture TTFB in Write() and WriteString() before async channel send
- Add SetFirstChunkTimestamp() to StreamingLogWriter interface
- Make requestTimestamp/apiResponseTimestamp required in LogRequest()
- Remove timestamp capture from WriteAPIResponse() (now via setter)
- Fix Gemini handler to set API_RESPONSE_TIMESTAMP before writing response

This ensures accurate TTFB measurement for all streaming API formats
(OpenAI, Gemini, Claude) by capturing timestamp synchronously when
the first response chunk arrives, not when the stream finalizes.
@sususu98 sususu98 force-pushed the feat/log-api-response-timestamp branch from d87c704 to 295f34d Compare January 29, 2026 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants