From eb1251fe5bf18e7f692cfe55128481da4addf9ad Mon Sep 17 00:00:00 2001 From: Yadnesh Kulkarni Date: Fri, 28 Apr 2023 14:36:21 +0530 Subject: [PATCH] Make room for project/user names in metric messages With changes [1] Ceilometer is now able to include "project_name" and "user_name" fields into it's metrics. These fields are only added when ceilometer has `tenant_name_discovery` config enabled. This change amends these fields to the outgoing metric messages. Fixes: #110 [1] https://review.opendev.org/q/topic:enhanced_metrics-stable/wallaby --- plugins/handler/ceilometer-metrics/main.go | 22 +++++++++++++-- .../handler/ceilometer-metrics/main_test.go | 16 +++++++---- .../messages/metric-tests.json | 28 ++++++++++++++++++- .../pkg/ceilometer/ceilometer.go | 2 ++ 4 files changed, 59 insertions(+), 9 deletions(-) diff --git a/plugins/handler/ceilometer-metrics/main.go b/plugins/handler/ceilometer-metrics/main.go index 7ce70e7f..052e9a4d 100644 --- a/plugins/handler/ceilometer-metrics/main.go +++ b/plugins/handler/ceilometer-metrics/main.go @@ -184,8 +184,8 @@ func genName(cNameShards []string) string { } func genLabels(m ceilometer.Metric, publisher string, cNameShards []string) ([]string, []string) { - labelKeys := make([]string, 8) // TODO: set to persistent var - labelVals := make([]string, 8) + labelKeys := make([]string, 11) // TODO: set to persistent var + labelVals := make([]string, 11) plugin := cNameShards[0] pluginVal := m.ResourceID if len(cNameShards) > 2 { @@ -223,6 +223,24 @@ func genLabels(m ceilometer.Metric, publisher string, cNameShards []string) ([]s index++ } + if m.ProjectName != "" { + labelKeys[index] = "project_name" + labelVals[index] = m.ProjectName + index++ + } + + if m.UserID != "" { + labelKeys[index] = "user" + labelVals[index] = m.UserID + index++ + } + + if m.UserName != "" { + labelKeys[index] = "user_name" + labelVals[index] = m.UserName + index++ + } + if m.CounterUnit != "" { labelKeys[index] = "unit" labelVals[index] = m.CounterUnit diff --git a/plugins/handler/ceilometer-metrics/main_test.go b/plugins/handler/ceilometer-metrics/main_test.go index 37d2f854..aa6cbe96 100644 --- a/plugins/handler/ceilometer-metrics/main_test.go +++ b/plugins/handler/ceilometer-metrics/main_test.go @@ -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", "unit", "resource"}, - LabelVals: []string{"test_resource_id", "localhost.localdomain", "test_name_0_0_82", "test_name_0_0_82", "test_project_id_0", "test_unit", "test_resource_id"}, + 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_0", "test_user_name_0", "test_unit", "test_resource_id"}, } // CeilometerMetricTemplate holds correct parsings for comparing against parsed results @@ -127,7 +127,9 @@ func TestGenLabelsSizes(t *testing.T) { CounterUnit: "GB", CounterVolume: 2, UserID: "user_id", + UserName: "user_name", ProjectID: "db3fce7b7aeb4109bb2794f9337e68fa", + ProjectName: "test_project", ResourceID: "ed8102c3-923a-4f5a-9a24-d59afc174755", Timestamp: "2021-03-30T15:20:19.891893", } @@ -144,8 +146,8 @@ func TestGenLabelsSizes(t *testing.T) { } } - // should have 7 labels - assert.Equal(t, len(labelKeys), 7) + // should have 10 labels + assert.Equal(t, len(labelKeys), 10) }) t.Run("exhaustive labels", func(t *testing.T) { @@ -156,7 +158,9 @@ func TestGenLabelsSizes(t *testing.T) { CounterUnit: "GB", CounterVolume: 2, UserID: "user_id", + UserName: "user_name", ProjectID: "db3fce7b7aeb4109bb2794f9337e68fa", + ProjectName: "test_project", ResourceID: "ed8102c3-923a-4f5a-9a24-d59afc174755", Timestamp: "2021-03-30T15:20:19.891893", ResourceMetadata: ceilometer.Metadata{ @@ -170,8 +174,8 @@ func TestGenLabelsSizes(t *testing.T) { assert.Equal(t, len(labelKeys), len(labelVals)) fmt.Println(labelKeys) - // should have 8 labels - assert.Equal(t, len(labelKeys), 8) + // should have 11 labels + assert.Equal(t, len(labelKeys), 11) }) diff --git a/plugins/handler/ceilometer-metrics/messages/metric-tests.json b/plugins/handler/ceilometer-metrics/messages/metric-tests.json index 0bdc0bcd..add850bb 100644 --- a/plugins/handler/ceilometer-metrics/messages/metric-tests.json +++ b/plugins/handler/ceilometer-metrics/messages/metric-tests.json @@ -4,7 +4,7 @@ "testInput": { "request": { "oslo.version": "2.0", - "oslo.message": "{\"message_id\": \"37f64423-db31-4cfb-8c9d-06f9c0fad04a\", \"publisher_id\": \"telemetry.publisher.controller-0.redhat.local\", \"event_type\": \"metering\", \"priority\": \"SAMPLE\", \"payload\": [{\"source\": \"openstack\", \"counter_name\": \"disk.ephemeral.size\", \"counter_type\": \"gauge\", \"counter_unit\": \"GB\", \"counter_volume\": 0, \"user_id\": \"3ee72fcd74aa4439bb07fa69f1bc7169\", \"project_id\": \"e56191ef77744c599dbcecae6af176bb\", \"resource_id\": \"d8bd99b6-6fd8-4c02-a2e3-efbf596df636\", \"timestamp\": \"2020-09-14T16:12:49.939250+00:00\", \"resource_metadata\": {\"host\": \"compute-0.redhat.local\", \"flavor_id\": \"71cd0af1-afd3-4ee4-b918-cec05bf89578\", \"flavor_name\": \"m1.tiny\", \"display_name\": \"new-instance\", \"image_ref\": \"45333e02-643d-4f4f-a817-065060753983\", \"launched_at\": \"2020-09-14T16:12:49.839122\", \"created_at\": \"2020-09-14 16:12:39+00:00\"}, \"message_id\": \"22a54880-f6a5-11ea-b0f2-525400971e97\", \"monotonic_time\": null, \"message_signature\": \"be55d63bd5d876a62ab52824104128eedfa0619386e8569e326ccef4dcf0d9db\"}, {\"source\": \"openstack\", \"counter_name\": \"disk.root.size\", \"counter_type\": \"gauge\", \"counter_unit\": \"GB\", \"counter_volume\": 1, \"user_id\": \"3ee72fcd74aa4439bb07fa69f1bc7169\", \"project_id\": \"e56191ef77744c599dbcecae6af176bb\", \"resource_id\": \"d8bd99b6-6fd8-4c02-a2e3-efbf596df636\", \"timestamp\": \"2020-09-14T16:12:49.939250+00:00\", \"resource_metadata\": {\"host\": \"compute-0.redhat.local\", \"flavor_id\": \"71cd0af1-afd3-4ee4-b918-cec05bf89578\", \"flavor_name\": \"m1.tiny\", \"display_name\": \"new-instance\", \"image_ref\": \"45333e02-643d-4f4f-a817-065060753983\", \"launched_at\": \"2020-09-14T16:12:49.839122\", \"created_at\": \"2020-09-14 16:12:39+00:00\"}, \"message_id\": \"22a55c80-f6a5-11ea-b0f2-525400971e97\", \"monotonic_time\": null, \"message_signature\": \"bc0b987d71fe9f0d5d902347f22a0a20b2d975344b4c948572cae4dae553e960\"}, {\"source\": \"openstack\", \"counter_name\": \"compute.instance.booting.time\", \"counter_type\": \"gauge\", \"counter_unit\": \"sec\", \"counter_volume\": 10.839122, \"user_id\": null, \"project_id\": \"e56191ef77744c599dbcecae6af176bb\", \"resource_id\": \"d8bd99b6-6fd8-4c02-a2e3-efbf596df636\", \"timestamp\": \"2020-09-14T16:12:49.939250+00:00\", \"resource_metadata\": {\"host\": \"compute-0.redhat.local\", \"flavor_id\": \"71cd0af1-afd3-4ee4-b918-cec05bf89578\", \"flavor_name\": \"m1.tiny\", \"display_name\": \"new-instance\", \"image_ref\": \"45333e02-643d-4f4f-a817-065060753983\", \"launched_at\": \"2020-09-14T16:12:49.839122\", \"created_at\": \"2020-09-14 16:12:39+00:00\"}, \"message_id\": \"22a574d6-f6a5-11ea-b0f2-525400971e97\", \"monotonic_time\": null, \"message_signature\": \"fd7f1e2fdb34b7beb836d0ead178289f7c36f39bcd68acfd0719848667c58a13\"}, {\"source\": \"openstack\", \"counter_name\": \"vcpus\", \"counter_type\": \"gauge\", \"counter_unit\": \"vcpu\", \"counter_volume\": 2, \"user_id\": \"3ee72fcd74aa4439bb07fa69f1bc7169\", \"project_id\": \"e56191ef77744c599dbcecae6af176bb\", \"resource_id\": \"d8bd99b6-6fd8-4c02-a2e3-efbf596df636\", \"timestamp\": \"2020-09-14T16:12:49.939250+00:00\", \"resource_metadata\": {\"host\": \"compute-0.redhat.local\", \"flavor_id\": \"71cd0af1-afd3-4ee4-b918-cec05bf89578\", \"flavor_name\": \"m1.tiny\", \"display_name\": \"new-instance\", \"image_ref\": \"45333e02-643d-4f4f-a817-065060753983\", \"launched_at\": \"2020-09-14T16:12:49.839122\", \"created_at\": \"2020-09-14 16:12:39+00:00\"}, \"message_id\": \"22a5821e-f6a5-11ea-b0f2-525400971e97\", \"monotonic_time\": null, \"message_signature\": \"d3f107c2ef6bb1b06e1a9975d1f1ff0bdc51432adff39403db2a1f6a9773b99d\"}, {\"source\": \"openstack\", \"counter_name\": \"memory\", \"counter_type\": \"gauge\", \"counter_unit\": \"MB\", \"counter_volume\": 512, \"user_id\": \"3ee72fcd74aa4439bb07fa69f1bc7169\", \"project_id\": \"e56191ef77744c599dbcecae6af176bb\", \"resource_id\": \"d8bd99b6-6fd8-4c02-a2e3-efbf596df636\", \"timestamp\": \"2020-09-14T16:12:49.939250+00:00\", \"resource_metadata\": {\"host\": \"compute-0.redhat.local\", \"flavor_id\": \"71cd0af1-afd3-4ee4-b918-cec05bf89578\", \"flavor_name\": \"m1.tiny\", \"display_name\": \"new-instance\", \"image_ref\": \"45333e02-643d-4f4f-a817-065060753983\", \"launched_at\": \"2020-09-14T16:12:49.839122\", \"created_at\": \"2020-09-14 16:12:39+00:00\"}, \"message_id\": \"22a591dc-f6a5-11ea-b0f2-525400971e97\", \"monotonic_time\": null, \"message_signature\": \"9dcf78e3cd43e7fcd66cda5cf33511150e79a086a634bd9d087bb567e4985980\"}], \"timestamp\": \"2020-09-14 16:12:49.954128\"}" + "oslo.message": "{\"message_id\": \"37f64423-db31-4cfb-8c9d-06f9c0fad04a\", \"publisher_id\": \"telemetry.publisher.controller-0.redhat.local\", \"event_type\": \"metering\", \"priority\": \"SAMPLE\", \"payload\": [{\"source\": \"openstack\", \"counter_name\": \"disk.ephemeral.size\", \"counter_type\": \"gauge\", \"counter_unit\": \"GB\", \"counter_volume\": 0, \"user_id\": \"3ee72fcd74aa4439bb07fa69f1bc7169\", \"project_id\": \"e56191ef77744c599dbcecae6af176bb\", \"user_name\": \"test_user\", \"project_name\": \"test_project\", \"resource_id\": \"d8bd99b6-6fd8-4c02-a2e3-efbf596df636\", \"timestamp\": \"2020-09-14T16:12:49.939250+00:00\", \"resource_metadata\": {\"host\": \"compute-0.redhat.local\", \"flavor_id\": \"71cd0af1-afd3-4ee4-b918-cec05bf89578\", \"flavor_name\": \"m1.tiny\", \"display_name\": \"new-instance\", \"image_ref\": \"45333e02-643d-4f4f-a817-065060753983\", \"launched_at\": \"2020-09-14T16:12:49.839122\", \"created_at\": \"2020-09-14 16:12:39+00:00\"}, \"message_id\": \"22a54880-f6a5-11ea-b0f2-525400971e97\", \"monotonic_time\": null, \"message_signature\": \"be55d63bd5d876a62ab52824104128eedfa0619386e8569e326ccef4dcf0d9db\"}, {\"source\": \"openstack\", \"counter_name\": \"disk.root.size\", \"counter_type\": \"gauge\", \"counter_unit\": \"GB\", \"counter_volume\": 1, \"user_id\": \"3ee72fcd74aa4439bb07fa69f1bc7169\", \"project_id\": \"e56191ef77744c599dbcecae6af176bb\", \"user_name\": \"test_user\", \"project_name\": \"test_project\", \"resource_id\": \"d8bd99b6-6fd8-4c02-a2e3-efbf596df636\", \"timestamp\": \"2020-09-14T16:12:49.939250+00:00\", \"resource_metadata\": {\"host\": \"compute-0.redhat.local\", \"flavor_id\": \"71cd0af1-afd3-4ee4-b918-cec05bf89578\", \"flavor_name\": \"m1.tiny\", \"display_name\": \"new-instance\", \"image_ref\": \"45333e02-643d-4f4f-a817-065060753983\", \"launched_at\": \"2020-09-14T16:12:49.839122\", \"created_at\": \"2020-09-14 16:12:39+00:00\"}, \"message_id\": \"22a55c80-f6a5-11ea-b0f2-525400971e97\", \"monotonic_time\": null, \"message_signature\": \"bc0b987d71fe9f0d5d902347f22a0a20b2d975344b4c948572cae4dae553e960\"}, {\"source\": \"openstack\", \"counter_name\": \"compute.instance.booting.time\", \"counter_type\": \"gauge\", \"counter_unit\": \"sec\", \"counter_volume\": 10.839122, \"user_id\": null, \"project_id\": \"e56191ef77744c599dbcecae6af176bb\", \"user_name\": null, \"project_name\": \"test_project\", \"resource_id\": \"d8bd99b6-6fd8-4c02-a2e3-efbf596df636\", \"timestamp\": \"2020-09-14T16:12:49.939250+00:00\", \"resource_metadata\": {\"host\": \"compute-0.redhat.local\", \"flavor_id\": \"71cd0af1-afd3-4ee4-b918-cec05bf89578\", \"flavor_name\": \"m1.tiny\", \"display_name\": \"new-instance\", \"image_ref\": \"45333e02-643d-4f4f-a817-065060753983\", \"launched_at\": \"2020-09-14T16:12:49.839122\", \"created_at\": \"2020-09-14 16:12:39+00:00\"}, \"message_id\": \"22a574d6-f6a5-11ea-b0f2-525400971e97\", \"monotonic_time\": null, \"message_signature\": \"fd7f1e2fdb34b7beb836d0ead178289f7c36f39bcd68acfd0719848667c58a13\"}, {\"source\": \"openstack\", \"counter_name\": \"vcpus\", \"counter_type\": \"gauge\", \"counter_unit\": \"vcpu\", \"counter_volume\": 2, \"user_id\": \"3ee72fcd74aa4439bb07fa69f1bc7169\", \"project_id\": \"e56191ef77744c599dbcecae6af176bb\", \"user_name\": \"test_user\", \"project_name\": \"test_project\", \"resource_id\": \"d8bd99b6-6fd8-4c02-a2e3-efbf596df636\", \"timestamp\": \"2020-09-14T16:12:49.939250+00:00\", \"resource_metadata\": {\"host\": \"compute-0.redhat.local\", \"flavor_id\": \"71cd0af1-afd3-4ee4-b918-cec05bf89578\", \"flavor_name\": \"m1.tiny\", \"display_name\": \"new-instance\", \"image_ref\": \"45333e02-643d-4f4f-a817-065060753983\", \"launched_at\": \"2020-09-14T16:12:49.839122\", \"created_at\": \"2020-09-14 16:12:39+00:00\"}, \"message_id\": \"22a5821e-f6a5-11ea-b0f2-525400971e97\", \"monotonic_time\": null, \"message_signature\": \"d3f107c2ef6bb1b06e1a9975d1f1ff0bdc51432adff39403db2a1f6a9773b99d\"}, {\"source\": \"openstack\", \"counter_name\": \"memory\", \"counter_type\": \"gauge\", \"counter_unit\": \"MB\", \"counter_volume\": 512, \"user_id\": \"3ee72fcd74aa4439bb07fa69f1bc7169\", \"project_id\": \"e56191ef77744c599dbcecae6af176bb\", \"user_name\": \"test_user\", \"project_name\": \"test_project\", \"resource_id\": \"d8bd99b6-6fd8-4c02-a2e3-efbf596df636\", \"timestamp\": \"2020-09-14T16:12:49.939250+00:00\", \"resource_metadata\": {\"host\": \"compute-0.redhat.local\", \"flavor_id\": \"71cd0af1-afd3-4ee4-b918-cec05bf89578\", \"flavor_name\": \"m1.tiny\", \"display_name\": \"new-instance\", \"image_ref\": \"45333e02-643d-4f4f-a817-065060753983\", \"launched_at\": \"2020-09-14T16:12:49.839122\", \"created_at\": \"2020-09-14 16:12:39+00:00\"}, \"message_id\": \"22a591dc-f6a5-11ea-b0f2-525400971e97\", \"monotonic_time\": null, \"message_signature\": \"9dcf78e3cd43e7fcd66cda5cf33511150e79a086a634bd9d087bb567e4985980\"}], \"timestamp\": \"2020-09-14 16:12:49.954128\"}" }, "context": {} }, @@ -17,6 +17,9 @@ "type", "counter", "project", + "project_name", + "user", + "user_name", "unit", "resource", "vm_instance" @@ -27,6 +30,9 @@ "ephemeral", "disk.ephemeral.size", "e56191ef77744c599dbcecae6af176bb", + "test_project", + "3ee72fcd74aa4439bb07fa69f1bc7169", + "test_user", "GB", "d8bd99b6-6fd8-4c02-a2e3-efbf596df636", "compute-0.redhat.local" @@ -44,6 +50,9 @@ "type", "counter", "project", + "project_name", + "user", + "user_name", "unit", "resource", "vm_instance" @@ -54,6 +63,9 @@ "root", "disk.root.size", "e56191ef77744c599dbcecae6af176bb", + "test_project", + "3ee72fcd74aa4439bb07fa69f1bc7169", + "test_user", "GB", "d8bd99b6-6fd8-4c02-a2e3-efbf596df636", "compute-0.redhat.local" @@ -71,6 +83,7 @@ "type", "counter", "project", + "project_name", "unit", "resource", "vm_instance" @@ -81,6 +94,7 @@ "instance", "compute.instance.booting.time", "e56191ef77744c599dbcecae6af176bb", + "test_project", "sec", "d8bd99b6-6fd8-4c02-a2e3-efbf596df636", "compute-0.redhat.local" @@ -98,6 +112,9 @@ "type", "counter", "project", + "project_name", + "user", + "user_name", "unit", "resource", "vm_instance" @@ -108,6 +125,9 @@ "vcpus", "vcpus", "e56191ef77744c599dbcecae6af176bb", + "test_project", + "3ee72fcd74aa4439bb07fa69f1bc7169", + "test_user", "vcpu", "d8bd99b6-6fd8-4c02-a2e3-efbf596df636", "compute-0.redhat.local" @@ -125,6 +145,9 @@ "type", "counter", "project", + "project_name", + "user", + "user_name", "unit", "resource", "vm_instance" @@ -135,6 +158,9 @@ "memory", "memory", "e56191ef77744c599dbcecae6af176bb", + "test_project", + "3ee72fcd74aa4439bb07fa69f1bc7169", + "test_user", "MB", "d8bd99b6-6fd8-4c02-a2e3-efbf596df636", "compute-0.redhat.local" diff --git a/plugins/handler/ceilometer-metrics/pkg/ceilometer/ceilometer.go b/plugins/handler/ceilometer-metrics/pkg/ceilometer/ceilometer.go index 4d76b2ba..c5c1e240 100644 --- a/plugins/handler/ceilometer-metrics/pkg/ceilometer/ceilometer.go +++ b/plugins/handler/ceilometer-metrics/pkg/ceilometer/ceilometer.go @@ -28,7 +28,9 @@ type Metric struct { 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"`