Skip to content

Commit

Permalink
Merge pull request #12 from TencentBlueKing/prometheus
Browse files Browse the repository at this point in the history
增加 Prometheus 指标支持
  • Loading branch information
alex-smile authored Oct 28, 2022
2 parents 7f82689 + 09117a0 commit 634b6c5
Show file tree
Hide file tree
Showing 28 changed files with 1,053 additions and 330 deletions.
38 changes: 20 additions & 18 deletions core/bkapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,42 @@ import (
"gopkg.in/h2non/gentleman.v2"
)

func newGentlemanClient(config define.ClientConfig) *gentleman.Client {
client := gentleman.New().
URL(config.GetUrl())
// the common options for all bkapi clients
var globalBkapiClientOptions []define.BkApiClientOption

headers := config.GetAuthorizationHeaders()
if len(headers) > 0 {
client.SetHeaders(headers)
}

return client
// RegisterGlobalBkapiClientOption use to register a global bkapi client option.
// Warning: this function is not safe for concurrent access.
func RegisterGlobalBkapiClientOption(opt define.BkApiClientOption) {
globalBkapiClientOptions = append(globalBkapiClientOptions, opt)
}

// NewBkApiClient creates a new BkApiClient.
func NewBkApiClient(
apiName string,
configProvider define.ClientConfigProvider,
opts ...define.BkApiClientOption,
options ...define.BkApiClientOption,
) (define.BkApiClient, error) {
client := internal.NewBkApiClient(
apiName,
gentleman.New(),
func(name string, request *gentleman.Request) define.Operation {
return internal.NewOperation(name, request)
func(name string, client define.BkApiClient, request *gentleman.Request) define.Operation {
return internal.NewOperation(name, client, request)
},
configProvider.ProvideConfig(apiName),
)

if len(opts) == 0 {
return client, nil
}
for phase, opts := range map[string][]define.BkApiClientOption{
"global": globalBkapiClientOptions,
"client": options,
} {
if len(opts) == 0 {
continue
}

err := client.Apply(opts...)
if err != nil {
return nil, define.ErrorWrapf(err, "failed to apply options to client %s", apiName)
err := client.Apply(opts...)
if err != nil {
return nil, define.ErrorWrapf(err, "failed to apply options to client %s, phase %s", apiName, phase)
}
}

return client, nil
Expand Down
5 changes: 4 additions & 1 deletion core/bkapi/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var _ = Describe("Option", func() {
ctrl *gomock.Controller
roundTripper *mock.MockRoundTripper
operation *internal.Operation
bkapiClient *mock.MockBkApiClient
requestError error
)

Expand All @@ -48,7 +49,9 @@ var _ = Describe("Option", func() {
request := gentleman.NewRequest()
request.Use(transport.Set(roundTripper))

operation = internal.NewOperation("testing", request)
bkapiClient = mock.NewMockBkApiClient(ctrl)

operation = internal.NewOperation("testing", bkapiClient, request)
})

AfterEach(func() {
Expand Down
9 changes: 7 additions & 2 deletions core/define/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ var (
ErrBkApiRequest = errors.New("bkapi request error")
)

// ErrorWrapf annotates err with the format specifier and arguments.
var ErrorWrapf = pkgErrors.WithMessagef
//
var (
// ErrorWrapf annotates err with the format specifier and arguments.
ErrorWrapf = pkgErrors.WithMessagef
// ErrorCause returns the underlying cause of the error, if possible.
ErrorCause = pkgErrors.Cause
)

// EnableStackTraceErrorWrapf enables stack trace for ErrorWrapf.
func EnableStackTraceErrorWrapf() {
Expand Down
6 changes: 6 additions & 0 deletions core/define/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ import (

// Operation defines the operation of the API.
type Operation interface {
// ClientName method returns the client's name.
ClientName() string

// Name method returns the operation's name.
Name() string

// FullName method returns the operation's name.
FullName() string

// Apply method applies the given options to the operation.
Apply(opts ...OperationOption) Operation

Expand Down
12 changes: 7 additions & 5 deletions core/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"gopkg.in/h2non/gentleman.v2/plugins/headers"
)

//go:generate mockgen -destination=../internal/mock/logging.go -package=mock github.com/TencentBlueKing/gopkg/logging Logger

// DefaultUserAgent :
var DefaultUserAgent string

Expand All @@ -33,7 +35,7 @@ type BkApiClient struct {
logger logging.Logger
client *gentleman.Client
operationOptions []define.OperationOption
operationFactory func(name string, request *gentleman.Request) define.Operation
operationFactory func(name string, client define.BkApiClient, request *gentleman.Request) define.Operation
}

// Name returns the client name.
Expand Down Expand Up @@ -99,10 +101,10 @@ func (cli *BkApiClient) newGentlemanRequest(config define.OperationConfig) *gent
func (cli *BkApiClient) newOperationName(config define.OperationConfig) string {
name := config.GetName()
if name != "" {
return fmt.Sprintf("%s.%s", cli.Name(), name)
return name
}

return fmt.Sprintf("%s(%s %s)", cli.Name(), config.GetMethod(), config.GetPath())
return fmt.Sprintf("(%s %s)", config.GetMethod(), config.GetPath())
}

func (cli *BkApiClient) applyOperationOptions(op define.Operation, opts ...define.OperationOption) {
Expand All @@ -123,7 +125,7 @@ func (cli *BkApiClient) NewOperation(
config := provider.ProvideConfig()
request := cli.newGentlemanRequest(config)
name := cli.newOperationName(config)
operation := cli.operationFactory(name, request)
operation := cli.operationFactory(name, cli, request)

request.Use(plugin.NewResponsePlugin(func(c *context.Context, h context.Handler) {
cli.logResponse(operation, c.Response)
Expand All @@ -139,7 +141,7 @@ func (cli *BkApiClient) NewOperation(
func NewBkApiClient(
name string,
client *gentleman.Client,
factory func(name string, request *gentleman.Request) define.Operation,
factory func(name string, client define.BkApiClient, request *gentleman.Request) define.Operation,
config define.ClientConfig,
) *BkApiClient {

Expand Down
32 changes: 2 additions & 30 deletions core/internal/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var _ = Describe("Client", func() {

client = internal.NewBkApiClient(
"testing", gentlemanClient,
func(name string, req *gentleman.Request) define.Operation {
func(name string, client define.BkApiClient, req *gentleman.Request) define.Operation {
operation.EXPECT().Name().Return(name).AnyTimes()

request = req
Expand Down Expand Up @@ -150,34 +150,6 @@ var _ = Describe("Client", func() {
Expect(op).NotTo(BeNil())
})

It("should generate operation name by config.Name", func() {
operationConfig := mock.NewMockOperationConfig(ctrl)
operationConfigProvider := mock.NewMockOperationConfigProvider(ctrl)
operationConfigProvider.EXPECT().ProvideConfig().Return(operationConfig).AnyTimes()

operationConfig.EXPECT().GetName().Return("operation").AnyTimes()
operationConfig.EXPECT().GetMethod().Return("GET").AnyTimes()
operationConfig.EXPECT().GetPath().Return("/test").AnyTimes()

operation := client.NewOperation(operationConfigProvider)

Expect(operation.Name()).To(Equal("testing.operation"))
})

It("should generate anonymous operation name", func() {
operationConfig := mock.NewMockOperationConfig(ctrl)
operationConfigProvider := mock.NewMockOperationConfigProvider(ctrl)
operationConfigProvider.EXPECT().ProvideConfig().Return(operationConfig).AnyTimes()

operationConfig.EXPECT().GetName().Return("").AnyTimes()
operationConfig.EXPECT().GetMethod().Return("GET").AnyTimes()
operationConfig.EXPECT().GetPath().Return("/test").AnyTimes()

operation := client.NewOperation(operationConfigProvider)

Expect(operation.Name()).To(Equal("testing(GET /test)"))
})

It("should set the user agent", func() {
op := client.NewOperation(operationConfigProvider)
Expect(op).To(Equal(operation))
Expand Down Expand Up @@ -206,7 +178,7 @@ var _ = Describe("Client", func() {

client = internal.NewBkApiClient(
"testing", gentlemanClient,
func(name string, req *gentleman.Request) define.Operation {
func(name string, client define.BkApiClient, req *gentleman.Request) define.Operation {
operation.EXPECT().Name().Return(name).AnyTimes()

request = req
Expand Down
41 changes: 5 additions & 36 deletions core/internal/mock/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 7 additions & 47 deletions core/internal/mock/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 634b6c5

Please sign in to comment.