Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/logging_v2/metric.py: 39%

51 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-08 06:45 +0000

1# Copyright 2016 Google LLC 

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 

15"""Define Cloud Logging API Metrics.""" 

16 

17from google.cloud.exceptions import NotFound 

18 

19 

20class Metric(object): 

21 """Metrics represent named filters for log entries. 

22 

23 See 

24 https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics 

25 """ 

26 

27 def __init__(self, name, *, filter_=None, client=None, description=""): 

28 """ 

29 Args: 

30 name (str): The name of the metric. 

31 filter_ (str): the advanced logs filter expression defining the entries 

32 tracked by the metric. If not passed, the instance should 

33 already exist, to be refreshed via :meth:`reload`. 

34 client (Optional[~logging_v2.client.Client]): A client which holds 

35 credentials and project configuration for the sink (which requires a project). 

36 description (Optional[str]): An optional description of the metric. 

37 

38 """ 

39 self.name = name 

40 self._client = client 

41 self.filter_ = filter_ 

42 self.description = description 

43 

44 @property 

45 def client(self): 

46 """Clent bound to the logger.""" 

47 return self._client 

48 

49 @property 

50 def project(self): 

51 """Project bound to the logger.""" 

52 return self._client.project 

53 

54 @property 

55 def full_name(self): 

56 """Fully-qualified name used in metric APIs""" 

57 return f"projects/{self.project}/metrics/{self.name}" 

58 

59 @property 

60 def path(self): 

61 """URL path for the metric's APIs""" 

62 return f"/{self.full_name}" 

63 

64 @classmethod 

65 def from_api_repr(cls, resource, client): 

66 """Construct a metric given its API representation 

67 

68 Args: 

69 resource (dict): metric resource representation returned from the API 

70 client (~logging_v2.client.Client): Client which holds 

71 credentials and project configuration for the sink. 

72 

73 Returns: 

74 google.cloud.logging_v2.metric.Metric 

75 """ 

76 metric_name = resource["name"] 

77 filter_ = resource["filter"] 

78 description = resource.get("description", "") 

79 return cls(metric_name, filter_=filter_, client=client, description=description) 

80 

81 def _require_client(self, client): 

82 """Check client or verify over-ride. Also sets ``parent``. 

83 

84 Args: 

85 client (Union[None, ~logging_v2.client.Client]): 

86 The client to use. If not passed, falls back to the 

87 ``client`` stored on the current sink. 

88 

89 Returns: 

90 google.cloud.logging_v2.client.Client: The client passed in 

91 or the currently bound client. 

92 """ 

93 if client is None: 

94 client = self._client 

95 return client 

96 

97 def create(self, *, client=None): 

98 """Create the metric via a PUT request 

99 

100 See 

101 https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/create 

102 

103 Args: 

104 client (Optional[~logging_v2.client.Client]): 

105 The client to use. If not passed, falls back to the 

106 ``client`` stored on the current sink. 

107 """ 

108 client = self._require_client(client) 

109 client.metrics_api.metric_create( 

110 self.project, self.name, self.filter_, self.description 

111 ) 

112 

113 def exists(self, *, client=None): 

114 """Test for the existence of the metric via a GET request 

115 

116 See 

117 https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/get 

118 

119 Args: 

120 client (Optional[~logging_v2.client.Client]): 

121 The client to use. If not passed, falls back to the 

122 ``client`` stored on the current sink. 

123 

124 Returns: 

125 bool: Boolean indicating existence of the metric. 

126 """ 

127 client = self._require_client(client) 

128 

129 try: 

130 client.metrics_api.metric_get(self.project, self.name) 

131 except NotFound: 

132 return False 

133 else: 

134 return True 

135 

136 def reload(self, *, client=None): 

137 """API call: sync local metric configuration via a GET request 

138 

139 See 

140 https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/get 

141 

142 Args: 

143 client (Optional[~logging_v2.client.Client]): 

144 The client to use. If not passed, falls back to the 

145 ``client`` stored on the current sink. 

146 """ 

147 client = self._require_client(client) 

148 data = client.metrics_api.metric_get(self.project, self.name) 

149 self.description = data.get("description", "") 

150 self.filter_ = data["filter"] 

151 

152 def update(self, *, client=None): 

153 """API call: update metric configuration via a PUT request 

154 

155 See 

156 https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/update 

157 

158 Args: 

159 client (Optional[~logging_v2.client.Client]): 

160 The client to use. If not passed, falls back to the 

161 ``client`` stored on the current sink. 

162 """ 

163 client = self._require_client(client) 

164 client.metrics_api.metric_update( 

165 self.project, self.name, self.filter_, self.description 

166 ) 

167 

168 def delete(self, *, client=None): 

169 """API call: delete a metric via a DELETE request 

170 

171 See 

172 https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/delete 

173 

174 Args: 

175 client (Optional[~logging_v2.client.Client]): 

176 The client to use. If not passed, falls back to the 

177 ``client`` stored on the current sink. 

178 """ 

179 client = self._require_client(client) 

180 client.metrics_api.metric_delete(self.project, self.name)