Skip to content

Commit

Permalink
chore: clean up protobuf loose ends (influxdata#22823)
Browse files Browse the repository at this point in the history
- Remove `gogo/protobuf` and `golang/protobuf` deps
- Fix mistake in `CONTRIBUTING.md`
- Use Prometheus `MetricType` type over our own copy-paste version
  • Loading branch information
serenibyss authored Nov 5, 2021
1 parent 2136291 commit a7f3b67
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 75 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ $ go generate ./...

If generating the protobuf code is failing for you, check each of the following:
* Ensure the protobuf library can be found. Make sure that `LD_LIBRARY_PATH` includes the directory in which the library `libprotoc.so` has been installed.
* Ensure the command `protoc-gen-gogo`, found in `GOPATH/bin`, is on your path. This can be done by adding `GOPATH/bin` to `PATH`.
* Ensure the command `protoc-gen-go`, found in `GOPATH/bin`, is on your path. This can be done by adding `GOPATH/bin` to `PATH`.


## Generated Go Templates
Expand Down
51 changes: 2 additions & 49 deletions gather/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"io"
"time"

gogoproto "github.com/gogo/protobuf/proto" // Used for Prometheus
"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/models"
dto "github.com/prometheus/client_model/go"
)

// MetricsCollection is the struct including metrics and other requirements.
Expand All @@ -23,7 +23,7 @@ type Metrics struct {
Tags map[string]string `json:"tags"`
Fields map[string]interface{} `json:"fields"`
Timestamp time.Time `json:"timestamp"`
Type MetricType `json:"type"`
Type dto.MetricType `json:"type"`
}

// MetricsSlice is a slice of Metrics
Expand Down Expand Up @@ -67,50 +67,3 @@ func (ms MetricsSlice) Reader() (io.Reader, error) {
}
return buf, nil
}

// MetricType is prometheus metrics type.
type MetricType int

// the set of metric types
const (
MetricTypeCounter MetricType = iota
MetricTypeGauge
MetricTypeSummary
MetricTypeUntyped
MetricTypeHistogrm
)

var metricTypeName = []string{
"COUNTER",
"GAUGE",
"SUMMARY",
"UNTYPED",
"HISTOGRAM",
}
var metricTypeValue = map[string]int32{
"COUNTER": 0,
"GAUGE": 1,
"SUMMARY": 2,
"UNTYPED": 3,
"HISTOGRAM": 4,
}

// Valid returns whether the metrics type is valid.
func (x MetricType) Valid() bool {
return x >= MetricTypeCounter && x <= MetricTypeHistogrm
}

// String returns the string value of MetricType.
func (x MetricType) String() string {
return metricTypeName[x]
}

// UnmarshalJSON implements the unmarshaler interface.
func (x *MetricType) UnmarshalJSON(data []byte) error {
value, err := gogoproto.UnmarshalJSONEnum(metricTypeValue, data, "MetricType")
if err != nil {
return err
}
*x = MetricType(value)
return nil
}
11 changes: 6 additions & 5 deletions gather/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/google/go-cmp/cmp"
dto "github.com/prometheus/client_model/go"
)

func TestMetricsReader(t *testing.T) {
Expand Down Expand Up @@ -41,7 +42,7 @@ func TestMetricsReader(t *testing.T) {
Fields: map[string]interface{}{
"value": "yes",
},
Type: MetricTypeGauge,
Type: dto.MetricType_GAUGE,
Timestamp: time.Unix(0, 1422568543702900257),
},
},
Expand All @@ -59,7 +60,7 @@ func TestMetricsReader(t *testing.T) {
Fields: map[string]interface{}{
"value": 0.64,
},
Type: MetricTypeGauge,
Type: dto.MetricType_GAUGE,
Timestamp: time.Unix(0, 1422568543702900257),
},
},
Expand Down Expand Up @@ -148,7 +149,7 @@ func TestMetricsMarshal(t *testing.T) {
"x": 12.3,
"y": "a long string",
},
Type: MetricTypeSummary,
Type: dto.MetricType_SUMMARY,
},
},
},
Expand All @@ -166,7 +167,7 @@ func TestMetricsMarshal(t *testing.T) {
"x": 12.3,
"y": "a long string",
},
Type: MetricTypeSummary,
Type: dto.MetricType_SUMMARY,
},

{
Expand All @@ -180,7 +181,7 @@ func TestMetricsMarshal(t *testing.T) {
"x": 12.5,
"y": "a long string2",
},
Type: MetricTypeGauge,
Type: dto.MetricType_GAUGE,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion gather/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (p *prometheusScraper) parse(r io.Reader, header http.Header, target influx
Tags: tags,
Fields: fields,
Name: name,
Type: MetricType(family.GetType()),
Type: family.GetType(),
}
ms = append(ms, me)
}
Expand Down
3 changes: 2 additions & 1 deletion gather/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/mock"
influxdbtesting "github.com/influxdata/influxdb/v2/testing"
dto "github.com/prometheus/client_model/go"
"go.uber.org/zap/zaptest"
)

