Coverage for /builds/BuildGrid/buildgrid/buildgrid/server/job_metrics.py: 0.00%

35 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-06-11 15:37 +0000

1# Copyright (C) 2021 Bloomberg LP 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# <http://www.apache.org/licenses/LICENSE-2.0> 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15from typing import Dict, Optional 

16 

17from buildgrid._enums import LeaseState, OperationStage 

18from buildgrid.server.metrics_names import JOB_CHANGES_COUNTER_METRIC_NAME, LEASE_CHANGES_COUNTER_METRIC_NAME 

19from buildgrid.server.metrics_utils import publish_counter_metric 

20 

21 

22class JobMetrics: 

23 @staticmethod 

24 def publish_metrics_on_job_updates( 

25 initial_values_of_interest: Dict[str, OperationStage], changes: Dict[str, int], instance_name: Optional[str] 

26 ) -> None: 

27 if "stage" in changes: 

28 new_stage = OperationStage(changes["stage"]) 

29 old_stage = None 

30 if "stage" in initial_values_of_interest: 

31 old_stage = OperationStage(initial_values_of_interest["stage"]) 

32 

33 JobMetrics._publish_job_stage_transition_metric_nowait( 

34 new_stage=new_stage, old_stage=old_stage, instance_name=instance_name 

35 ) 

36 

37 @staticmethod 

38 def publish_metrics_on_lease_updates( 

39 initial_values_of_interest: Dict[str, int], changes: Dict[str, int], instance_name: Optional[str] 

40 ) -> None: 

41 if "state" in changes: 

42 new_state = LeaseState(changes["state"]) 

43 old_state = None 

44 if "state" in initial_values_of_interest: 

45 old_state = LeaseState(initial_values_of_interest["state"]) 

46 

47 JobMetrics._publish_lease_state_transition_metric_nowait( 

48 new_state=new_state, old_state=old_state, instance_name=instance_name 

49 ) 

50 

51 @staticmethod 

52 def _publish_job_stage_transition_metric_nowait( 

53 new_stage: OperationStage, old_stage: Optional[OperationStage] = None, instance_name: Optional[str] = None 

54 ) -> None: 

55 if not instance_name: 

56 instance_name = "" 

57 # Publish record for new stage 

58 publish_counter_metric( 

59 JOB_CHANGES_COUNTER_METRIC_NAME, 

60 1, 

61 {"instance-name": instance_name, "operation-stage": new_stage.name, "statsd-bucket": new_stage.name}, 

62 ) 

63 # If we're given prior stage, indicate that transition too 

64 if old_stage and old_stage != new_stage: 

65 publish_counter_metric( 

66 JOB_CHANGES_COUNTER_METRIC_NAME, 

67 -1, 

68 {"instance-name": instance_name, "operation-stage": old_stage.name, "statsd-bucket": old_stage.name}, 

69 ) 

70 

71 @staticmethod 

72 def _publish_lease_state_transition_metric_nowait( 

73 new_state: LeaseState, old_state: Optional[LeaseState] = None, instance_name: Optional[str] = None 

74 ) -> None: 

75 if not instance_name: 

76 instance_name = "" 

77 # Publish record for new state 

78 publish_counter_metric( 

79 LEASE_CHANGES_COUNTER_METRIC_NAME, 

80 1, 

81 {"instance-name": instance_name, "operation-stage": new_state.name, "statsd-bucket": new_state.name}, 

82 ) 

83 # If we're given prior state, indicate that transition too 

84 if old_state and old_state != new_state: 

85 publish_counter_metric( 

86 LEASE_CHANGES_COUNTER_METRIC_NAME, 

87 -1, 

88 {"instance-name": instance_name, "operation-stage": old_state.name, "statsd-bucket": old_state.name}, 

89 )