Skip to content

Commit

Permalink
Add resource name and status to outgoing sg-core metrics
Browse files Browse the repository at this point in the history
Rename `vm_instance` field to `host` which correctly represents
the host on which a particular resource exists.
  • Loading branch information
yadneshk committed Aug 7, 2023
1 parent 4c79806 commit d8e3c96
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 30 deletions.
22 changes: 18 additions & 4 deletions plugins/handler/ceilometer-metrics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ func genName(cNameShards []string) string {
}

func genLabels(m ceilometer.Metric, publisher string, cNameShards []string) ([]string, []string) {
labelKeys := make([]string, 11) // TODO: set to persistent var
labelVals := make([]string, 11)
labelKeys := make([]string, 12) // TODO: set to persistent var
labelVals := make([]string, 12)
plugin := cNameShards[0]
pluginVal := m.ResourceID
if len(cNameShards) > 2 {
Expand Down Expand Up @@ -253,12 +253,26 @@ func genLabels(m ceilometer.Metric, publisher string, cNameShards []string) ([]s
index++
}

if m.ResourceMetadata.Host != "" {
labelKeys[index] = "vm_instance"
if m.ResourceMetadata.InstanceHost != "" {
labelKeys[index] = "host"
labelVals[index] = m.ResourceMetadata.InstanceHost
index++
} else if m.ResourceMetadata.Host != "" {
labelKeys[index] = "host"
labelVals[index] = m.ResourceMetadata.Host
index++
}

if m.ResourceMetadata.DisplayName != "" {
labelKeys[index] = "resource_name"
labelVals[index] = m.ResourceMetadata.DisplayName
index++
} else if m.ResourceMetadata.Name != "" {
labelKeys[index] = "resource_name"
labelVals[index] = m.ResourceMetadata.Name
index++
}

return labelKeys[:index], labelVals[:index]
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/handler/ceilometer-metrics/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ var expectedMsgpackMetric = data.Metric{
Type: data.UNTYPED,
Interval: 100 * time.Second,
Value: 0,
LabelKeys: []string{"test_name_0_0_82", "publisher", "type", "counter", "project", "project_name", "user", "user_name", "unit", "resource"},
LabelVals: []string{"test_resource_id", "localhost.localdomain", "test_name_0_0_82", "test_name_0_0_82", "test_project_id_0", "test_project_name_0", "test_user_id", "test_user_name", "test_unit", "test_resource_id"},
LabelKeys: []string{"test_name_0_0_82", "publisher", "type", "counter", "project", "project_name", "user", "user_name", "unit", "resource", "host", "resource_name"},
LabelVals: []string{"test_resource_id", "localhost.localdomain", "test_name_0_0_82", "test_name_0_0_82", "test_project_id_0", "test_project_name_0", "test_user_id", "test_user_name", "test_unit", "test_resource_id", "test_compute-0.localdomain", "test_display_name"},
}

// CeilometerMetricTemplate holds correct parsings for comparing against parsed results
Expand Down Expand Up @@ -103,6 +103,7 @@ func TestCeilometerIncomingMsgpack(t *testing.T) {
}

testData, err := os.ReadFile("messages/msgpack-test.msgpack")
fmt.Println(string(testData))
if err != nil {
t.Errorf("failed loading test data: %s", err.Error())
}
Expand Down Expand Up @@ -173,7 +174,6 @@ func TestGenLabelsSizes(t *testing.T) {
// must always be same size since they represent a map
assert.Equal(t, len(labelKeys), len(labelVals))

fmt.Println(labelKeys)
// should have 11 labels
assert.Equal(t, len(labelKeys), 11)

Expand Down
30 changes: 20 additions & 10 deletions plugins/handler/ceilometer-metrics/messages/metric-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"user_name",
"unit",
"resource",
"vm_instance"
"host",
"resource_name"
],
"LabelVals": [
"size",
Expand All @@ -35,7 +36,8 @@
"test_user",
"GB",
"d8bd99b6-6fd8-4c02-a2e3-efbf596df636",
"compute-0.redhat.local"
"compute-0.redhat.local",
"new-instance"
],
"Time": 1600099969,
"Type": 2,
Expand All @@ -55,7 +57,8 @@
"user_name",
"unit",
"resource",
"vm_instance"
"host",
"resource_name"
],
"LabelVals": [
"size",
Expand All @@ -68,7 +71,8 @@
"test_user",
"GB",
"d8bd99b6-6fd8-4c02-a2e3-efbf596df636",
"compute-0.redhat.local"
"compute-0.redhat.local",
"new-instance"
],
"Time": 1600099969,
"Type": 2,
Expand All @@ -86,7 +90,8 @@
"project_name",
"unit",
"resource",
"vm_instance"
"host",
"resource_name"
],
"LabelVals": [
"booting",
Expand All @@ -97,7 +102,8 @@
"test_project",
"sec",
"d8bd99b6-6fd8-4c02-a2e3-efbf596df636",
"compute-0.redhat.local"
"compute-0.redhat.local",
"new-instance"
],
"Time": 1600099969,
"Type": 2,
Expand All @@ -117,7 +123,8 @@
"user_name",
"unit",
"resource",
"vm_instance"
"host",
"resource_name"
],
"LabelVals": [
"d8bd99b6-6fd8-4c02-a2e3-efbf596df636",
Expand All @@ -130,7 +137,8 @@
"test_user",
"vcpu",
"d8bd99b6-6fd8-4c02-a2e3-efbf596df636",
"compute-0.redhat.local"
"compute-0.redhat.local",
"new-instance"
],
"Time": 1600099969,
"Type": 2,
Expand All @@ -150,7 +158,8 @@
"user_name",
"unit",
"resource",
"vm_instance"
"host",
"resource_name"
],
"LabelVals": [
"d8bd99b6-6fd8-4c02-a2e3-efbf596df636",
Expand All @@ -163,7 +172,8 @@
"test_user",
"MB",
"d8bd99b6-6fd8-4c02-a2e3-efbf596df636",
"compute-0.redhat.local"
"compute-0.redhat.local",
"new-instance"
],
"Time": 1600099969,
"Type": 2,
Expand Down
Binary file modified plugins/handler/ceilometer-metrics/messages/msgpack-test.msgpack
Binary file not shown.
29 changes: 16 additions & 13 deletions plugins/handler/ceilometer-metrics/pkg/ceilometer/ceilometer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,26 @@ var (

// Metedata represents metadataof a metric from ceilometer
type Metadata struct {
Host string
Host string `json:"host" msgpack:"host"`
Name string `json:"name" msgpack:"name"`
DisplayName string `json:"display_name" msgpack:"display_name"`
InstanceHost string `json:"instance_host" msgpack:"instance_host"`
}

// Metric represents a single metric from ceilometer for unmarshalling
type Metric struct {
Source string
CounterName string `json:"counter_name" msgpack:"counter_name"`
CounterType string `json:"counter_type" msgpack:"counter_type"`
CounterUnit string `json:"counter_unit" msgpack:"counter_unit"`
CounterVolume float64 `json:"counter_volume" msgpack:"counter_volume"`
UserID string `json:"user_id" msgpack:"user_id"`
UserName string `json:"user_name" msgpack:"user_name"`
ProjectID string `json:"project_id" msgpack:"project_id"`
ProjectName string `json:"project_name" msgpack:"project_name"`
ResourceID string `json:"resource_id" msgpack:"resource_id"`
Timestamp string
ResourceMetadata Metadata `json:"resource_metadata"`
Source string `json:"source" msgpack:"source"`
CounterName string `json:"counter_name" msgpack:"counter_name"`
CounterType string `json:"counter_type" msgpack:"counter_type"`
CounterUnit string `json:"counter_unit" msgpack:"counter_unit"`
CounterVolume float64 `json:"counter_volume" msgpack:"counter_volume"`
UserID string `json:"user_id" msgpack:"user_id"`
UserName string `json:"user_name" msgpack:"user_name"`
ProjectID string `json:"project_id" msgpack:"project_id"`
ProjectName string `json:"project_name" msgpack:"project_name"`
ResourceID string `json:"resource_id" msgpack:"resource_id"`
Timestamp string `json:"timestamp" msgpack:"timestamp"`
ResourceMetadata Metadata `json:"resource_metadata" msgpack:"resource_metadata"`
}

// Message struct represents an incoming ceilometer metrics message
Expand Down

0 comments on commit d8e3c96

Please sign in to comment.