|
| 1 | +import logging |
| 2 | +import os |
| 3 | + |
| 4 | +from codeguru_profiler_agent.utils.synchronization import synchronized |
| 5 | +from codeguru_profiler_agent.utils.time import to_iso |
| 6 | + |
| 7 | +logger = logging.getLogger(__name__) |
| 8 | + |
| 9 | + |
| 10 | +class ErrorsMetadata: |
| 11 | + def __init__(self): |
| 12 | + self.reset() |
| 13 | + |
| 14 | + def reset(self): |
| 15 | + """ |
| 16 | + We want to differentiate API call errors more granularly. We want to gather ResourceNotFoundException errors |
| 17 | + because we are going to get this exception with auto-create feature and want to monitor how many times |
| 18 | + the agent is not able to create the PG and resulting in subsequent ResourceNotFoundException. |
| 19 | + """ |
| 20 | + self.errors_count = 0 |
| 21 | + self.sdk_client_errors = 0 |
| 22 | + self.configure_agent_errors = 0 |
| 23 | + self.configure_agent_rnfe_auto_create_enabled_errors = 0 |
| 24 | + self.create_profiling_group_errors = 0 |
| 25 | + self.post_agent_profile_errors = 0 |
| 26 | + self.post_agent_profile_rnfe_auto_create_enabled_errors = 0 |
| 27 | + |
| 28 | + def serialize_to_json(self): |
| 29 | + """ |
| 30 | + This needs to be compliant with errors count schema. |
| 31 | + """ |
| 32 | + return { |
| 33 | + "errorsCount": self.errors_count, |
| 34 | + "sdkClientErrors": self.sdk_client_errors, |
| 35 | + "configureAgentErrors": self.configure_agent_errors, |
| 36 | + "configureAgentRnfeAutoCreateEnabledErrors": self.configure_agent_rnfe_auto_create_enabled_errors, |
| 37 | + "createProfilingGroupErrors": self.create_profiling_group_errors, |
| 38 | + "postAgentProfileErrors": self.post_agent_profile_errors, |
| 39 | + "postAgentProfileRnfeAutoCreateEnabledErrors": self.post_agent_profile_rnfe_auto_create_enabled_errors |
| 40 | + } |
| 41 | + |
| 42 | + @synchronized |
| 43 | + def increment_sdk_error(self, error_type): |
| 44 | + """ |
| 45 | + ErrorsCount is the umbrella of all the kinds of error we want to capture. Currently we have only SdkClientErrors |
| 46 | + in it. SdkClientErrors is comprised of different API level errors like ConfigureAgentErrors, |
| 47 | + PostAgentProfileErrors, CreateProfilingGroupErrors. |
| 48 | + :param error_type: The type of API level error that we want to capture. |
| 49 | + """ |
| 50 | + self.errors_count += 1 |
| 51 | + self.sdk_client_errors += 1 |
| 52 | + |
| 53 | + """ |
| 54 | + Special handling for ResourceNotFoundException errors. |
| 55 | + For example configureAgentRnfeAutoCreateEnabledErrors is also a configureAgentErrors. |
| 56 | + """ |
| 57 | + if error_type == "configureAgentErrors": |
| 58 | + self.configure_agent_errors += 1 |
| 59 | + elif error_type == "configureAgentRnfeAutoCreateEnabledErrors": |
| 60 | + self.configure_agent_errors += 1 |
| 61 | + self.configure_agent_rnfe_auto_create_enabled_errors += 1 |
| 62 | + elif error_type == "createProfilingGroupErrors": |
| 63 | + self.create_profiling_group_errors += 1 |
| 64 | + elif error_type == "postAgentProfileErrors": |
| 65 | + self.post_agent_profile_errors += 1 |
| 66 | + elif error_type == "postAgentProfileRnfeAutoCreateEnabledErrors": |
| 67 | + self.post_agent_profile_errors += 1 |
| 68 | + self.post_agent_profile_rnfe_auto_create_enabled_errors += 1 |
| 69 | + |
| 70 | + def record_sdk_error(self, error_type): |
| 71 | + self.increment_sdk_error(error_type) |
| 72 | + |
| 73 | + |
| 74 | +class AgentDebugInfo: |
| 75 | + def __init__(self, errors_metadata=None, agent_start_time=None, timer=None): |
| 76 | + self.process_id = get_process_id() |
| 77 | + self.errors_metadata = errors_metadata |
| 78 | + self.agent_start_time = agent_start_time |
| 79 | + self.timer = timer |
| 80 | + |
| 81 | + def serialize_to_json(self): |
| 82 | + """ |
| 83 | + This needs to be compliant with agent debug info schema. |
| 84 | + """ |
| 85 | + json = {} |
| 86 | + |
| 87 | + self.add_agent_start_time(json) |
| 88 | + self.add_process_id(json) |
| 89 | + self.add_errors_metadata(json) |
| 90 | + self.add_generic_metrics(json) |
| 91 | + |
| 92 | + return json |
| 93 | + |
| 94 | + def add_agent_start_time(self, json): |
| 95 | + if self.agent_start_time is not None: |
| 96 | + json["agentStartTime"] = to_iso(self.agent_start_time) |
| 97 | + |
| 98 | + def add_errors_metadata(self, json): |
| 99 | + if self.errors_metadata is not None: |
| 100 | + json["errorsCount"] = self.errors_metadata.serialize_to_json() |
| 101 | + |
| 102 | + def add_process_id(self, json): |
| 103 | + if self.process_id is not None: |
| 104 | + json["processId"] = self.process_id |
| 105 | + |
| 106 | + def add_generic_metrics(self, json): |
| 107 | + if self.timer is not None and self.timer.metrics: |
| 108 | + generic_metrics = {} |
| 109 | + |
| 110 | + for metric, metric_value in self.timer.metrics.items(): |
| 111 | + generic_metrics[metric + "_timings_max"] = metric_value.max |
| 112 | + generic_metrics[metric + "_timings_average"] = metric_value.average() |
| 113 | + |
| 114 | + if generic_metrics: |
| 115 | + json["genericMetrics"] = generic_metrics |
| 116 | + |
| 117 | + |
| 118 | +def get_process_id(): |
| 119 | + try: |
| 120 | + return os.getpid() |
| 121 | + except Exception as e: |
| 122 | + logger.info("Failed to get the process id", exc_info=True) |
| 123 | + return None |
| 124 | + |
0 commit comments