1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
// Copyright 2016 Google LLC
//
// 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 logadmin
import (
"context"
"fmt"
vkit "cloud.google.com/go/logging/apiv2"
"google.golang.org/api/iterator"
logpb "google.golang.org/genproto/googleapis/logging/v2"
)
// Metric describes a logs-based metric. The value of the metric is the
// number of log entries that match a logs filter.
//
// Metrics are a feature of Stackdriver Monitoring.
// See https://cloud.google.com/monitoring/api/v3/metrics for more about them.
type Metric struct {
// ID is a client-assigned metric identifier. Example:
// "severe_errors". Metric identifiers are limited to 1000
// characters and can include only the following characters: A-Z,
// a-z, 0-9, and the special characters _-.,+!*',()%/\. The
// forward-slash character (/) denotes a hierarchy of name pieces,
// and it cannot be the first character of the name.
ID string
// Description describes this metric. It is used in documentation.
Description string
// Filter is an advanced logs filter (see
// https://cloud.google.com/logging/docs/view/advanced_filters).
// Example: "logName:syslog AND severity>=ERROR".
Filter string
}
// CreateMetric creates a logs-based metric.
func (c *Client) CreateMetric(ctx context.Context, m *Metric) error {
_, err := c.mClient.CreateLogMetric(ctx, &logpb.CreateLogMetricRequest{
Parent: c.parent,
Metric: toLogMetric(m),
})
return err
}
// DeleteMetric deletes a log-based metric.
// The provided metric ID is the metric identifier. For example, "severe_errors".
func (c *Client) DeleteMetric(ctx context.Context, metricID string) error {
return c.mClient.DeleteLogMetric(ctx, &logpb.DeleteLogMetricRequest{
MetricName: c.metricPath(metricID),
})
}
// Metric gets a logs-based metric.
// The provided metric ID is the metric identifier. For example, "severe_errors".
// Requires ReadScope or AdminScope.
func (c *Client) Metric(ctx context.Context, metricID string) (*Metric, error) {
lm, err := c.mClient.GetLogMetric(ctx, &logpb.GetLogMetricRequest{
MetricName: c.metricPath(metricID),
})
if err != nil {
return nil, err
}
return fromLogMetric(lm), nil
}
// UpdateMetric creates a logs-based metric if it does not exist, or updates an
// existing one.
func (c *Client) UpdateMetric(ctx context.Context, m *Metric) error {
_, err := c.mClient.UpdateLogMetric(ctx, &logpb.UpdateLogMetricRequest{
MetricName: c.metricPath(m.ID),
Metric: toLogMetric(m),
})
return err
}
func (c *Client) metricPath(metricID string) string {
return fmt.Sprintf("%s/metrics/%s", c.parent, metricID)
}
// Metrics returns a MetricIterator for iterating over all Metrics in the Client's project.
// Requires ReadScope or AdminScope.
func (c *Client) Metrics(ctx context.Context) *MetricIterator {
it := &MetricIterator{
it: c.mClient.ListLogMetrics(ctx, &logpb.ListLogMetricsRequest{Parent: c.parent}),
}
it.pageInfo, it.nextFunc = iterator.NewPageInfo(
it.fetch,
func() int { return len(it.items) },
func() interface{} { b := it.items; it.items = nil; return b })
return it
}
// A MetricIterator iterates over Metrics.
type MetricIterator struct {
it *vkit.LogMetricIterator
pageInfo *iterator.PageInfo
nextFunc func() error
items []*Metric
}
// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
func (it *MetricIterator) PageInfo() *iterator.PageInfo { return it.pageInfo }
// Next returns the next result. Its second return value is Done if there are
// no more results. Once Next returns Done, all subsequent calls will return
// Done.
func (it *MetricIterator) Next() (*Metric, error) {
if err := it.nextFunc(); err != nil {
return nil, err
}
item := it.items[0]
it.items = it.items[1:]
return item, nil
}
func (it *MetricIterator) fetch(pageSize int, pageToken string) (string, error) {
return iterFetch(pageSize, pageToken, it.it.PageInfo(), func() error {
item, err := it.it.Next()
if err != nil {
return err
}
it.items = append(it.items, fromLogMetric(item))
return nil
})
}
func toLogMetric(m *Metric) *logpb.LogMetric {
return &logpb.LogMetric{
Name: m.ID,
Description: m.Description,
Filter: m.Filter,
}
}
func fromLogMetric(lm *logpb.LogMetric) *Metric {
return &Metric{
ID: lm.Name,
Description: lm.Description,
Filter: lm.Filter,
}
}
|