-
Notifications
You must be signed in to change notification settings - Fork 2
Clean up logging in controller and wrap errors #132
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
Open
yushan8
wants to merge
2
commits into
main
Choose a base branch
from
update-logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,7 +52,8 @@ func (c *controller) GetChangedTargets(request *pb.GetChangedTargetsRequest, str | |
| } | ||
| }() | ||
| if err := validateGetChangedTargetsRequest(request); err != nil { | ||
| return common.WithReason(common.FailureReasonValidation, common.ErrorTypeUser, err) | ||
| c.logger.Error("GetChangedTargets: validation failed", zap.Error(err)) | ||
| return fmt.Errorf("validate request: %w", err) | ||
| } | ||
| scope = scope.Tagged(map[string]string{"repo": common.ToShortRemote(request.GetFirstRevision().GetRemote())}) | ||
| ctx, cancelLink := c.linkRequestCtx(stream.Context()) | ||
|
|
@@ -63,8 +64,6 @@ func (c *controller) GetChangedTargets(request *pb.GetChangedTargetsRequest, str | |
| zap.Any("second_revision", request.GetSecondRevision()), | ||
| ) | ||
|
|
||
| logger.Info("GetChangedTargets: Processing request") | ||
|
|
||
| // Default max_distance to -1 (no filtering) when the client omits OutputConfig | ||
| // entirely. When OutputConfig is supplied, take max_distance at face value — | ||
| // see proto/tango.proto OutputConfig.max_distance for the wire-default caveat. | ||
|
|
@@ -82,14 +81,14 @@ func (c *controller) GetChangedTargets(request *pb.GetChangedTargetsRequest, str | |
| cacheStart := time.Now() | ||
| treehash1, treehash2, err := readTreehashParallel(ctx, c.storage, request.GetFirstRevision(), request.GetSecondRevision()) | ||
| if err != nil { | ||
| logger.Error("GetChangedTargets: Failed to read revision treehash", zap.Error(err)) | ||
| return common.WithReason(failureReasonTreehashRead, common.ErrorTypeInfra, err) | ||
| logger.Error("GetChangedTargets: failed to read revision treehash", zap.Error(err)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same issue here |
||
| return fmt.Errorf("read revision treehash: %w", err) | ||
| } | ||
| if treehash1 != "" && treehash2 != "" { | ||
| cacheKey := common.GetComparedTargetsCachePath(request.GetFirstRevision().GetRemote(), treehash1, treehash2, request.GetRequestOptions()) | ||
| cachedReader, cacheErr := storage.NewChangedTargetsReader(ctx, c.storage, cacheKey) | ||
| if cacheErr != nil && !storage.IsNotFound(cacheErr) { | ||
| logger.Warn("GetChangedTargets: Failed to read from cache, proceeding to compute", zap.Error(cacheErr)) | ||
| logger.Warn("GetChangedTargets: failed to read from cache, proceeding to compute", zap.Error(cacheErr)) | ||
| } else if cachedReader != nil { | ||
| // Buffer all responses before sending any. A concurrent goroutine write may have | ||
| // left a partial blob in storage; buffering lets us detect corruption and fall | ||
|
|
@@ -98,9 +97,8 @@ func (c *controller) GetChangedTargets(request *pb.GetChangedTargetsRequest, str | |
| var readErr error | ||
| for { | ||
| if err := ctx.Err(); err != nil { | ||
| cachedReader.Close() | ||
| // Client gave up while we were draining the cache. Surface as a user-cancelled error. | ||
| return common.WithReason(common.FailureReasonCancelled, common.ErrorTypeUser, err) | ||
| closeErr := cachedReader.Close() | ||
| return errors.Join(err, closeErr) | ||
| } | ||
| var resp *pb.GetChangedTargetsResponse | ||
| resp, readErr = cachedReader.Read() | ||
|
|
@@ -113,26 +111,21 @@ func (c *controller) GetChangedTargets(request *pb.GetChangedTargetsRequest, str | |
| } | ||
| cached = append(cached, resp) | ||
| } | ||
| cachedReader.Close() | ||
| if closeErr := cachedReader.Close(); closeErr != nil { | ||
| logger.Warn("GetChangedTargets: failed to close cached reader", zap.Error(closeErr)) | ||
| } | ||
|
|
||
| if readErr != nil { | ||
| // Blob is corrupt (likely an incomplete write). Log and fall through to recompute. | ||
| logger.Warn("GetChangedTargets: Cached result is incomplete, recomputing", zap.Error(readErr)) | ||
| logger.Warn("GetChangedTargets: cached result is incomplete, recomputing", zap.Error(readErr)) | ||
| } else { | ||
| cacheReadDuration := time.Since(cacheStart) | ||
| logger.Info("GetChangedTargets: Cache hit, streaming from storage", | ||
| zap.Duration("cache_read_duration", cacheReadDuration), | ||
| ) | ||
| scope.Counter("changed_targets_cache_hit").Inc(1) | ||
| scope.Timer("cache_read_duration").Record(cacheReadDuration) | ||
| if sendErr := sendTrimmedChangedTargets(stream, cached, maxDist, request.GetOutputConfig()); sendErr != nil { | ||
| logger.Error("GetChangedTargets: Failed to send cached response", zap.Error(sendErr)) | ||
| return common.WithReason(failureReasonSend, common.ErrorTypeInfra, fmt.Errorf("failed to send cached response: %w", sendErr)) | ||
| logger.Error("GetChangedTargets: failed to send cached response", zap.Error(sendErr)) | ||
| return fmt.Errorf("failed to send cached response: %w", sendErr) | ||
| } | ||
| totalDuration := time.Since(start) | ||
| logger.Info("GetChangedTargets: Successfully streamed from cache", | ||
| zap.Duration("total_duration", totalDuration), | ||
| ) | ||
| scope.Timer("total_duration").Record(totalDuration) | ||
| scope.Histogram("total_duration.histogram", c.totalDurationBuckets).RecordDuration(totalDuration) | ||
| return nil | ||
|
|
@@ -223,14 +216,10 @@ func (c *controller) GetChangedTargets(request *pb.GetChangedTargetsRequest, str | |
| } | ||
|
|
||
| graphFetchDuration := time.Since(graphFetchStart) | ||
| logger.Info("GetChangedTargets: Both graphs fetched", | ||
| zap.Duration("graph_fetch_duration", graphFetchDuration), | ||
| ) | ||
| scope.Timer("graph_fetch_duration").Record(graphFetchDuration) | ||
|
|
||
| if ctx.Err() != nil { | ||
| // If the context was cancelled by the upstream, just return the original error without additional augmentation | ||
| return common.WithReason(common.FailureReasonCancelled, common.ErrorTypeUser, ctx.Err()) | ||
| return ctx.Err() | ||
| } | ||
|
|
||
| // Process errors, only aggregating the ones that are original ones and not a result of the other job being cancelled | ||
|
|
@@ -246,6 +235,7 @@ func (c *controller) GetChangedTargets(request *pb.GetChangedTargetsRequest, str | |
| } | ||
|
|
||
| if err != nil { | ||
| logger.Error("GetChangedTargets: failed to get target graphs", zap.Error(err)) | ||
| return err | ||
| } | ||
| firstGraph := jobs[0].graphStreamChunks | ||
|
|
@@ -255,21 +245,18 @@ func (c *controller) GetChangedTargets(request *pb.GetChangedTargetsRequest, str | |
| jobs[1].graphStreamChunks = nil | ||
|
|
||
| compareStart := time.Now() | ||
| changedTargetsResponses, err := c.compareTargetGraphs(ctx, logger, firstGraph, secondGraph, maxDist) | ||
| changedTargetsResponses, err := c.compareTargetGraphs(ctx, firstGraph, secondGraph, maxDist) | ||
| // Allow GC of raw graph data while the caching goroutine runs. | ||
| firstGraph = nil | ||
| secondGraph = nil | ||
| if err != nil { | ||
| if ctx.Err() != nil { | ||
| return common.WithReason(common.FailureReasonCancelled, common.ErrorTypeUser, ctx.Err()) | ||
| return ctx.Err() | ||
| } | ||
| logger.Error("GetChangedTargets: Failed to compare target graphs", zap.Error(err)) | ||
| return common.WithReason(failureReasonCompare, common.ErrorTypeInfra, fmt.Errorf("failed to compare target graphs: %w", err)) | ||
| logger.Error("GetChangedTargets: failed to compare target graphs", zap.Error(err)) | ||
| return fmt.Errorf("failed to compare target graphs: %w", err) | ||
| } | ||
| compareDuration := time.Since(compareStart) | ||
| logger.Info("GetChangedTargets: Target graphs compared", | ||
| zap.Duration("compare_duration", compareDuration), | ||
| ) | ||
| scope.Timer("compare_duration").Record(compareDuration) | ||
|
|
||
| // Cache the computed result concurrently so it doesn't block the stream send. | ||
|
|
@@ -306,17 +293,13 @@ func (c *controller) GetChangedTargets(request *pb.GetChangedTargetsRequest, str | |
|
|
||
| sendStart := time.Now() | ||
| if err := sendTrimmedChangedTargets(stream, changedTargetsResponses, maxDist, request.GetOutputConfig()); err != nil { | ||
| logger.Error("GetChangedTargets: Failed to send response", zap.Error(err)) | ||
| return common.WithReason(failureReasonSend, common.ErrorTypeInfra, fmt.Errorf("failed to send response: %w", err)) | ||
| logger.Error("GetChangedTargets: failed to send response", zap.Error(err)) | ||
| return fmt.Errorf("failed to send response: %w", err) | ||
| } | ||
| sendDuration := time.Since(sendStart) | ||
| scope.Timer("send_duration").Record(sendDuration) | ||
|
|
||
| totalDuration := time.Since(start) | ||
| logger.Info("GetChangedTargets: Successfully processed request", | ||
| zap.Duration("send_duration", sendDuration), | ||
| zap.Duration("total_duration", totalDuration), | ||
| ) | ||
| scope.Timer("total_duration").Record(totalDuration) | ||
| scope.Histogram("total_duration.histogram", c.totalDurationBuckets).RecordDuration(totalDuration) | ||
| return nil | ||
|
|
@@ -331,10 +314,9 @@ func (c *controller) GetChangedTargets(request *pb.GetChangedTargetsRequest, str | |
| // targets get their distance from BFS over the reverse-dep graph. | ||
| // Output IDs are re-mapped into a canonical per-call namespace so the | ||
| // response metadata only carries the names actually referenced. | ||
| func (c *controller) compareTargetGraphs(ctx context.Context, logger *zap.Logger, firstGraph, secondGraph []*pb.GetTargetGraphResponse, maxDist int32) ([]*pb.GetChangedTargetsResponse, error) { | ||
| func (c *controller) compareTargetGraphs(ctx context.Context, firstGraph, secondGraph []*pb.GetTargetGraphResponse, maxDist int32) ([]*pb.GetChangedTargetsResponse, error) { | ||
| start := time.Now() | ||
| scope := c.scope.SubScope("compare_target_graphs") | ||
| logger.Info("compareTargetGraphs: Computing differences between target graphs") | ||
|
|
||
| // 1) Extract targets and metadata; index by canonical names | ||
| indexStart := time.Now() | ||
|
|
@@ -592,9 +574,6 @@ func (c *controller) compareTargetGraphs(ctx context.Context, logger *zap.Logger | |
| }) | ||
| } | ||
| totalDuration := time.Since(start) | ||
| logger.Info("compareTargetGraphs: Done", | ||
| zap.Duration("total_duration", totalDuration), | ||
| ) | ||
| scope.Timer("total_duration").Record(totalDuration) | ||
| return results, nil | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please don't log and return the errors. Suppose you have a chain of funcs each returning an error:
func1 -> func2 -> func3
and you log before returning the error each time.
You'll end up with logs that look like:
"func3: some error happened"
"func2: error while doing something: func3: some error happened"
"func1: another thing: func2: error while doing something: func3: some error happened"
This is completely redundant and likely not what you want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All errors are logged once at the controller level now. The rest of the functions errors logs were removed #127. Shouldn't we have service logs for errors at this level?