Skip to content

Commit

Permalink
Use eks:cluster-name as clusterId (aws#856)
Browse files Browse the repository at this point in the history
* Use eks:cluster-name as clusterId
  • Loading branch information
groodt authored and Claes Mogren committed Apr 20, 2020
1 parent 3698722 commit fa3db63
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions pkg/publisher/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ const (

// maxDataPoints is the maximum number of data points per PutMetricData API request
maxDataPoints = 20

// Default cluster id if unable to detect something more suitable
defaultClusterID = "k8s-cluster"
)

var (
// List of EC2 tags (in priority order) to use as the CLUSTER_ID metric dimension
clusterIDTags = []string{
"eks:cluster-name",
"CLUSTER_ID",
"Name",
}
)

// Publisher defines the interface to publish one or more data points
Expand Down Expand Up @@ -84,16 +96,7 @@ func New(ctx context.Context) (Publisher, error) {
if err != nil {
return nil, errors.Wrap(err, "publisher: unable to obtain EC2 service client")
}
clusterID, err := ec2Client.GetClusterTag("CLUSTER_ID")
if err != nil || clusterID == "" {
glog.Errorf("Failed to obtain cluster-id, fetching name. %v", err)
clusterID, err = ec2Client.GetClusterTag("Name")
if err != nil || clusterID == "" {
glog.Errorf("Failed to obtain cluster-id or name, defaulting to 'k8s-cluster'. %v", err)
clusterID = "k8s-cluster"
}
}
glog.Info("Using cluster ID ", clusterID)
clusterID := getClusterID(ec2Client)

// Get CloudWatch client
ec2MetadataClient := ec2metadatawrapper.New(nil)
Expand Down Expand Up @@ -207,6 +210,22 @@ func (p *cloudWatchPublisher) getCloudWatchMetricNamespace() *string {
return aws.String(cloudwatchMetricNamespace)
}

func getClusterID(ec2Client *ec2wrapper.EC2Wrapper) string {
var clusterID string
var err error
for _, tag := range clusterIDTags {
clusterID, err = ec2Client.GetClusterTag(tag)
if err == nil && clusterID != "" {
break
}
}
if clusterID == "" {
clusterID = defaultClusterID
}
glog.Info("Using cluster ID ", clusterID)
return clusterID
}

func (p *cloudWatchPublisher) getCloudWatchMetricDatumDimensions() []*cloudwatch.Dimension {
return []*cloudwatch.Dimension{
{
Expand Down

0 comments on commit fa3db63

Please sign in to comment.