Skip to content

Commit a56c0e5

Browse files
jatakiajanvi12Janvi Jatakia (from Dev Box)
and
Janvi Jatakia (from Dev Box)
authored
Telemetry optimization - Add K8sAPI and CAdvisorAPI response code telemetry in customMetrics (#1122)
* Add kubernetes response code telemetry * Adding resource to the telemetry and resolving other comments * Adding Cadvisor API status to the PR as well * Separate maps for cadvisor and k8s * Send only failure codes --------- Co-authored-by: Janvi Jatakia (from Dev Box) <[email protected]>
1 parent 7025725 commit a56c0e5

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

source/plugins/ruby/ApplicationInsightsUtility.rb

+42-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class ApplicationInsightsUtility
3939
end
4040

4141
@@controllerType = {"daemonset" => "DS", "replicaset" => "RS"}
42-
4342
def initialize
4443
end
4544

@@ -323,5 +322,47 @@ def getWorkspaceCloud()
323322
$log.warn("Exception in AppInsightsUtility: getWorkspaceCloud - error: #{errorStr}")
324323
end
325324
end
325+
326+
def sendAPIResponseTelemetry(responseCode, resource, metricName, apiResponseCodeHash, apiResponseTelemetryTimeTracker)
327+
begin
328+
if (!responseCode.nil? && !responseCode.empty?)
329+
if (!apiResponseCodeHash.has_key?(responseCode))
330+
telemetryProps = {}
331+
telemetryProps[resource] = 1
332+
apiResponseCodeHash[responseCode] = telemetryProps
333+
else
334+
telemetryProps = apiResponseCodeHash[responseCode]
335+
if (telemetryProps.nil?)
336+
telemetryProps = {}
337+
end
338+
if (!telemetryProps.has_key?(resource))
339+
telemetryProps[resource] = 1
340+
else
341+
telemetryProps[resource] += 1
342+
end
343+
apiResponseCodeHash[responseCode] = telemetryProps
344+
end
345+
346+
timeDifference = (DateTime.now.to_time.to_i - apiResponseTelemetryTimeTracker).abs
347+
timeDifferenceInMinutes = timeDifference / 60
348+
if (timeDifferenceInMinutes >= Constants::TELEMETRY_FLUSH_INTERVAL_IN_MINUTES)
349+
apiResponseTelemetryTimeTracker = DateTime.now.to_time.to_i
350+
apiResponseCodeHash.each do |key, value|
351+
value.each do |resourceName, count|
352+
telemetryProps = {}
353+
telemetryProps["Resource"] = resourceName
354+
telemetryProps["ResponseCode"] = key
355+
sendMetricTelemetry(metricName, count, telemetryProps)
356+
end
357+
end
358+
apiResponseCodeHash.clear
359+
end
360+
end
361+
return apiResponseTelemetryTimeTracker
362+
rescue => err
363+
$log.warn("Exception in AppInsightsUtility: sendAPIResponseTelemetry failed with an error: #{err}")
364+
end
365+
end
366+
326367
end
327368
end

source/plugins/ruby/CAdvisorMetricsAPIClient.rb

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class CAdvisorMetricsAPIClient
3939
@npmIntegrationAdvanced = ENV["TELEMETRY_NPM_INTEGRATION_METRICS_ADVANCED"]
4040
@subnetIpUsageMetrics = ENV["TELEMETRY_SUBNET_IP_USAGE_INTEGRATION_METRICS"]
4141

42+
@@CAdvisorApiResponseCodeHash = {}
43+
@@CAdvisorApiResponseTelemetryTimeTracker = DateTime.now.to_time.to_i
44+
45+
4246
@os_type = ENV["OS_TYPE"]
4347
if !@os_type.nil? && !@os_type.empty? && @os_type.strip.casecmp("windows") == 0
4448
@LogPath = Constants::WINDOWS_LOG_PATH + "kubernetes_perf_log.txt"
@@ -963,6 +967,10 @@ def getResponse(winNode, relativeUri)
963967
@Log.info "Got response code #{response.code} from #{uri.request_uri}"
964968
end
965969
end
970+
# Send telemetry for cAdvisor API response code if it is not success
971+
if !response.nil? && !response.code.nil? && !response.code.start_with?("2")
972+
@@CAdvisorApiResponseTelemetryTimeTracker = ApplicationInsightsUtility.sendAPIResponseTelemetry(response.code, relativeUri, "CAdvisorAPIStatus", @@CAdvisorApiResponseCodeHash, @@CAdvisorApiResponseTelemetryTimeTracker)
973+
end
966974
end
967975
rescue => error
968976
@Log.warn("CAdvisor api request for #{cAdvisorUri} failed: #{error}")

source/plugins/ruby/KubernetesApiClient.rb

+13
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class KubernetesApiClient
3030
#@@IsValidRunningNode = nil
3131
#@@IsLinuxCluster = nil
3232
@@KubeSystemNamespace = "kube-system"
33+
@@K8sApiResponseCodeHash = {}
34+
@@K8sApiResponseTelemetryTimeTracker = DateTime.now.to_time.to_i
35+
3336

3437
@os_type = ENV["OS_TYPE"]
3538
if !@os_type.nil? && !@os_type.empty? && @os_type.strip.casecmp("windows") == 0
@@ -70,6 +73,11 @@ def getKubeResourceInfo(resource, api_group: nil)
7073
@Log.info "KubernetesAPIClient::getKubeResourceInfo : Making request to #{uri.request_uri} @ #{Time.now.utc.iso8601}"
7174
response = http.request(kubeApiRequest)
7275
@Log.info "KubernetesAPIClient::getKubeResourceInfo : Got response of #{response.code} for #{uri.request_uri} @ #{Time.now.utc.iso8601}"
76+
77+
# Send telemetry for response code if not success
78+
if !response.nil? && !response.code.nil? && !response.code.start_with?("2")
79+
@@K8sApiResponseTelemetryTimeTracker = ApplicationInsightsUtility.sendAPIResponseTelemetry(response.code, resource, "K8sAPIStatus", @@K8sApiResponseCodeHash, @@K8sApiResponseTelemetryTimeTracker)
80+
end
7381
end
7482
end
7583
end
@@ -104,6 +112,11 @@ def getKubeResourceInfoV2(resource, api_group: nil)
104112
response = http.request(kubeApiRequest)
105113
responseCode = response.code
106114
@Log.info "KubernetesAPIClient::getKubeResourceInfoV2 : Got response of #{response.code} for #{uri.request_uri} @ #{Time.now.utc.iso8601}"
115+
116+
# Send telemetry for response code if not success
117+
if !response.nil? && !response.code.nil? && !response.code.start_with?("2")
118+
@@K8sApiResponseTelemetryTimeTracker = ApplicationInsightsUtility.sendAPIResponseTelemetry(response.code, resource, "K8sAPIStatus", @@K8sApiResponseCodeHash, @@K8sApiResponseTelemetryTimeTracker)
119+
end
107120
end
108121
end
109122
end

0 commit comments

Comments
 (0)