Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add one-line logs with time #392

Merged

Conversation

carrascoacd
Copy link
Contributor

@carrascoacd carrascoacd commented Oct 4, 2024

Context

To properly observe our systems in unary calls, using one-line logs allows the user to see the elapsed time in a single place without joining two separate lines.

Solution

Use timer to measure time and log while maintaining the same behavior.

cc/ @polvalente

@carrascoacd carrascoacd changed the title Add on-line logs with time Add one-line logs with time Oct 4, 2024
def call(%{grpc_type: grpc_type} = stream, req, next, opts) do
{time, result} = :timer.tc(next, [stream, req])
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe we need a try-rescue now.
Perhaps instead of timer.tc, keep the previous code structure, and log either on the rescue or after block depending on whether it raised an exception or was successful.

This will probably minimize the code changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why do you think a try is necessary here? And what about using telemetry instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

Because otherwise you won't get any logs for the call anymore. In the previous version you would get the starting but not the terminal log.

I think telemetry would be an addition, but doesn't substitute the logs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah ok, I get you. I'll improve it.

Related to telemetry what I mean is decoupling the call from the interceptor. In the same way phoenix instruments traces (see opentelemetry_phoenix). It will allow clients to make structured logging as well. Basically you emit an event instead of logging and we subscribe in the interceptor to the event, then log in the handle function.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think having the log message coupled here is the more natural way, although a telemetry event being published alongside it wouldn't hurt.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've applied your suggestions regarding safer logs with try/rescue keeping the same way we were measuring time, without timex.

@polvalente polvalente merged commit c26acb9 into elixir-grpc:master Oct 8, 2024
8 checks passed
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