Skip to content
This repository was archived by the owner on Dec 3, 2024. It is now read-only.

Commit 711c86c

Browse files
authored
Merge pull request #13 from google/linting
cleanup: linting fixes and testing on PRs
2 parents f1fe255 + 14c7ba4 commit 711c86c

File tree

4 files changed

+129
-112
lines changed

4 files changed

+129
-112
lines changed

.github/workflows/go.yml

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
1-
name: Go
2-
on: [push]
1+
name: test
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches:
8+
- main
9+
310
jobs:
11+
test:
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
go: [
16+
'1.12',
17+
'1.13',
18+
'1.14',
19+
'1.15'
20+
]
421

5-
build:
6-
name: Build
22+
name: unit
723
runs-on: ubuntu-latest
8-
steps:
924

10-
- name: Set up Go 1.13
11-
uses: actions/setup-go@v1
25+
steps:
26+
- uses: actions/checkout@v2
27+
- uses: actions/setup-go@v2
1228
with:
13-
go-version: 1.13
14-
id: go
15-
16-
- name: Check out code into the Go module directory
17-
uses: actions/checkout@v1
18-
29+
go-version: ${{ matrix.go }}
1930
- name: Build
2031
run: go build -v .
21-
2232
- name: Test
23-
run: go test ./...
33+
run: go test -cover ./...
34+
- name: Race
35+
run: go test -race ./...

example/main.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,22 @@ func main() {
3939
}
4040
defer client.Close()
4141

42-
projectID, err := metadata.ProjectID()
43-
if err != nil {
44-
log.Fatal(err)
45-
}
42+
projectID := os.Getenv("GOOGLE_CLOUD_PROJECT")
4643

