vendor: update all dependencies
This commit is contained in:
+119
-30
@@ -36,6 +36,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"cloud.google.com/go/compute/metadata"
|
||||
"cloud.google.com/go/internal/version"
|
||||
vkit "cloud.google.com/go/logging/apiv2"
|
||||
"cloud.google.com/go/logging/internal"
|
||||
@@ -96,6 +97,10 @@ type Client struct {
|
||||
loggers sync.WaitGroup // so we can wait for loggers to close
|
||||
closed bool
|
||||
|
||||
mu sync.Mutex
|
||||
nErrs int // number of errors we saw
|
||||
lastErr error // last error we saw
|
||||
|
||||
// OnError is called when an error occurs in a call to Log or Flush. The
|
||||
// error may be due to an invalid Entry, an overflow because BufferLimit
|
||||
// was reached (in which case the error will be ErrOverflow) or an error
|
||||
@@ -171,18 +176,43 @@ func init() {
|
||||
// log entry "ping" to a log named "ping".
|
||||
func (c *Client) Ping(ctx context.Context) error {
|
||||
ent := &logpb.LogEntry{
|
||||
Payload: &logpb.LogEntry_TextPayload{"ping"},
|
||||
Payload: &logpb.LogEntry_TextPayload{TextPayload: "ping"},
|
||||
Timestamp: unixZeroTimestamp, // Identical timestamps and insert IDs are both
|
||||
InsertId: "ping", // necessary for the service to dedup these entries.
|
||||
}
|
||||
_, err := c.client.WriteLogEntries(ctx, &logpb.WriteLogEntriesRequest{
|
||||
LogName: internal.LogPath(c.parent(), "ping"),
|
||||
Resource: &mrpb.MonitoredResource{Type: "global"},
|
||||
Resource: globalResource(c.projectID),
|
||||
Entries: []*logpb.LogEntry{ent},
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// error puts the error on the client's error channel
|
||||
// without blocking, and records summary error info.
|
||||
func (c *Client) error(err error) {
|
||||
select {
|
||||
case c.errc <- err:
|
||||
default:
|
||||
}
|
||||
c.mu.Lock()
|
||||
c.lastErr = err
|
||||
c.nErrs++
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
func (c *Client) extractErrorInfo() error {
|
||||
var err error
|
||||
c.mu.Lock()
|
||||
if c.lastErr != nil {
|
||||
err = fmt.Errorf("saw %d errors; last: %v", c.nErrs, c.lastErr)
|
||||
c.nErrs = 0
|
||||
c.lastErr = nil
|
||||
}
|
||||
c.mu.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
// A Logger is used to write log messages to a single log. It can be configured
|
||||
// with a log ID, common monitored resource, and a set of common labels.
|
||||
type Logger struct {
|
||||
@@ -202,14 +232,58 @@ type LoggerOption interface {
|
||||
}
|
||||
|
||||
// CommonResource sets the monitored resource associated with all log entries
|
||||
// written from a Logger. If not provided, a resource of type "global" is used.
|
||||
// This value can be overridden by setting an Entry's Resource field.
|
||||
// written from a Logger. If not provided, the resource is automatically
|
||||
// detected based on the running environment. This value can be overridden
|
||||
// per-entry by setting an Entry's Resource field.
|
||||
func CommonResource(r *mrpb.MonitoredResource) LoggerOption { return commonResource{r} }
|
||||
|
||||
type commonResource struct{ *mrpb.MonitoredResource }
|
||||
|
||||
func (r commonResource) set(l *Logger) { l.commonResource = r.MonitoredResource }
|
||||
|
||||
var detectedResource struct {
|
||||
pb *mrpb.MonitoredResource
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func detectResource() *mrpb.MonitoredResource {
|
||||
detectedResource.once.Do(func() {
|
||||
if !metadata.OnGCE() {
|
||||
return
|
||||
}
|
||||
projectID, err := metadata.ProjectID()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
id, err := metadata.InstanceID()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
zone, err := metadata.Zone()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
detectedResource.pb = &mrpb.MonitoredResource{
|
||||
Type: "gce_instance",
|
||||
Labels: map[string]string{
|
||||
"project_id": projectID,
|
||||
"instance_id": id,
|
||||
"zone": zone,
|
||||
},
|
||||
}
|
||||
})
|
||||
return detectedResource.pb
|
||||
}
|
||||
|
||||
func globalResource(projectID string) *mrpb.MonitoredResource {
|
||||
return &mrpb.MonitoredResource{
|
||||
Type: "global",
|
||||
Labels: map[string]string{
|
||||
"project_id": projectID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// CommonLabels are labels that apply to all log entries written from a Logger,
|
||||
// so that you don't have to repeat them in each log entry's Labels field. If
|
||||
// any of the log entries contains a (key, value) with the same key that is in
|
||||
@@ -288,10 +362,14 @@ func (b bufferedByteLimit) set(l *Logger) { l.bundler.BufferedByteLimit = int(b)
|
||||
// characters: [A-Za-z0-9]; and punctuation characters: forward-slash,
|
||||
// underscore, hyphen, and period.
|
||||
func (c *Client) Logger(logID string, opts ...LoggerOption) *Logger {
|
||||
r := detectResource()
|
||||
if r == nil {
|
||||
r = globalResource(c.projectID)
|
||||
}
|
||||
l := &Logger{
|
||||
client: c,
|
||||
logName: internal.LogPath(c.parent(), logID),
|
||||
commonResource: &mrpb.MonitoredResource{Type: "global"},
|
||||
commonResource: r,
|
||||
}
|
||||
// TODO(jba): determine the right context for the bundle handler.
|
||||
ctx := context.TODO()
|
||||
@@ -314,7 +392,7 @@ func (c *Client) Logger(logID string, opts ...LoggerOption) *Logger {
|
||||
go func() {
|
||||
defer c.loggers.Done()
|
||||
<-c.donec
|
||||
l.bundler.Stop()
|
||||
l.bundler.Flush()
|
||||
}()
|
||||
return l
|
||||
}
|
||||
@@ -332,7 +410,7 @@ func (w severityWriter) Write(p []byte) (n int, err error) {
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// Close closes the client.
|
||||
// Close waits for all opened loggers to be flushed and closes the client.
|
||||
func (c *Client) Close() error {
|
||||
if c.closed {
|
||||
return nil
|
||||
@@ -341,9 +419,12 @@ func (c *Client) Close() error {
|
||||
c.loggers.Wait() // wait for all bundlers to flush and close
|
||||
// Now there can be no more errors.
|
||||
close(c.errc) // terminate error goroutine
|
||||
// Return only the first error. Since all clients share an underlying connection,
|
||||
// Closes after the first always report a "connection is closing" error.
|
||||
err := c.client.Close()
|
||||
// Prefer logging errors to close errors.
|
||||
err := c.extractErrorInfo()
|
||||
err2 := c.client.Close()
|
||||
if err == nil {
|
||||
err = err2
|
||||
}
|
||||
c.closed = true
|
||||
return err
|
||||
}
|
||||
@@ -453,6 +534,11 @@ type Entry struct {
|
||||
// by the client when reading entries. It is an error to set it when
|
||||
// writing entries.
|
||||
Resource *mrpb.MonitoredResource
|
||||
|
||||
// Trace is the resource name of the trace associated with the log entry,
|
||||
// if any. If it contains a relative resource name, the name is assumed to
|
||||
// be relative to //tracing.googleapis.com.
|
||||
Trace string
|
||||
}
|
||||
|
||||
// HTTPRequest contains an http.Request as well as additional
|
||||
@@ -477,6 +563,10 @@ type HTTPRequest struct {
|
||||
// received until the response was sent.
|
||||
Latency time.Duration
|
||||
|
||||
// LocalIP is the IP address (IPv4 or IPv6) of the origin server that the request
|
||||
// was sent to.
|
||||
LocalIP string
|
||||
|
||||
// RemoteIP is the IP address (IPv4 or IPv6) of the client that issued the
|
||||
// HTTP request. Examples: "192.168.1.1", "FE80::0202:B3FF:FE1E:8329".
|
||||
RemoteIP string
|
||||
@@ -507,6 +597,7 @@ func fromHTTPRequest(r *HTTPRequest) *logtypepb.HttpRequest {
|
||||
Status: int32(r.Status),
|
||||
ResponseSize: r.ResponseSize,
|
||||
UserAgent: r.Request.UserAgent(),
|
||||
ServerIp: r.LocalIP,
|
||||
RemoteIp: r.RemoteIP, // TODO(jba): attempt to parse http.Request.RemoteAddr?
|
||||
Referer: r.Request.Referer(),
|
||||
CacheHit: r.CacheHit,
|
||||
@@ -552,21 +643,21 @@ func jsonMapToProtoStruct(m map[string]interface{}) *structpb.Struct {
|
||||
func jsonValueToStructValue(v interface{}) *structpb.Value {
|
||||
switch x := v.(type) {
|
||||
case bool:
|
||||
return &structpb.Value{Kind: &structpb.Value_BoolValue{x}}
|
||||
return &structpb.Value{Kind: &structpb.Value_BoolValue{BoolValue: x}}
|
||||
case float64:
|
||||
return &structpb.Value{Kind: &structpb.Value_NumberValue{x}}
|
||||
return &structpb.Value{Kind: &structpb.Value_NumberValue{NumberValue: x}}
|
||||
case string:
|
||||
return &structpb.Value{Kind: &structpb.Value_StringValue{x}}
|
||||
return &structpb.Value{Kind: &structpb.Value_StringValue{StringValue: x}}
|
||||
case nil:
|
||||
return &structpb.Value{Kind: &structpb.Value_NullValue{}}
|
||||
case map[string]interface{}:
|
||||
return &structpb.Value{Kind: &structpb.Value_StructValue{jsonMapToProtoStruct(x)}}
|
||||
return &structpb.Value{Kind: &structpb.Value_StructValue{StructValue: jsonMapToProtoStruct(x)}}
|
||||
case []interface{}:
|
||||
var vals []*structpb.Value
|
||||
for _, e := range x {
|
||||
vals = append(vals, jsonValueToStructValue(e))
|
||||
}
|
||||
return &structpb.Value{Kind: &structpb.Value_ListValue{&structpb.ListValue{vals}}}
|
||||
return &structpb.Value{Kind: &structpb.Value_ListValue{ListValue: &structpb.ListValue{Values: vals}}}
|
||||
default:
|
||||
panic(fmt.Sprintf("bad type %T for JSON value", v))
|
||||
}
|
||||
@@ -594,17 +685,23 @@ func (l *Logger) LogSync(ctx context.Context, e Entry) error {
|
||||
func (l *Logger) Log(e Entry) {
|
||||
ent, err := toLogEntry(e)
|
||||
if err != nil {
|
||||
l.error(err)
|
||||
l.client.error(err)
|
||||
return
|
||||
}
|
||||
if err := l.bundler.Add(ent, proto.Size(ent)); err != nil {
|
||||
l.error(err)
|
||||
l.client.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Flush blocks until all currently buffered log entries are sent.
|
||||
func (l *Logger) Flush() {
|
||||
//
|
||||
// If any errors occurred since the last call to Flush from any Logger, or the
|
||||
// creation of the client if this is the first call, then Flush returns a non-nil
|
||||
// error with summary information about the errors. This information is unlikely to
|
||||
// be actionable. For more accurate error reporting, set Client.OnError.
|
||||
func (l *Logger) Flush() error {
|
||||
l.bundler.Flush()
|
||||
return l.client.extractErrorInfo()
|
||||
}
|
||||
|
||||
func (l *Logger) writeLogEntries(ctx context.Context, entries []*logpb.LogEntry) {
|
||||
@@ -616,16 +713,7 @@ func (l *Logger) writeLogEntries(ctx context.Context, entries []*logpb.LogEntry)
|
||||
}
|
||||
_, err := l.client.client.WriteLogEntries(ctx, req)
|
||||
if err != nil {
|
||||
l.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// error puts the error on the client's error channel
|
||||
// without blocking.
|
||||
func (l *Logger) error(err error) {
|
||||
select {
|
||||
case l.client.errc <- err:
|
||||
default:
|
||||
l.client.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -662,17 +750,18 @@ func toLogEntry(e Entry) (*logpb.LogEntry, error) {
|
||||
HttpRequest: fromHTTPRequest(e.HTTPRequest),
|
||||
Operation: e.Operation,
|
||||
Labels: e.Labels,
|
||||
Trace: e.Trace,
|
||||
}
|
||||
|
||||
switch p := e.Payload.(type) {
|
||||
case string:
|
||||
ent.Payload = &logpb.LogEntry_TextPayload{p}
|
||||
ent.Payload = &logpb.LogEntry_TextPayload{TextPayload: p}
|
||||
default:
|
||||
s, err := toProtoStruct(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ent.Payload = &logpb.LogEntry_JsonPayload{s}
|
||||
ent.Payload = &logpb.LogEntry_JsonPayload{JsonPayload: s}
|
||||
}
|
||||
return ent, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user