Expand Down Expand Up @@ -71,7 +72,7 @@ func TestScheduler(t *testing.T) {

want := Metrics{
Name: "go_goroutines",
Type: MetricTypeGauge,
Type: dto.MetricType_GAUGE,
Tags: map[string]string{},
Fields: map[string]interface{}{
"gauge": float64(36),
Expand Down
17 changes: 9 additions & 8 deletions gather/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/platform"
dto "github.com/prometheus/client_model/go"
)

var (
Expand Down Expand Up @@ -49,7 +50,7 @@ func TestPrometheusScraper(t *testing.T) {
ms: []Metrics{
{
Name: "go_gc_duration_seconds",
Type: MetricTypeSummary,
Type: dto.MetricType_SUMMARY,
Fields: map[string]interface{}{
"count": float64(326),
"sum": 0.07497837,
Expand All @@ -63,15 +64,15 @@ func TestPrometheusScraper(t *testing.T) {
},
{
Name: "go_goroutines",
Type: MetricTypeGauge,
Type: dto.MetricType_GAUGE,
Tags: map[string]string{},
Fields: map[string]interface{}{
"gauge": float64(36),
},
},
{
Name: "go_info",
Type: MetricTypeGauge,
Type: dto.MetricType_GAUGE,
Tags: map[string]string{
"version": "go1.10.3",
},
Expand All @@ -81,39 +82,39 @@ func TestPrometheusScraper(t *testing.T) {
},
{
Name: "go_memstats_alloc_bytes",
Type: MetricTypeGauge,
Type: dto.MetricType_GAUGE,
Tags: map[string]string{},
Fields: map[string]interface{}{
"gauge": 2.0091312e+07,
},
},
{
Name: "go_memstats_alloc_bytes_total",
Type: MetricTypeCounter,
Type: dto.MetricType_COUNTER,
Fields: map[string]interface{}{
"counter": 4.183173328e+09,
},
Tags: map[string]string{},
},
{
Name: "go_memstats_buck_hash_sys_bytes",
Type: MetricTypeGauge,
Type: dto.MetricType_GAUGE,
Tags: map[string]string{},
Fields: map[string]interface{}{
"gauge": 1.533852e+06,
},
},
{
Name: "go_memstats_frees_total",
Type: MetricTypeCounter,
Type: dto.MetricType_COUNTER,
Tags: map[string]string{},
Fields: map[string]interface{}{
"counter": 1.8944339e+07,
},
},
{
Name: "go_memstats_gc_cpu_fraction",
Type: MetricTypeGauge,
Type: dto.MetricType_GAUGE,
Tags: map[string]string{},
Fields: map[string]interface{}{
"gauge": 1.972734963012756e-05,
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ require (
github.com/glycerine/goconvey v0.0.0-20180728074245-46e3a41ad493 // indirect
github.com/go-chi/chi v4.1.0+incompatible
github.com/go-stack/stack v1.8.0
github.com/gogo/protobuf v1.3.2
github.com/golang-jwt/jwt v3.2.1+incompatible
github.com/golang/gddo v0.0.0-20181116215533-9bd4a3295021
github.com/golang/mock v1.5.0
github.com/golang/protobuf v1.5.2
github.com/golang/snappy v0.0.3
github.com/google/btree v1.0.1
github.com/google/go-cmp v0.5.6
Expand Down Expand Up @@ -151,9 +149,11 @@ require (
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/gofrs/uuid v3.3.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
github.com/golang/geo v0.0.0-20190916061304-5b978397cfec // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/flatbuffers v2.0.0+incompatible // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
Expand Down
18 changes: 14 additions & 4 deletions prometheus/filter.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package prometheus

import (
"fmt"
"sort"
"strings"

"github.com/golang/protobuf/proto" //lint:ignore SA1019 this deprecated package will be removed by https://github.com/influxdata/influxdb/pull/22571
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/proto"
)

var _ prometheus.Gatherer = (*Filter)(nil)
Expand Down Expand Up @@ -113,6 +115,14 @@ type labelPairs struct {
Label []*dto.LabelPair `protobuf:"bytes,1,rep,name=label" json:"label,omitempty"`
}

func (l *labelPairs) Reset() {}
func (l *labelPairs) String() string { return proto.CompactTextString(l) }
func (*labelPairs) ProtoMessage() {}
func (l *labelPairs) Reset() {}

func (l *labelPairs) String() string {
var a []string
for _, lbl := range l.Label {
a = append(a, fmt.Sprintf("label:<%s> ", lbl.String()))
}
return strings.Join(a, "")
}

func (*labelPairs) ProtoMessage() {}
2 changes: 1 addition & 1 deletion prometheus/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"reflect"
"testing"

"github.com/golang/protobuf/proto" //lint:ignore SA1019 this deprecated package will be removed by https://github.com/influxdata/influxdb/pull/22571
pr "github.com/influxdata/influxdb/v2/prometheus"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/proto"
)

func TestFilter_Gather(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package prometheus_test

import (
"github.com/golang/protobuf/proto" //lint:ignore SA1019 this deprecated package will be removed by https://github.com/influxdata/influxdb/pull/22571
dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/proto"
)

func NewCounter(name string, v float64, ls ...*dto.LabelPair) *dto.MetricFamily {
Expand Down
2 changes: 1 addition & 1 deletion telemetry/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"testing"
"time"

"github.com/golang/protobuf/proto" //lint:ignore SA1019 this deprecated package will be removed by https://github.com/influxdata/influxdb/pull/22571
"github.com/google/go-cmp/cmp"
"github.com/matttproud/golang_protobuf_extensions/pbutil"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"google.golang.org/protobuf/proto"
)

func TestPusher_Push(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package telemetry

import (
"github.com/golang/protobuf/proto" //lint:ignore SA1019 this deprecated package will be removed by https://github.com/influxdata/influxdb/pull/22571
dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/proto"
)

func NewCounter(name string, v float64, ls ...*dto.LabelPair) *dto.MetricFamily {
Expand Down

0 comments on commit a7f3b67

Please sign in to comment.