47-
if os.Getenv("GOOGLE_CLOUD_PROJECT") != "" {
48-
projectID = os.Getenv("GOOGLE_CLOUD_PROJECT")
44+
if projectID == "" {
45+
if projectID, err = metadata.ProjectID(); err != nil {
46+
log.Fatal(err)
47+
}
4948
}
5049

5150
log.Printf("initializing sink, project_id: %q", projectID)
5251

5352
// create sink
5453
ss := stackdriver.NewSink(client, &stackdriver.Config{
55-
ProjectID: projectID,
56-
Location: "us-east1-c",
54+
ProjectID: projectID,
55+
Location: "us-east1-c",
56+
DebugLogs: true,
57+
ReportingInterval: 35 * time.Second,
5758
})
5859
cfg := metrics.DefaultConfig("go-metrics-stackdriver")
5960
cfg.EnableHostname = false
@@ -107,6 +108,7 @@ func exercise(ctx context.Context, m metrics.MetricSink) {
107108
m.AddSample([]string{"method", "dist"}, 50)
108109
m.AddSample([]string{"method", "dist"}, 100)
109110
m.AddSample([]string{"method", "dist"}, 150)
111+
m.AddSample([]string{"foo"}, 100)
110112
case <-ctx.Done():
111113
log.Printf("terminating")
112114
return

stackdriver.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
15+
// Package stackdriver provides a cloud monitoring sink for applications
16+
// instrumented with the go-metrics library.
1417
package stackdriver
1518

1619
import (
@@ -30,7 +33,6 @@ import (
3033
metrics "github.com/armon/go-metrics"
3134
googlepb "github.com/golang/protobuf/ptypes/timestamp"
3235
distributionpb "google.golang.org/genproto/googleapis/api/distribution"
33-
"google.golang.org/genproto/googleapis/api/metric"
3436
metricpb "google.golang.org/genproto/googleapis/api/metric"
3537
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
3638
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
@@ -80,8 +82,8 @@ type Config struct {
8082
// variable parameters within a metric name.
8183
// Optional. Defaults to DefaultLabelExtractor.
8284
LabelExtractor ExtractLabelsFn
83-
// Prefix of the metrics recorded. Defaults to "go-metrics/" so your metric "foo" will be recorded as
84-
// "custom.googleapis.com/go-metrics/foo".
85+
// Prefix of the metrics recorded. Defaults to "go-metrics/" so a metric
86+
// "foo" will be recorded as "custom.googleapis.com/go-metrics/foo".
8587
Prefix *string
8688
// The bucketer is used to determine histogram bucket boundaries
8789
// for the sampled metrics. This will execute before the LabelExtractor.
@@ -110,19 +112,21 @@ type Config struct {
110112
// Optional. Defaults to a combination of hostname+pid.
111113
TaskID string
112114

113-
// Debug logging. Errors will be logged to stderr, but setting this to true
114-
// will log additional information that is helpful when debugging errors.
115+
// Debug logging. Errors are always logged to stderr, but setting this to
116+
// true will log additional information that is helpful when debugging
117+
// errors.
115118
// Optional. Defaults to false.
116119
DebugLogs bool
117120

118-
// MonitoredResource identifies the machine/service/resource that is monitored.
119-
// Different possible settings are defined here:
121+
// MonitoredResource identifies the machine/service/resource that is
122+
// monitored. Different possible settings are defined here:
120123
// https://cloud.google.com/monitoring/api/resources
121124
//
122-
// Setting a nil MonitoredResource will run a defaultMonitoredResource function.
125+
// Setting a nil MonitoredResource will run a defaultMonitoredResource
126+
// function.
123127
MonitoredResource *monitoredrespb.MonitoredResource
124128

125-
// Logger implements our Logger interface, providing Printf and Println functions
129+
// Logger that can be injected for custom log formatting.
126130
Logger Logger
127131
}
128132

@@ -176,7 +180,7 @@ func DefaultLabelExtractor(key []string, kind string) ([]string, []metrics.Label
176180
case "histogram":
177181
return key, nil, nil
178182
}
179-
return nil, nil, fmt.Errorf("Unknown metric kind: %s", kind)
183+
return nil, nil, fmt.Errorf("unknown metric kind: %s", kind)
180184
}
181185

182186
// NewSink creates a Sink to flush metrics to stackdriver every interval. The
@@ -354,10 +358,10 @@ func (s *Sink) report(ctx context.Context) {
354358
Type: fmt.Sprintf("custom.googleapis.com/%s%s", s.prefix, name),
355359
Labels: labels,
356360
},
357-
MetricKind: metric.MetricDescriptor_GAUGE,
361+
MetricKind: metricpb.MetricDescriptor_GAUGE,
358362
Resource: resource,
359363
Points: []*monitoringpb.Point{
360-
&monitoringpb.Point{
364+
{
361365
Interval: &monitoringpb.TimeInterval{
362366
EndTime: &googlepb.Timestamp{
363367
Seconds: end.Unix(),
@@ -387,10 +391,10 @@ func (s *Sink) report(ctx context.Context) {
387391
Type: fmt.Sprintf("custom.googleapis.com/%s%s", s.prefix, name),
388392
Labels: labels,
389393
},
390-
MetricKind: metric.MetricDescriptor_GAUGE,
394+
MetricKind: metricpb.MetricDescriptor_GAUGE,
391395
Resource: resource,
392396
Points: []*monitoringpb.Point{
393-
&monitoringpb.Point{
397+
{
394398
Interval: &monitoringpb.TimeInterval{
395399
EndTime: &googlepb.Timestamp{
396400
Seconds: end.Unix(),
@@ -427,10 +431,10 @@ func (s *Sink) report(ctx context.Context) {
427431
Type: fmt.Sprintf("custom.googleapis.com/%s%s", s.prefix, name),
428432
Labels: labels,
429433
},
430-
MetricKind: metric.MetricDescriptor_CUMULATIVE,
434+
MetricKind: metricpb.MetricDescriptor_CUMULATIVE,
431435
Resource: resource,
432436
Points: []*monitoringpb.Point{
433-
&monitoringpb.Point{
437+
{
434438
Interval: &monitoringpb.TimeInterval{
435439
StartTime: &googlepb.Timestamp{
436440
Seconds: s.firstTime.Unix(),
@@ -487,12 +491,12 @@ func (s *Sink) report(ctx context.Context) {
487491
}
488492
}
489493

490-
// A Gauge should retain the last value it is set to.
494+
// SetGauge retains the last value it is set to.
491495
func (s *Sink) SetGauge(key []string, val float32) {
492496
s.SetGaugeWithLabels(key, val, nil)
493497
}
494498

495-
// A Gauge should retain the last value it is set to.
499+
// SetGaugeWithLabels retains the last value it is set to.
496500
func (s *Sink) SetGaugeWithLabels(key []string, val float32, labels []metrics.Label) {
497501
n := newSeries(key, labels)
498502

@@ -507,17 +511,17 @@ func (s *Sink) SetGaugeWithLabels(key []string, val float32, labels []metrics.La
507511
s.gauges[n.hash] = g
508512
}
509513

510-
// Should emit a Key/Value pair for each call.
514+
// EmitKey is not implemented.
511515
func (s *Sink) EmitKey(key []string, val float32) {
512516
// EmitKey is not implemented for stackdriver
513517
}
514518

515-
// Counters should accumulate values.
519+
// IncrCounter increments a counter by a value.
516520
func (s *Sink) IncrCounter(key []string, val float32) {
517521
s.IncrCounterWithLabels(key, val, nil)
518522
}
519523

520-
// Counters should accumulate values.
524+
// IncrCounterWithLabels increments a counter by a value.
521525
func (s *Sink) IncrCounterWithLabels(key []string, val float32, labels []metrics.Label) {
522526
n := newSeries(key, labels)
523527

@@ -535,12 +539,12 @@ func (s *Sink) IncrCounterWithLabels(key []string, val float32, labels []metrics
535539
}
536540
}
537541

538-
// Samples are for timing information, where quantiles are used.
542+
// AddSample adds a sample to a histogram metric.
539543
func (s *Sink) AddSample(key []string, val float32) {
540544
s.AddSampleWithLabels(key, val, nil)
541545
}
542546

543-
// Samples are for timing information, where quantiles are used.
547+
// AddSampleWithLabels adds a sample to a histogram metric.
544548
func (s *Sink) AddSampleWithLabels(key []string, val float32, labels []metrics.Label) {
545549
n := newSeries(key, labels)
546550

@@ -566,7 +570,7 @@ type series struct {
566570
hash string
567571
}
568572

569-
var forbiddenChars = regexp.MustCompile("[ .=\\-/]")
573+
var forbiddenChars = regexp.MustCompile(`[ .=\-/]`)
570574

571575
func newSeries(key []string, labels []metrics.Label) *series {
572576
hash := strings.Join(key, "_")

0 commit comments

Comments
 (0)