From b19777166814f0eba16eb51f7656406d31530636 Mon Sep 17 00:00:00 2001 From: anushree-n Date: Fri, 17 Jul 2015 13:15:02 -0700 Subject: [PATCH] API changes for custom metrics --- api/versions.go | 37 ++++++++++++++++++++++++++++++------- info/v1/container.go | 5 +++++ info/v2/container.go | 5 +++++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/api/versions.go b/api/versions.go index bac878e4..e43149bc 100644 --- a/api/versions.go +++ b/api/versions.go @@ -39,6 +39,7 @@ const ( attributesApi = "attributes" versionApi = "version" psApi = "ps" + customMetricsApi = "appmetrics" ) // Interface for a cAdvisor API version @@ -305,7 +306,7 @@ func (self *version2_0) Version() string { } func (self *version2_0) SupportedRequestTypes() []string { - return []string{versionApi, attributesApi, eventsApi, machineApi, summaryApi, statsApi, specApi, storageApi, psApi} + return []string{versionApi, attributesApi, eventsApi, machineApi, summaryApi, statsApi, specApi, storageApi, psApi, customMetricsApi} } func (self *version2_0) HandleRequest(requestType string, request []string, m manager.Manager, w http.ResponseWriter, r *http.Request) error { @@ -364,6 +365,24 @@ func (self *version2_0) HandleRequest(requestType string, request []string, m ma contStats[name] = convertStats(cont) } return writeResult(contStats, w) + case customMetricsApi: + containerName := getContainerName(request) + glog.V(4).Infof("Api - Custom Metrics: Looking for metrics for container %q, options %+v", containerName, opt) + conts, err := m.GetRequestedContainersInfo(containerName, opt) + if err != nil { + return err + } + contMetrics := make(map[string][][]info.Metric, 0) + metrics := [][]info.Metric{} + for _, cont := range conts { + contStats := convertStats(cont) + for _, contStat := range contStats { + metric := contStat.CustomMetrics + metrics = append(metrics, metric) + } + contMetrics[containerName] = metrics + } + return writeResult(contMetrics, w) case specApi: containerName := getContainerName(request) glog.V(4).Infof("Api - Spec for container %q, options %+v", containerName, opt) @@ -412,12 +431,13 @@ func convertStats(cont *info.ContainerInfo) []v2.ContainerStats { stats := []v2.ContainerStats{} for _, val := range cont.Stats { stat := v2.ContainerStats{ - Timestamp: val.Timestamp, - HasCpu: cont.Spec.HasCpu, - HasMemory: cont.Spec.HasMemory, - HasNetwork: cont.Spec.HasNetwork, - HasFilesystem: cont.Spec.HasFilesystem, - HasDiskIo: cont.Spec.HasDiskIo, + Timestamp: val.Timestamp, + HasCpu: cont.Spec.HasCpu, + HasMemory: cont.Spec.HasMemory, + HasNetwork: cont.Spec.HasNetwork, + HasFilesystem: cont.Spec.HasFilesystem, + HasDiskIo: cont.Spec.HasDiskIo, + HasCustomMetrics: cont.Spec.HasCustomMetrics, } if stat.HasCpu { stat.Cpu = val.Cpu @@ -434,6 +454,9 @@ func convertStats(cont *info.ContainerInfo) []v2.ContainerStats { if stat.HasDiskIo { stat.DiskIo = val.DiskIo } + if stat.HasCustomMetrics { + stat.CustomMetrics = val.CustomMetrics + } // TODO(rjnagal): Handle load stats. stats = append(stats, stat) } diff --git a/info/v1/container.go b/info/v1/container.go index 1f22c35e..38e387a5 100644 --- a/info/v1/container.go +++ b/info/v1/container.go @@ -58,6 +58,8 @@ type ContainerSpec struct { // HasDiskIo when true, indicates that DiskIo stats will be available. HasDiskIo bool `json:"has_diskio"` + + HasCustomMetrics bool `json:"has_custom_metrics"` } // Container reference contains enough information to uniquely identify a container @@ -190,6 +192,9 @@ func (self *ContainerSpec) Eq(b *ContainerSpec) bool { if self.HasDiskIo != b.HasDiskIo { return false } + if self.HasCustomMetrics != b.HasCustomMetrics { + return false + } return true } diff --git a/info/v2/container.go b/info/v2/container.go index 7d9c388c..782389f6 100644 --- a/info/v2/container.go +++ b/info/v2/container.go @@ -73,6 +73,8 @@ type ContainerSpec struct { HasMemory bool `json:"has_memory"` Memory MemorySpec `json:"memory,omitempty"` + HasCustomMetrics bool `json:"has_custom_metrics"` + // Following resources have no associated spec, but are being isolated. HasNetwork bool `json:"has_network"` HasFilesystem bool `json:"has_filesystem"` @@ -100,6 +102,9 @@ type ContainerStats struct { // Task load statistics HasLoad bool `json:"has_load"` Load v1.LoadStats `json:"load_stats,omitempty"` + //Custom statistics + HasCustomMetrics bool `json:"has_custom_metrics"` + CustomMetrics []v1.Metric `json:"custom_metrics,omitempty"` } type Percentiles struct {