Skip to content

Commit

Permalink
feat(prometheus): prometheus client (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoaguimaraes authored Nov 12, 2021
1 parent 0303718 commit 86dea40
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/google/uuid v1.1.2
github.com/kelseyhightower/envconfig v1.4.0
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/common v0.26.0
github.com/sirupsen/logrus v1.8.1
google.golang.org/grpc v1.38.0
google.golang.org/protobuf v1.27.1
Expand Down Expand Up @@ -42,7 +43,6 @@ require (
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJS
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down Expand Up @@ -307,6 +308,7 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
Expand Down
62 changes: 62 additions & 0 deletions pkg/common/clients/prometheus_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package clients

import (
"context"
"time"

"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/sirupsen/logrus"
)

// PrometheusClient represents the Prometheus client.
type PrometheusClient struct {
logger *logrus.Logger
config PrometheusConfiguration
api v1.API
}

// NewPrometheusClient creates a PrometheusClient instance.
func NewPrometheusClient(
config PrometheusConfiguration,
logger *logrus.Logger) PrometheusClient {
api := createClientAPI(config, logger)
return PrometheusClient{
config: config,
logger: logger,
api: api,
}
}

func createClientAPI(config PrometheusConfiguration, logger *logrus.Logger) v1.API {
client, err := api.NewClient(api.Config{
Address: config.Address,
})
if err != nil {
logger.Errorf("Error creating client: %v", err)
}
return v1.NewAPI(client)
}

// QueryRange returns range vectors as result type matrix, given the following parameters:
// - query: Prometheus query
// - startAt: start time
// - endAt: end time
// - step: interval duration
// It returns the result matrix and the execution error encountered.
func (c PrometheusClient) QueryRange(ctx context.Context, query string, startAt, endAt time.Time, step time.Duration) (model.Value, error) {
result, warnings, err := c.api.QueryRange(ctx, query, v1.Range{
Start: startAt,
End: endAt,
Step: step,
})
if err != nil {
c.logger.Errorf("Error querying Prometheus: %v", err)
return nil, err
}
if len(warnings) > 0 {
c.logger.Warnf("Prometheus query warnings result: %v", warnings)
}
return result, nil
}
13 changes: 13 additions & 0 deletions pkg/common/clients/prometheus_client_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package clients

import "github.com/kelseyhightower/envconfig"

// PrometheusConfiguration represents the client configuration to connect to Prometheus.
type PrometheusConfiguration struct {
Address string `envconfig:"PROMETHEUS_ADDRESS" required:"true"`
}

// LoadFromEnvVars for PrometheusConfiguration.
func (c *PrometheusConfiguration) LoadFromEnvVars() error {
return envconfig.Process("", c)
}

0 comments on commit 86dea40

Please sign in to comment.