generated from ydataai/opensource-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prometheus): prometheus client (#20)
- Loading branch information
1 parent
0303718
commit 86dea40
Showing
4 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |