enumerate

in source/plugins/ruby/in_kubestate_hpa.rb [74:210]


    def enumerate
      begin
        hpaList = nil
        currentTime = Time.now
        batchTime = currentTime.utc.iso8601

        @hpaCount = 0

        if ExtensionUtils.isAADMSIAuthMode()
          $log.info("in_kubestate_hpa::enumerate: AAD AUTH MSI MODE")
          if @tag.nil? || !@tag.start_with?(Constants::EXTENSION_OUTPUT_STREAM_ID_TAG_PREFIX)
            @tag = ExtensionUtils.getOutputStreamId(Constants::INSIGHTS_METRICS_DATA_TYPE)
          end
            $log.info("in_kubestate_hpa::enumerate: using tag -#{@tag} @ #{Time.now.utc.iso8601}")
        end
        
        continuationToken = nil
        $log.info("in_kubestate_hpa::enumerate : Getting HPAs from Kube API @ #{Time.now.utc.iso8601}")
        continuationToken, hpaList = KubernetesApiClient.getResourcesAndContinuationToken("horizontalpodautoscalers?limit=#{@HPA_CHUNK_SIZE}", api_group: @HPA_API_GROUP)
        $log.info("in_kubestate_hpa::enumerate : Done getting HPAs from Kube API @ #{Time.now.utc.iso8601}")
        if (!hpaList.nil? && !hpaList.empty? && hpaList.key?("items") && !hpaList["items"].nil? && !hpaList["items"].empty?)
          parse_and_emit_records(hpaList, batchTime)
        else
          $log.warn "in_kubestate_hpa::enumerate:Received empty hpaList"
        end

        
        while (!continuationToken.nil? && !continuationToken.empty?)
          continuationToken, hpaList = KubernetesApiClient.getResourcesAndContinuationToken("horizontalpodautoscalers?limit=#{@HPA_CHUNK_SIZE}&continue=#{continuationToken}", api_group: @HPA_API_GROUP)
          if (!hpaList.nil? && !hpaList.empty? && hpaList.key?("items") && !hpaList["items"].nil? && !hpaList["items"].empty?)
            parse_and_emit_records(hpaList, batchTime)
          else
            $log.warn "in_kubestate_hpa::enumerate:Received empty hpaList"
          end
        end

        
        hpaList = nil

        
        if (@hpaCount > 0)
          
          $log.info("in_kubestate_hpa::hpaCount= #{hpaCount}")
          
        end
      rescue => errorStr
        $log.warn "in_kubestate_hpa::enumerate:Failed in enumerate: #{errorStr}"
        ApplicationInsightsUtility.sendExceptionTelemetry("in_kubestate_hpa::enumerate:Failed in enumerate: #{errorStr}")
      end
    end 

    def parse_and_emit_records(hpas, batchTime = Time.utc.iso8601)
      metricItems = []
      insightsMetricsEventStream = Fluent::MultiEventStream.new
      begin
        metricInfo = hpas
        metricInfo["items"].each do |hpa|
          hpaName = hpa["metadata"]["name"]
          hpaNameSpace = hpa["metadata"]["namespace"]
          hpaCreatedTime = ""
          if !hpa["metadata"]["creationTimestamp"].nil?
            hpaCreatedTime = hpa["metadata"]["creationTimestamp"]
          end
          hpaSpecMinReplicas = 1 
          if !hpa["spec"]["minReplicas"].nil?
            hpaSpecMinReplicas = hpa["spec"]["minReplicas"]
          end
          hpaSpecMaxReplicas = 0
          if !hpa["spec"]["maxReplicas"].nil?
            hpaSpecMaxReplicas = hpa["spec"]["maxReplicas"]
          end
          hpaSpecScaleTargetKind = ""
          hpaSpecScaleTargetName = ""
          if !hpa["spec"]["scaleTargetRef"].nil?
            if !hpa["spec"]["scaleTargetRef"]["kind"].nil?
              hpaSpecScaleTargetKind = hpa["spec"]["scaleTargetRef"]["kind"]
            end
            if !hpa["spec"]["scaleTargetRef"]["name"].nil?
              hpaSpecScaleTargetName = hpa["spec"]["scaleTargetRef"]["name"]
            end
          end
          hpaStatusCurrentReplicas = 0
          if !hpa["status"]["currentReplicas"].nil?
            hpaStatusCurrentReplicas = hpa["status"]["currentReplicas"]
          end
          hpaStatusDesiredReplicas = 0
          if !hpa["status"]["desiredReplicas"].nil?
            hpaStatusDesiredReplicas = hpa["status"]["desiredReplicas"]
          end

          hpaStatuslastScaleTime = ""
          if !hpa["status"]["lastScaleTime"].nil?
            hpaStatuslastScaleTime = hpa["status"]["lastScaleTime"]
          end

          metricItem = {}
          metricItem["CollectionTime"] = batchTime
          metricItem["Computer"] = @NodeName
          metricItem["Name"] = Constants::INSIGHTSMETRICS_METRIC_NAME_KUBE_STATE_HPA_STATE
          metricItem["Value"] = hpaStatusCurrentReplicas
          metricItem["Origin"] = Constants::INSIGHTSMETRICS_TAGS_ORIGIN
          metricItem["Namespace"] = Constants::INSIGHTSMETRICS_TAGS_KUBESTATE_NAMESPACE

          metricTags = {}
          metricTags[Constants::INSIGHTSMETRICS_TAGS_CLUSTERID] = @ClusterId
          metricTags[Constants::INSIGHTSMETRICS_TAGS_CLUSTERNAME] = @ClusterName
          metricTags[Constants::INSIGHTSMETRICS_TAGS_KUBE_STATE_HPA_NAME] = hpaName
          metricTags[Constants::INSIGHTSMETRICS_TAGS_K8SNAMESPACE] = hpaNameSpace
          metricTags[Constants::INSIGHTSMETRICS_TAGS_KUBE_STATE_CREATIONTIME] = hpaCreatedTime
          metricTags[Constants::INSIGHTSMETRICS_TAGS_KUBE_STATE_HPA_SPEC_MIN_REPLICAS] = hpaSpecMinReplicas
          metricTags[Constants::INSIGHTSMETRICS_TAGS_KUBE_STATE_HPA_SPEC_MAX_REPLICAS] = hpaSpecMaxReplicas
          metricTags[Constants::INSIGHTSMETRICS_TAGS_KUBE_STATE_HPA_SPEC_SCALE_TARGET_KIND] = hpaSpecScaleTargetKind
          metricTags[Constants::INSIGHTSMETRICS_TAGS_KUBE_STATE_HPA_SPEC_SCALE_TARGET_NAME] = hpaSpecScaleTargetName
          metricTags[Constants::INSIGHTSMETRICS_TAGS_KUBE_STATE_HPA_STATUS_DESIRED_REPLICAS] = hpaStatusDesiredReplicas
          metricTags[Constants::INSIGHTSMETRICS_TAGS_KUBE_STATE_HPA_STATUS_LAST_SCALE_TIME] = hpaStatuslastScaleTime

          metricItem["Tags"] = metricTags

          metricItems.push(metricItem)
        end

        time = Fluent::Engine.now
        metricItems.each do |insightsMetricsRecord|
          insightsMetricsEventStream.add(time, insightsMetricsRecord) if insightsMetricsRecord
        end

        router.emit_stream(@tag, insightsMetricsEventStream) if insightsMetricsEventStream
        $log.info("successfully emitted #{metricItems.length()} kube_state_hpa metrics")
        if (!@@istestvar.nil? && !@@istestvar.empty? && @@istestvar.casecmp("true") == 0 && insightsMetricsEventStream.count > 0)
          $log.info("kubestatehpaInsightsMetricsEmitStreamSuccess @ #{Time.now.utc.iso8601}")
        end
      rescue => error
        $log.warn("in_kubestate_hpa::parse_and_emit_records failed: #{error} ")
        ApplicationInsightsUtility.sendExceptionTelemetry("in_kubestate_hpa::parse_and_emit_records failed: #{error}")
      end
    end