mirror of https://github.com/docker/compose.git
API Metrics : send context type, not context name
Signed-off-by: Guillaume Tardif <guillaume.tardif@docker.com>
This commit is contained in:
parent
fbe505f27f
commit
7807fb33cc
|
@ -44,10 +44,16 @@ func New(ctx context.Context) (*Client, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Client{
|
client := NewClient(cc.Type(), service)
|
||||||
backendType: cc.Type(),
|
return &client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClient returns new client
|
||||||
|
func NewClient(backendType string, service backend.Service) Client {
|
||||||
|
return Client{
|
||||||
|
backendType: backendType,
|
||||||
bs: service,
|
bs: service,
|
||||||
}, nil
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCloudService returns a backend CloudService (typically login, create context)
|
// GetCloudService returns a backend CloudService (typically login, create context)
|
||||||
|
@ -61,6 +67,11 @@ type Client struct {
|
||||||
bs backend.Service
|
bs backend.Service
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ContextType the context type associated with backend
|
||||||
|
func (c *Client) ContextType() string {
|
||||||
|
return c.backendType
|
||||||
|
}
|
||||||
|
|
||||||
// ContainerService returns the backend service for the current context
|
// ContainerService returns the backend service for the current context
|
||||||
func (c *Client) ContainerService() containers.Service {
|
func (c *Client) ContainerService() containers.Service {
|
||||||
if cs := c.bs.ContainerService(); cs != nil {
|
if cs := c.bs.ContainerService(); cs != nil {
|
||||||
|
|
|
@ -111,10 +111,7 @@ func configureContext(ctx context.Context, currentContext string, method string)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, err = proxy.WithClient(ctx, c)
|
ctx = proxy.WithClient(ctx, c)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s, err := store.New(configDir)
|
s, err := store.New(configDir)
|
||||||
|
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
|
||||||
"github.com/docker/compose-cli/metrics"
|
"github.com/docker/compose-cli/metrics"
|
||||||
|
"github.com/docker/compose-cli/server/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -41,14 +42,12 @@ var (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func metricsServerInterceptor(clictx context.Context, client metrics.Client) grpc.UnaryServerInterceptor {
|
func metricsServerInterceptor(client metrics.Client) grpc.UnaryServerInterceptor {
|
||||||
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
||||||
currentContext, err := getIncomingContext(ctx)
|
backendClient := proxy.Client(ctx)
|
||||||
if err != nil {
|
contextType := ""
|
||||||
currentContext, err = getConfigContext(clictx)
|
if backendClient != nil {
|
||||||
if err != nil {
|
contextType = backendClient.ContextType()
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := handler(ctx, req)
|
data, err := handler(ctx, req)
|
||||||
|
@ -61,7 +60,7 @@ func metricsServerInterceptor(clictx context.Context, client metrics.Client) grp
|
||||||
if command != "" {
|
if command != "" {
|
||||||
client.Send(metrics.Command{
|
client.Send(metrics.Command{
|
||||||
Command: command,
|
Command: command,
|
||||||
Context: currentContext,
|
Context: contextType,
|
||||||
Source: metrics.APISource,
|
Source: metrics.APISource,
|
||||||
Status: status,
|
Status: status,
|
||||||
})
|
})
|
||||||
|
|
|
@ -26,6 +26,11 @@ import (
|
||||||
"google.golang.org/grpc/metadata"
|
"google.golang.org/grpc/metadata"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
|
|
||||||
|
"github.com/docker/compose-cli/api/client"
|
||||||
|
"github.com/docker/compose-cli/api/compose"
|
||||||
|
"github.com/docker/compose-cli/api/containers"
|
||||||
|
"github.com/docker/compose-cli/api/secrets"
|
||||||
|
"github.com/docker/compose-cli/api/volumes"
|
||||||
"github.com/docker/compose-cli/errdefs"
|
"github.com/docker/compose-cli/errdefs"
|
||||||
"github.com/docker/compose-cli/metrics"
|
"github.com/docker/compose-cli/metrics"
|
||||||
containersv1 "github.com/docker/compose-cli/protos/containers/v1"
|
containersv1 "github.com/docker/compose-cli/protos/containers/v1"
|
||||||
|
@ -55,18 +60,21 @@ func TestAllMethodsHaveCorrespondingCliCommand(t *testing.T) {
|
||||||
func TestTrackSuccess(t *testing.T) {
|
func TestTrackSuccess(t *testing.T) {
|
||||||
var mockMetrics = &mockMetricsClient{}
|
var mockMetrics = &mockMetricsClient{}
|
||||||
mockMetrics.On("Send", metrics.Command{Command: "ps", Context: "aci", Status: "success", Source: "api"}).Return()
|
mockMetrics.On("Send", metrics.Command{Command: "ps", Context: "aci", Status: "success", Source: "api"}).Return()
|
||||||
interceptor := metricsServerInterceptor(context.TODO(), mockMetrics)
|
newClient := client.NewClient("aci", noopService{})
|
||||||
|
interceptor := metricsServerInterceptor(mockMetrics)
|
||||||
|
|
||||||
_, err := interceptor(incomingContext("aci"), nil, containerMethodRoute("List"), mockHandler(nil))
|
ctx := proxy.WithClient(incomingContext("acicontext"), &newClient)
|
||||||
|
_, err := interceptor(ctx, nil, containerMethodRoute("List"), mockHandler(nil))
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTrackSFailures(t *testing.T) {
|
func TestTrackSFailures(t *testing.T) {
|
||||||
var mockMetrics = &mockMetricsClient{}
|
var mockMetrics = &mockMetricsClient{}
|
||||||
mockMetrics.On("Send", metrics.Command{Command: "ps", Context: "default", Status: "failure", Source: "api"}).Return()
|
newClient := client.NewClient("moby", noopService{})
|
||||||
interceptor := metricsServerInterceptor(context.TODO(), mockMetrics)
|
interceptor := metricsServerInterceptor(mockMetrics)
|
||||||
|
|
||||||
_, err := interceptor(incomingContext("default"), nil, containerMethodRoute("Create"), mockHandler(errdefs.ErrLoginRequired))
|
ctx := proxy.WithClient(incomingContext("default"), &newClient)
|
||||||
|
_, err := interceptor(ctx, nil, containerMethodRoute("Create"), mockHandler(errdefs.ErrLoginRequired))
|
||||||
assert.Assert(t, err == errdefs.ErrLoginRequired)
|
assert.Assert(t, err == errdefs.ErrLoginRequired)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,6 +108,13 @@ func setupServer() *grpc.Server {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type noopService struct{}
|
||||||
|
|
||||||
|
func (noopService) ContainerService() containers.Service { return nil }
|
||||||
|
func (noopService) ComposeService() compose.Service { return nil }
|
||||||
|
func (noopService) SecretsService() secrets.Service { return nil }
|
||||||
|
func (noopService) VolumeService() volumes.Service { return nil }
|
||||||
|
|
||||||
type mockMetricsClient struct {
|
type mockMetricsClient struct {
|
||||||
mock.Mock
|
mock.Mock
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,8 +31,8 @@ import (
|
||||||
type clientKey struct{}
|
type clientKey struct{}
|
||||||
|
|
||||||
// WithClient adds the client to the context
|
// WithClient adds the client to the context
|
||||||
func WithClient(ctx context.Context, c *client.Client) (context.Context, error) {
|
func WithClient(ctx context.Context, c *client.Client) context.Context {
|
||||||
return context.WithValue(ctx, clientKey{}, c), nil
|
return context.WithValue(ctx, clientKey{}, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client returns the client from the context
|
// Client returns the client from the context
|
||||||
|
|
|
@ -33,7 +33,7 @@ func New(ctx context.Context) *grpc.Server {
|
||||||
s := grpc.NewServer(
|
s := grpc.NewServer(
|
||||||
grpc.ChainUnaryInterceptor(
|
grpc.ChainUnaryInterceptor(
|
||||||
unaryServerInterceptor(ctx),
|
unaryServerInterceptor(ctx),
|
||||||
metricsServerInterceptor(ctx, metrics.NewClient()),
|
metricsServerInterceptor(metrics.NewClient()),
|
||||||
),
|
),
|
||||||
grpc.StreamInterceptor(streamServerInterceptor(ctx)),
|
grpc.StreamInterceptor(streamServerInterceptor(ctx)),
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue