From b390e19a7dcfce99ceb055b1b8b81a95ab1568ad Mon Sep 17 00:00:00 2001 From: Danail Branekov Date: Wed, 21 Apr 2021 17:37:27 +0300 Subject: [PATCH] Map alphabetical instance_id to numeric one When using deployments in Eirini for workloads, the metrics proxy would send the pod name suffix (which is not a number) as instance id. In order to convert it to a number, we naively unique and sort those instance ids and map them to their order. [cloudfoundry-incubator/eirini-release#207] Co-authored-by: Mario Nitchev --- lib/logcache/traffic_controller_decorator.rb | 22 +++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/logcache/traffic_controller_decorator.rb b/lib/logcache/traffic_controller_decorator.rb index 1f0212ba7de..497ea3480c8 100644 --- a/lib/logcache/traffic_controller_decorator.rb +++ b/lib/logcache/traffic_controller_decorator.rb @@ -35,15 +35,22 @@ def container_metrics(auth_token: nil, source_guid:, logcache_filter:) end end + instance_id_to_index = Hash[final_envelopes. + uniq { |e| e.instance_id }. + sort_by { |e| e.instance_id }. + each_with_index. + map { |e, i| [e.instance_id, i] } + ] + final_envelopes. select { |e| has_container_metrics_fields?(e) && logcache_filter.call(e) }. # workaround metric-proxy sending non-numeric instance_id in disk usage metric, see # https://github.com/cloudfoundry/metric-proxy/blob/10ea8430e142910ef949f1f425f2d9eda10b950c/pkg/metrics/proxy.go#L176 # uniq { |e| e.gauge.metrics.keys << e.instance_id }. - uniq { |e| e.gauge.metrics.keys << '0' }. - sort_by(&:instance_id). - chunk(&:instance_id). - map { |envelopes_by_instance| convert_to_traffic_controller_envelope(source_guid, envelopes_by_instance) } + uniq { |e | e.gauge.metrics.keys << instance_id_to_index[e.instance_id] }. + sort_by{ |e| e.instance_id }. + chunk{ |e| e.instance_id }. + map { |envelopes_by_instance| convert_to_traffic_controller_envelope(source_guid, envelopes_by_instance, instance_id_to_index[envelopes_by_instance.first]) } end private @@ -71,17 +78,18 @@ def has_container_metrics_fields?(envelope) # rubocop:enable Style/PreferredHashMethods end - def convert_to_traffic_controller_envelope(source_guid, envelopes_by_instance) + def convert_to_traffic_controller_envelope(source_guid, envelopes_by_instance, index) tc_envelope = TrafficController::Models::Envelope.new( containerMetric: TrafficController::Models::ContainerMetric.new({ applicationId: source_guid, - instanceIndex: envelopes_by_instance.first, + # instanceIndex: envelopes_by_instance.first, + instanceIndex: index, }), ) tags = {} envelopes_by_instance.second.each { |e| - tc_envelope.containerMetric.instanceIndex = e.instance_id + tc_envelope.containerMetric.instanceIndex = index # rubocop seems to think that there is a 'key?' method # on envelope.gauge.metrics - but it does not # rubocop:disable Style/PreferredHashMethods