Skip to content

Commit

Permalink
Merge pull request #65 from liwenwu-amazon/metrics
Browse files Browse the repository at this point in the history
Add prometheus metrics
  • Loading branch information
liwenwu-amazon authored May 15, 2018
2 parents 7e541ee + 8e4f6bd commit d69852b
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 16 deletions.
38 changes: 37 additions & 1 deletion Gopkg.lock

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

41 changes: 41 additions & 0 deletions ipamd/datastore/data_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

log "github.com/cihub/seelog"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"

"github.com/aws/amazon-vpc-cni-k8s/pkg/k8sapi"
)
Expand All @@ -45,6 +46,28 @@ var ErrUnknownPod = errors.New("datastore: unknown pod")
// ErrUnknownPodIP is an error where pod's IP address is not found in data store
var ErrUnknownPodIP = errors.New("datastore: pod using unknown IP address")

var (
enis = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "eni_allocated",
Help: "The number of ENI allocated",
},
)
totalIPs = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "total_ip_addresses",
Help: "The total number of IP addresses",
},
)
assignedIPs = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "assigned_ip_addresses",
Help: "The number of IP addresses assigned",
},
)
prometheusRegistered = false
)

// ENIIPPool contains ENI/IP Pool information. Exported fields will be Marshaled for introspection.
type ENIIPPool struct {
createTime time.Time
Expand Down Expand Up @@ -105,8 +128,18 @@ type ENIInfos struct {
ENIIPPools map[string]ENIIPPool
}

func prometheusRegister() {
if !prometheusRegistered {
prometheus.MustRegister(enis)
prometheus.MustRegister(totalIPs)
prometheus.MustRegister(assignedIPs)
prometheusRegistered = true
}
}

// NewDataStore returns DataStore structure
func NewDataStore() *DataStore {
prometheusRegister()
return &DataStore{
eniIPPools: make(map[string]*ENIIPPool),
podsIP: make(map[PodKey]PodIPInfo),
Expand All @@ -130,6 +163,7 @@ func (ds *DataStore) AddENI(eniID string, deviceNumber int, isPrimary bool) erro
Id: eniID,
DeviceNumber: deviceNumber,
IPv4Addresses: make(map[string]*AddressInfo)}
enis.Set(float64(len(ds.eniIPPools)))
return nil
}

Expand All @@ -153,6 +187,7 @@ func (ds *DataStore) AddENIIPv4Address(eniID string, ipv4 string) error {
}

ds.total++
totalIPs.Set(float64(ds.total))

curENI.IPv4Addresses[ipv4] = &AddressInfo{address: ipv4, Assigned: false}

Expand Down Expand Up @@ -213,6 +248,7 @@ func (ds *DataStore) assignPodIPv4AddressUnsafe(k8sPod *k8sapi.K8SPodInfo) (stri
if !addr.Assigned {
ds.assigned++
eni.AssignedIPv4Addresses++
assignedIPs.Set(float64(ds.assigned))
addr.Assigned = true
}
ds.podsIP[podKey] = PodIPInfo{IP: addr.address, DeviceNumber: eni.DeviceNumber}
Expand All @@ -223,6 +259,7 @@ func (ds *DataStore) assignPodIPv4AddressUnsafe(k8sPod *k8sapi.K8SPodInfo) (stri
if !addr.Assigned && k8sPod.IP == "" && curTime.Sub(addr.unAssignedTime) > addressCoolingPeriod {
// This is triggered by a pod's Add Network command from CNI plugin
ds.assigned++
assignedIPs.Set(float64(ds.assigned))
eni.AssignedIPv4Addresses++
addr.Assigned = true
ds.podsIP[podKey] = PodIPInfo{IP: addr.address, DeviceNumber: eni.DeviceNumber}
Expand Down Expand Up @@ -300,6 +337,9 @@ func (ds *DataStore) FreeENI() (string, error) {
deletedENI := deletableENI.Id
delete(ds.eniIPPools, deletableENI.Id)

enis.Set(float64(len(ds.eniIPPools)))
assignedIPs.Set(float64(ds.assigned))
totalIPs.Set(float64(ds.total))
return deletedENI, nil
}

Expand Down Expand Up @@ -328,6 +368,7 @@ func (ds *DataStore) UnAssignPodIPv4Address(k8sPod *k8sapi.K8SPodInfo) (string,
if ok && ip.Assigned {
ip.Assigned = false
ds.assigned--
assignedIPs.Set(float64(ds.assigned))
eni.AssignedIPv4Addresses--
curTime := time.Now()
ip.unAssignedTime = curTime
Expand Down
10 changes: 9 additions & 1 deletion ipamd/introspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import (
"time"

log "github.com/cihub/seelog"
"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/aws/amazon-vpc-cni-k8s/pkg/utils"
)

const (
// IntrospectionPort is the port for ipamd introspection
IntrospectionPort = 51678
IntrospectionPort = 61678
)

type rootResponse struct {
Expand Down Expand Up @@ -91,6 +92,7 @@ func (c *IPAMContext) setupServer() *http.Server {
for key, fn := range serverFunctions {
serveMux.HandleFunc(key, fn)
}
serveMux.Handle("/metrics", promhttp.Handler())

// Log all requests and then pass through to serveMux
loggingServeMux := http.NewServeMux()
Expand Down Expand Up @@ -129,3 +131,9 @@ func podV1RequestHandler(ipam *IPAMContext) func(http.ResponseWriter, *http.Requ
w.Write(responseJSON)
}
}

func metricsHandler(ipam *IPAMContext) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
promhttp.Handler()
}
}
Loading

0 comments on commit d69852b

Please sign in to comment.