This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add prometheus client factory and sli retrieval interface
Signed-off-by: Xabier Larrakoetxea <slok69@gmail.com>
- Loading branch information
Showing
6 changed files
with
122 additions
and
4 deletions.
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
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,43 @@ | ||
package prometheus | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/prometheus/client_golang/api" | ||
promv1 "github.com/prometheus/client_golang/api/prometheus/v1" | ||
) | ||
|
||
// ClientFactory knows how to get prometheus API clients. | ||
type ClientFactory interface { | ||
// GetV1APIClient returns a new prometheus v1 API client. | ||
// address is the address of the prometheus. | ||
GetV1APIClient(address string) (promv1.API, error) | ||
} | ||
|
||
type factory struct { | ||
clis map[string]api.Client | ||
climu sync.Mutex | ||
} | ||
|
||
// NewFactory returns a new client factory. | ||
func NewFactory() ClientFactory { | ||
return &factory{ | ||
clis: map[string]api.Client{}, | ||
} | ||
} | ||
|
||
// GetV1APIClient satisfies ClientFactory interface. | ||
func (f *factory) GetV1APIClient(address string) (promv1.API, error) { | ||
f.climu.Lock() | ||
f.climu.Unlock() | ||
|
||
cli, ok := f.clis[address] | ||
if !ok { | ||
cli, err := api.NewClient(api.Config{Address: address}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
f.clis[address] = cli | ||
} | ||
return promv1.NewAPI(cli), 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,34 @@ | ||
package prometheus | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/url" | ||
|
||
promv1 "github.com/prometheus/client_golang/api/prometheus/v1" | ||
) | ||
|
||
type fakeFactory struct { | ||
} | ||
|
||
// NewFakeFactory returns a new fake factory. | ||
func NewFakeFactory() ClientFactory { | ||
return &fakeFactory{} | ||
} | ||
|
||
// GetV1APIClient satisfies ClientFactory interface. | ||
func (f *fakeFactory) GetV1APIClient(_ string) (promv1.API, error) { | ||
cli := &fakeClient{} | ||
return promv1.NewAPI(cli), nil | ||
} | ||
|
||
// fakeClient is a faked http client. | ||
// TODO | ||
type fakeClient struct{} | ||
|
||
func (c *fakeClient) URL(ep string, args map[string]string) *url.URL { return nil } | ||
func (c *fakeClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) { | ||
b := []byte{} | ||
resp := &http.Response{} | ||
return resp, b, 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,41 @@ | ||
package sli | ||
|
||
import ( | ||
"errors" | ||
|
||
measurev1alpha1 "github.com/slok/service-level-operator/pkg/apis/measure/v1alpha1" | ||
"github.com/slok/service-level-operator/pkg/log" | ||
promcli "github.com/slok/service-level-operator/pkg/service/client/prometheus" | ||
) | ||
|
||
// Result is the result of getting a SLI from a backend. | ||
type Result struct { | ||
TotalQ float64 | ||
ErrorQ float64 | ||
} | ||
|
||
// Service knows how to get . | ||
type Service interface { | ||
// GetSLI returns the result of a ServiceLevel SLI | ||
GetSLI(*measurev1alpha1.ServiceLevel) (Result, error) | ||
} | ||
|
||
// Prometheus knows how to get SLIs from a prometheus backend. | ||
type prometheus struct { | ||
cliFactory promcli.ClientFactory | ||
logger log.Logger | ||
} | ||
|
||
// NewPrometheus returns a new prometheus SLI service. | ||
func NewPrometheus(promCliFactory promcli.ClientFactory, logger log.Logger) Service { | ||
return &prometheus{ | ||
cliFactory: promCliFactory, | ||
logger: logger, | ||
} | ||
} | ||
|
||
// GetSLI satisfies Service interface.. | ||
func (p *prometheus) GetSLI(sl *measurev1alpha1.ServiceLevel) (Result, error) { | ||
// TODO | ||
return Result{}, errors.New("not implemented") | ||
} |