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
binarylog: export Sink #3879
Merged
Merged
binarylog: export Sink #3879
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* | ||
* Copyright 2020 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
// Package binarylog implementation binary logging as defined in | ||
// https://github.com/grpc/proposal/blob/master/A16-binary-logging.md. | ||
// | ||
// Notice: All APIs in this package are experimental. | ||
package binarylog | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
|
||
pb "google.golang.org/grpc/binarylog/grpc_binarylog_v1" | ||
iblog "google.golang.org/grpc/internal/binarylog" | ||
) | ||
|
||
// SetSink sets the destination for the binary log entries. | ||
// | ||
// NOTE: this function must only be called during initialization time (i.e. in | ||
// an init() function), and is not thread-safe. | ||
func SetSink(s Sink) { | ||
if iblog.DefaultSink != nil { | ||
iblog.DefaultSink.Close() | ||
} | ||
iblog.DefaultSink = s | ||
} | ||
|
||
// Sink represents the destination for the binary log entries. | ||
type Sink interface { | ||
// Write marshals the log entry and writes it to the destination. The format | ||
// is not specified, but should have sufficient information to rebuild the | ||
// entry. Some options are: proto bytes, or proto json. | ||
// | ||
// Note this function needs to be thread-safe. | ||
Write(*pb.GrpcLogEntry) error | ||
// Close closes this sink and cleans up resources (e.g. the flushing | ||
// goroutine). | ||
Close() error | ||
} | ||
|
||
// NewTempFileSink creates a temp file and returns a Sink that writes to this | ||
// file. | ||
func NewTempFileSink() (Sink, error) { | ||
tempFile, err := ioutil.TempFile("/tmp", "grpcgo_binarylog_*.txt") | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create temp file: %v", err) | ||
} | ||
return iblog.NewBufferedSink(tempFile), nil | ||
} |
This file contains 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
This file contains 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
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.
Would it make more sense to get the name of the file to write to from the user? The way it is right now, the user would just to go over to the
tmp
dir and figure out the file for themselves.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.
That is reasonable, and in that case we would just export
NewBufferedSink
from internal. But there's no tool to read the file content anyway, so I would expect users to just implement their own Sink.I'm leaving this as is for now (I added comment to remind us later). We can decide what to export based on what users want.