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

35 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-06-22 21:04 +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 Optional, Dict 

16 

17from buildgrid._enums import LeaseState, OperationStage 

18from buildgrid.server.metrics_utils import publish_counter_metric 

19from buildgrid.server.metrics_names import (JOB_CHANGES_COUNTER_METRIC_NAME, 

20 LEASE_CHANGES_COUNTER_METRIC_NAME) 

21 

22 

23class JobMetrics(): 

24 @staticmethod 

25 def publish_metrics_on_job_updates(initial_values_of_interest: Dict, 

26 changes: Dict, 

27 instance_name: Optional[str]): 

28 if "stage" in changes: 

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

30 old_stage = None 

31 if "stage" in initial_values_of_interest: 

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

33 

34 JobMetrics._publish_job_stage_transition_metric_nowait( 

35 new_stage=new_stage, 

36 old_stage=old_stage, 

37 instance_name=instance_name) 

38 

39 @staticmethod 

40 def publish_metrics_on_lease_updates(initial_values_of_interest: Dict, 

41 changes: Dict, 

42 instance_name: Optional[str]): 

43 if "state" in changes: 

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

45 old_state = None 

46 if "state" in initial_values_of_interest: 

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

48 

49 JobMetrics._publish_lease_state_transition_metric_nowait( 

50 new_state=new_state, 

51 old_state=old_state, 

52 instance_name=instance_name) 

53 

54 @staticmethod 

55 def _publish_job_stage_transition_metric_nowait( 

56 new_stage: OperationStage, 

57 old_stage: Optional[OperationStage] = None, 

58 instance_name: Optional[str] = None): 

59 if not instance_name: 

60 instance_name = "" 

61 # Publish record for new stage 

62 publish_counter_metric( 

63 JOB_CHANGES_COUNTER_METRIC_NAME, 1, { 

64 "instance-name": instance_name, 

65 "operation-stage": new_stage.name, 

66 "statsd-bucket": new_stage.name 

67 }) 

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

69 if old_stage and old_stage != new_stage: 

70 publish_counter_metric( 

71 JOB_CHANGES_COUNTER_METRIC_NAME, -1, { 

72 "instance-name": instance_name, 

73 "operation-stage": old_stage.name, 

74 "statsd-bucket": old_stage.name 

75 }) 

76 

77 @staticmethod 

78 def _publish_lease_state_transition_metric_nowait( 

79 new_state: LeaseState, 

80 old_state: Optional[LeaseState] = None, 

81 instance_name: Optional[str] = None): 

82 if not instance_name: 

83 instance_name = "" 

84 # Publish record for new state 

85 publish_counter_metric( 

86 LEASE_CHANGES_COUNTER_METRIC_NAME, 1, { 

87 "instance-name": instance_name, 

88 "operation-stage": new_state.name, 

89 "statsd-bucket": new_state.name 

90 }) 

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

92 if old_state and old_state != new_state: 

93 publish_counter_metric( 

94 LEASE_CHANGES_COUNTER_METRIC_NAME, -1, { 

95 "instance-name": instance_name, 

96 "operation-stage": old_state.name, 

97 "statsd-bucket": old_state.name 

98 })