| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package cloudtail | 5 package cloudtail |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "encoding/json" | 8 "encoding/json" |
| 9 "fmt" | 9 "fmt" |
| 10 "net/http" | 10 "net/http" |
| 11 "os" | 11 "os" |
| 12 "time" | 12 "time" |
| 13 | 13 |
| 14 "github.com/luci/luci-go/common/logging" | 14 "github.com/luci/luci-go/common/logging" |
| 15 "github.com/luci/luci-go/common/tsmon/field" | 15 "github.com/luci/luci-go/common/tsmon/field" |
| 16 "github.com/luci/luci-go/common/tsmon/metric" | 16 "github.com/luci/luci-go/common/tsmon/metric" |
| 17 "github.com/luci/luci-go/common/tsmon/types" |
| 17 "golang.org/x/net/context" | 18 "golang.org/x/net/context" |
| 18 cloudlog "google.golang.org/api/logging/v1beta3" | 19 cloudlog "google.golang.org/api/logging/v1beta3" |
| 19 ) | 20 ) |
| 20 | 21 |
| 21 // DefaultResourceType is used by NewClient if ClientOptions doesn't specify | 22 // DefaultResourceType is used by NewClient if ClientOptions doesn't specify |
| 22 // ResourceType. | 23 // ResourceType. |
| 23 const DefaultResourceType = "machine" | 24 const DefaultResourceType = "machine" |
| 24 | 25 |
| 25 // Entry is a single log entry. It can be a text message, or a JSONish struct. | 26 // Entry is a single log entry. It can be a text message, or a JSONish struct. |
| 26 type Entry struct { | 27 type Entry struct { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 // LogID identifies what sort of log this is. Must be set. | 71 // LogID identifies what sort of log this is. Must be set. |
| 71 LogID string | 72 LogID string |
| 72 | 73 |
| 73 // Debug is true to print log entries to stdout instead of sending them. | 74 // Debug is true to print log entries to stdout instead of sending them. |
| 74 Debug bool | 75 Debug bool |
| 75 } | 76 } |
| 76 | 77 |
| 77 var ( | 78 var ( |
| 78 entriesCounter = metric.NewCounter("cloudtail/log_entries", | 79 entriesCounter = metric.NewCounter("cloudtail/log_entries", |
| 79 "Log entries processed", | 80 "Log entries processed", |
| 81 types.MetricMetadata{}, |
| 80 field.String("log"), | 82 field.String("log"), |
| 81 field.String("resource_type"), | 83 field.String("resource_type"), |
| 82 field.String("resource_id"), | 84 field.String("resource_id"), |
| 83 field.String("severity")) | 85 field.String("severity")) |
| 84 writesCounter = metric.NewCounter("cloudtail/api_writes", | 86 writesCounter = metric.NewCounter("cloudtail/api_writes", |
| 85 "Writes to Cloud Logging API", | 87 "Writes to Cloud Logging API", |
| 88 types.MetricMetadata{}, |
| 86 field.String("log"), | 89 field.String("log"), |
| 87 field.String("resource_type"), | 90 field.String("resource_type"), |
| 88 field.String("resource_id"), | 91 field.String("resource_id"), |
| 89 field.String("result")) | 92 field.String("result")) |
| 90 ) | 93 ) |
| 91 | 94 |
| 92 // NewClient returns new object that knows how to push log entries to a single | 95 // NewClient returns new object that knows how to push log entries to a single |
| 93 // log in Cloud Logging. | 96 // log in Cloud Logging. |
| 94 func NewClient(opts ClientOptions) (Client, error) { | 97 func NewClient(opts ClientOptions) (Client, error) { |
| 95 if opts.Logger == nil { | 98 if opts.Logger == nil { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 entriesCounter.Add(c.ctx, 1, c.opts.LogID, c.opts.ResourceType,
c.opts.ResourceID, metadata.Severity) | 187 entriesCounter.Add(c.ctx, 1, c.opts.LogID, c.opts.ResourceType,
c.opts.ResourceID, metadata.Severity) |
| 185 } | 188 } |
| 186 if err := c.writeFunc(c.opts.ProjectID, c.opts.LogID, &req); err != nil
{ | 189 if err := c.writeFunc(c.opts.ProjectID, c.opts.LogID, &req); err != nil
{ |
| 187 writesCounter.Add(c.ctx, 1, c.opts.LogID, c.opts.ResourceType, c
.opts.ResourceID, "failure") | 190 writesCounter.Add(c.ctx, 1, c.opts.LogID, c.opts.ResourceType, c
.opts.ResourceID, "failure") |
| 188 return err | 191 return err |
| 189 } | 192 } |
| 190 | 193 |
| 191 writesCounter.Add(c.ctx, 1, c.opts.LogID, c.opts.ResourceType, c.opts.Re
sourceID, "success") | 194 writesCounter.Add(c.ctx, 1, c.opts.LogID, c.opts.ResourceType, c.opts.Re
sourceID, "success") |
| 192 return nil | 195 return nil |
| 193 } | 196 } |
| OLD | NEW |