Add test for client metrics
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
f45269be6b
commit
8510512e7e
@ -2926,9 +2926,16 @@ file {
|
||||
json_name: "timestamp"
|
||||
}
|
||||
field {
|
||||
name: "data"
|
||||
name: "id"
|
||||
number: 2
|
||||
label: LABEL_OPTIONAL
|
||||
type: TYPE_STRING
|
||||
json_name: "id"
|
||||
}
|
||||
field {
|
||||
name: "data"
|
||||
number: 3
|
||||
label: LABEL_OPTIONAL
|
||||
type: TYPE_MESSAGE
|
||||
type_name: ".google.protobuf.Any"
|
||||
json_name: "data"
|
||||
|
@ -44,7 +44,8 @@ func (*MetricsResponse) Descriptor() ([]byte, []int) { return fileDescriptorMetr
|
||||
|
||||
type Metric struct {
|
||||
Timestamp time.Time `protobuf:"bytes,1,opt,name=timestamp,stdtime" json:"timestamp"`
|
||||
Data *google_protobuf1.Any `protobuf:"bytes,2,opt,name=data" json:"data,omitempty"`
|
||||
ID string `protobuf:"bytes,2,opt,name=id,proto3" json:"id,omitempty"`
|
||||
Data *google_protobuf1.Any `protobuf:"bytes,3,opt,name=data" json:"data,omitempty"`
|
||||
}
|
||||
|
||||
func (m *Metric) Reset() { *m = Metric{} }
|
||||
@ -142,9 +143,15 @@ func (m *Metric) MarshalTo(dAtA []byte) (int, error) {
|
||||
return 0, err
|
||||
}
|
||||
i += n1
|
||||
if m.Data != nil {
|
||||
if len(m.ID) > 0 {
|
||||
dAtA[i] = 0x12
|
||||
i++
|
||||
i = encodeVarintMetrics(dAtA, i, uint64(len(m.ID)))
|
||||
i += copy(dAtA[i:], m.ID)
|
||||
}
|
||||
if m.Data != nil {
|
||||
dAtA[i] = 0x1a
|
||||
i++
|
||||
i = encodeVarintMetrics(dAtA, i, uint64(m.Data.Size()))
|
||||
n2, err := m.Data.MarshalTo(dAtA[i:])
|
||||
if err != nil {
|
||||
@ -211,6 +218,10 @@ func (m *Metric) Size() (n int) {
|
||||
_ = l
|
||||
l = github_com_gogo_protobuf_types.SizeOfStdTime(m.Timestamp)
|
||||
n += 1 + l + sovMetrics(uint64(l))
|
||||
l = len(m.ID)
|
||||
if l > 0 {
|
||||
n += 1 + l + sovMetrics(uint64(l))
|
||||
}
|
||||
if m.Data != nil {
|
||||
l = m.Data.Size()
|
||||
n += 1 + l + sovMetrics(uint64(l))
|
||||
@ -257,6 +268,7 @@ func (this *Metric) String() string {
|
||||
}
|
||||
s := strings.Join([]string{`&Metric{`,
|
||||
`Timestamp:` + strings.Replace(strings.Replace(this.Timestamp.String(), "Timestamp", "google_protobuf2.Timestamp", 1), `&`, ``, 1) + `,`,
|
||||
`ID:` + fmt.Sprintf("%v", this.ID) + `,`,
|
||||
`Data:` + strings.Replace(fmt.Sprintf("%v", this.Data), "Any", "google_protobuf1.Any", 1) + `,`,
|
||||
`}`,
|
||||
}, "")
|
||||
@ -490,6 +502,35 @@ func (m *Metric) Unmarshal(dAtA []byte) error {
|
||||
}
|
||||
iNdEx = postIndex
|
||||
case 2:
|
||||
if wireType != 2 {
|
||||
return fmt.Errorf("proto: wrong wireType = %d for field ID", wireType)
|
||||
}
|
||||
var stringLen uint64
|
||||
for shift := uint(0); ; shift += 7 {
|
||||
if shift >= 64 {
|
||||
return ErrIntOverflowMetrics
|
||||
}
|
||||
if iNdEx >= l {
|
||||
return io.ErrUnexpectedEOF
|
||||
}
|
||||
b := dAtA[iNdEx]
|
||||
iNdEx++
|
||||
stringLen |= (uint64(b) & 0x7F) << shift
|
||||
if b < 0x80 {
|
||||
break
|
||||
}
|
||||
}
|
||||
intStringLen := int(stringLen)
|
||||
if intStringLen < 0 {
|
||||
return ErrInvalidLengthMetrics
|
||||
}
|
||||
postIndex := iNdEx + intStringLen
|
||||
if postIndex > l {
|
||||
return io.ErrUnexpectedEOF
|
||||
}
|
||||
m.ID = string(dAtA[iNdEx:postIndex])
|
||||
iNdEx = postIndex
|
||||
case 3:
|
||||
if wireType != 2 {
|
||||
return fmt.Errorf("proto: wrong wireType = %d for field Data", wireType)
|
||||
}
|
||||
@ -653,24 +694,25 @@ func init() {
|
||||
}
|
||||
|
||||
var fileDescriptorMetrics = []byte{
|
||||
// 297 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xb2, 0x48, 0xcf, 0x2c, 0xc9,
|
||||
0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xce, 0xcf, 0x2b, 0x49, 0xcc, 0xcc, 0x4b, 0x2d,
|
||||
0x4a, 0x41, 0x66, 0x26, 0x16, 0x64, 0xea, 0x97, 0x54, 0x16, 0xa4, 0x16, 0xeb, 0xe7, 0xa6, 0x96,
|
||||
0x14, 0x65, 0x26, 0x17, 0xeb, 0x15, 0x14, 0xe5, 0x97, 0xe4, 0x0b, 0x09, 0x20, 0xd4, 0xe8, 0x81,
|
||||
0xe5, 0xa5, 0x44, 0xd2, 0xf3, 0xd3, 0xf3, 0xc1, 0x92, 0xfa, 0x20, 0x16, 0x44, 0x9d, 0x94, 0x64,
|
||||
0x7a, 0x7e, 0x7e, 0x7a, 0x4e, 0xaa, 0x3e, 0x98, 0x97, 0x54, 0x9a, 0xa6, 0x9f, 0x98, 0x57, 0x09,
|
||||
0x95, 0x92, 0x47, 0x97, 0x2a, 0xc9, 0xcc, 0x4d, 0x2d, 0x2e, 0x49, 0xcc, 0x2d, 0x80, 0x28, 0x50,
|
||||
0xd2, 0xe2, 0xe2, 0xf3, 0x85, 0x58, 0x1a, 0x94, 0x5a, 0x58, 0x9a, 0x5a, 0x5c, 0x22, 0x24, 0xc1,
|
||||
0xc5, 0x9e, 0x96, 0x99, 0x53, 0x92, 0x5a, 0x54, 0x2c, 0xc1, 0xa8, 0xc0, 0xac, 0xc1, 0x19, 0x04,
|
||||
0xe3, 0x2a, 0xb9, 0x72, 0xf1, 0xc3, 0xd5, 0x16, 0x17, 0xe4, 0xe7, 0x15, 0xa7, 0x0a, 0x19, 0x71,
|
||||
0xb1, 0x43, 0xdd, 0x0c, 0x56, 0xcc, 0x6d, 0x24, 0xa1, 0x87, 0xee, 0x68, 0x3d, 0x88, 0x9e, 0x20,
|
||||
0x98, 0x42, 0xa5, 0x32, 0x2e, 0x36, 0x88, 0x90, 0x90, 0x13, 0x17, 0x27, 0xdc, 0x3d, 0x12, 0x8c,
|
||||
0x0a, 0x8c, 0x1a, 0xdc, 0x46, 0x52, 0x7a, 0x10, 0x17, 0xeb, 0xc1, 0x5c, 0xac, 0x17, 0x02, 0x53,
|
||||
0xe1, 0xc4, 0x71, 0xe2, 0x9e, 0x3c, 0xc3, 0x84, 0xfb, 0xf2, 0x8c, 0x41, 0x08, 0x6d, 0x42, 0x1a,
|
||||
0x5c, 0x2c, 0x29, 0x89, 0x25, 0x89, 0x12, 0x4c, 0x60, 0xed, 0x22, 0x18, 0xda, 0x1d, 0xf3, 0x2a,
|
||||
0x83, 0xc0, 0x2a, 0x9c, 0xbc, 0x4e, 0x3c, 0x94, 0x63, 0xb8, 0xf1, 0x50, 0x8e, 0xa1, 0xe1, 0x91,
|
||||
0x1c, 0xe3, 0x89, 0x47, 0x72, 0x8c, 0x17, 0x1e, 0xc9, 0x31, 0x3e, 0x78, 0x24, 0xc7, 0x18, 0x65,
|
||||
0x40, 0x7c, 0x14, 0x59, 0x83, 0xc9, 0x24, 0x36, 0xb0, 0xf9, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff,
|
||||
0xff, 0xa5, 0x43, 0x7b, 0x37, 0xdd, 0x01, 0x00, 0x00,
|
||||
// 319 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x91, 0x31, 0x4b, 0x03, 0x31,
|
||||
0x14, 0xc7, 0x9b, 0xab, 0xb4, 0x36, 0x05, 0x95, 0xa3, 0x48, 0xec, 0x90, 0x2b, 0x9d, 0x0e, 0x87,
|
||||
0x44, 0xea, 0x22, 0x38, 0x79, 0xe8, 0xa0, 0xe0, 0x12, 0x9c, 0xdc, 0xd2, 0x5e, 0x7a, 0x06, 0x7a,
|
||||
0x97, 0xf3, 0x92, 0x0e, 0xdd, 0xfc, 0x04, 0xe2, 0xc7, 0xba, 0xd1, 0xd1, 0xa9, 0xda, 0xfb, 0x24,
|
||||
0x62, 0xd2, 0x6b, 0xa5, 0x2e, 0x2e, 0xe1, 0xbd, 0xfc, 0x7f, 0xef, 0xe5, 0x07, 0x81, 0x17, 0x89,
|
||||
0x34, 0x4f, 0xf3, 0x31, 0x99, 0xa8, 0x94, 0x4e, 0x54, 0x66, 0xb8, 0xcc, 0x44, 0x11, 0xff, 0x2e,
|
||||
0x79, 0x2e, 0xa9, 0x59, 0xe4, 0x42, 0xd3, 0x54, 0x98, 0x42, 0x4e, 0x34, 0xc9, 0x0b, 0x65, 0x94,
|
||||
0x7f, 0xb4, 0x65, 0x88, 0xcd, 0xfb, 0xbd, 0x44, 0x25, 0xca, 0x86, 0xf4, 0xa7, 0x72, 0x5c, 0xff,
|
||||
0x24, 0x51, 0x2a, 0x99, 0x09, 0x6a, 0xbb, 0xf1, 0x7c, 0x4a, 0x79, 0xb6, 0x58, 0x47, 0xc1, 0x6e,
|
||||
0x64, 0x64, 0x2a, 0xb4, 0xe1, 0x69, 0xee, 0x80, 0xe1, 0x29, 0x3c, 0xb8, 0x77, 0x8f, 0x32, 0xf1,
|
||||
0x3c, 0x17, 0xda, 0xf8, 0x08, 0xb6, 0xa7, 0x72, 0x66, 0x44, 0xa1, 0x11, 0x18, 0x34, 0xc3, 0x0e,
|
||||
0xab, 0xdb, 0xe1, 0x0d, 0x3c, 0xdc, 0xb0, 0x3a, 0x57, 0x99, 0x16, 0xfe, 0x08, 0xb6, 0xd7, 0xce,
|
||||
0x16, 0xee, 0x8e, 0x10, 0xd9, 0x95, 0x26, 0x6e, 0x86, 0xd5, 0xe0, 0xf0, 0x15, 0xc0, 0x96, 0xbb,
|
||||
0xf3, 0x23, 0xd8, 0xd9, 0x08, 0x21, 0x30, 0x00, 0x61, 0x77, 0xd4, 0x27, 0x4e, 0x99, 0xd4, 0xca,
|
||||
0xe4, 0xa1, 0x26, 0xa2, 0xfd, 0x72, 0x19, 0x34, 0xde, 0x3e, 0x03, 0xc0, 0xb6, 0x63, 0xfe, 0x31,
|
||||
0xf4, 0x64, 0x8c, 0xbc, 0x01, 0x08, 0x3b, 0x51, 0xab, 0x5a, 0x06, 0xde, 0xed, 0x35, 0xf3, 0x64,
|
||||
0xec, 0x87, 0x70, 0x2f, 0xe6, 0x86, 0xa3, 0xa6, 0x5d, 0xdb, 0xfb, 0xb3, 0xf6, 0x2a, 0x5b, 0x30,
|
||||
0x4b, 0x44, 0x77, 0xe5, 0x0a, 0x37, 0x3e, 0x56, 0xb8, 0xf1, 0x52, 0x61, 0x50, 0x56, 0x18, 0xbc,
|
||||
0x57, 0x18, 0x7c, 0x55, 0x18, 0x3c, 0x9e, 0xfd, 0xff, 0xef, 0x2e, 0xed, 0x39, 0x6e, 0xd9, 0xfd,
|
||||
0xe7, 0xdf, 0x01, 0x00, 0x00, 0xff, 0xff, 0x95, 0xe4, 0x62, 0x3d, 0xf6, 0x01, 0x00, 0x00,
|
||||
}
|
||||
|
@ -18,5 +18,6 @@ message MetricsResponse {
|
||||
|
||||
message Metric {
|
||||
google.protobuf.Timestamp timestamp = 1 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
|
||||
google.protobuf.Any data = 2;
|
||||
string id = 2;
|
||||
google.protobuf.Any data = 3;
|
||||
}
|
||||
|
@ -1307,3 +1307,61 @@ func TestDeleteContainerExecCreated(t *testing.T) {
|
||||
}
|
||||
<-finished
|
||||
}
|
||||
|
||||
func TestContainerMetrics(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client, err := newClient(t, address)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
var (
|
||||
image Image
|
||||
ctx, cancel = testContext()
|
||||
id = t.Name()
|
||||
)
|
||||
defer cancel()
|
||||
|
||||
if runtime.GOOS != "windows" {
|
||||
image, err = client.GetImage(ctx, testImage)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
container, err := client.NewContainer(ctx, id, WithNewSpec(withImageConfig(image), WithProcessArgs("sleep", "30")), withNewSnapshot(id, image))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer container.Delete(ctx, WithSnapshotCleanup)
|
||||
|
||||
task, err := container.NewTask(ctx, empty())
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer task.Delete(ctx)
|
||||
|
||||
statusC, err := task.Wait(ctx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
metric, err := task.Metrics(ctx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if metric.ID != id {
|
||||
t.Errorf("expected metric id %q but received %q", id, metric.ID)
|
||||
}
|
||||
if err := task.Kill(ctx, syscall.SIGKILL); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
<-statusC
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ import (
|
||||
"github.com/containerd/containerd/content"
|
||||
"github.com/containerd/containerd/errdefs"
|
||||
"github.com/containerd/containerd/events"
|
||||
"github.com/containerd/containerd/filters"
|
||||
"github.com/containerd/containerd/images"
|
||||
"github.com/containerd/containerd/log"
|
||||
"github.com/containerd/containerd/metadata"
|
||||
@ -457,33 +458,57 @@ func (s *Service) Update(ctx context.Context, r *api.UpdateTaskRequest) (*google
|
||||
}
|
||||
|
||||
func (s *Service) Metrics(ctx context.Context, r *types.MetricsRequest) (*types.MetricsResponse, error) {
|
||||
filter, err := filters.ParseAll(r.Filters...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var resp types.MetricsResponse
|
||||
for _, r := range s.runtimes {
|
||||
tasks, err := r.Tasks(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, t := range tasks {
|
||||
collected := time.Now()
|
||||
metrics, err := t.Metrics(ctx)
|
||||
if err != nil {
|
||||
log.G(ctx).WithError(err).Errorf("collecting metrics for %s", t.ID())
|
||||
continue
|
||||
}
|
||||
data, err := typeurl.MarshalAny(metrics)
|
||||
if err != nil {
|
||||
log.G(ctx).WithError(err).Errorf("marshal metrics for %s", t.ID())
|
||||
continue
|
||||
}
|
||||
resp.Metrics = append(resp.Metrics, &types.Metric{
|
||||
Timestamp: collected,
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
getTasksMetrics(ctx, filter, tasks, &resp)
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func getTasksMetrics(ctx context.Context, filter filters.Filter, tasks []runtime.Task, r *types.MetricsResponse) {
|
||||
for _, tk := range tasks {
|
||||
if !filter.Match(filters.AdapterFunc(func(fieldpath []string) (string, bool) {
|
||||
t := tk
|
||||
switch fieldpath[0] {
|
||||
case "id":
|
||||
return t.ID(), true
|
||||
case "namespace":
|
||||
return t.Info().Namespace, true
|
||||
case "runtime":
|
||||
return t.Info().Runtime, true
|
||||
}
|
||||
return "", false
|
||||
})) {
|
||||
continue
|
||||
}
|
||||
|
||||
collected := time.Now()
|
||||
metrics, err := tk.Metrics(ctx)
|
||||
if err != nil {
|
||||
log.G(ctx).WithError(err).Errorf("collecting metrics for %s", tk.ID())
|
||||
continue
|
||||
}
|
||||
data, err := typeurl.MarshalAny(metrics)
|
||||
if err != nil {
|
||||
log.G(ctx).WithError(err).Errorf("marshal metrics for %s", tk.ID())
|
||||
continue
|
||||
}
|
||||
r.Metrics = append(r.Metrics, &types.Metric{
|
||||
ID: tk.ID(),
|
||||
Timestamp: collected,
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) writeContent(ctx context.Context, mediaType, ref string, r io.Reader) (*types.Descriptor, error) {
|
||||
writer, err := s.store.Writer(ctx, ref, 0, "")
|
||||
if err != nil {
|
||||
|
14
task.go
14
task.go
@ -118,6 +118,8 @@ type Task interface {
|
||||
Update(context.Context, ...UpdateTaskOpts) error
|
||||
// LoadProcess loads a previously created exec'd process
|
||||
LoadProcess(context.Context, string, IOAttach) (Process, error)
|
||||
// Metrics returns task metrics for runtime specific metrics
|
||||
Metrics(context.Context) (*types.Metric, error)
|
||||
}
|
||||
|
||||
var _ = (Task)(&task{})
|
||||
@ -472,6 +474,18 @@ func (t *task) LoadProcess(ctx context.Context, id string, ioAttach IOAttach) (P
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *task) Metrics(ctx context.Context) (*types.Metric, error) {
|
||||
response, err := t.client.TaskService().Metrics(ctx, &types.MetricsRequest{
|
||||
Filters: []string{
|
||||
"id==" + t.id,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errdefs.FromGRPC(err)
|
||||
}
|
||||
return response.Metrics[0], nil
|
||||
}
|
||||
|
||||
func (t *task) checkpointTask(ctx context.Context, index *v1.Index, request *tasks.CheckpointTaskRequest) error {
|
||||
response, err := t.client.TaskService().Checkpoint(ctx, request)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user