9
9
10
10
from ..shared import constants
11
11
from ..shared .functions import resolve_env_var_choice
12
- from ..shared .lazy_import import LazyLoader
13
12
from .exceptions import MetricUnitError , MetricValueError , SchemaValidationError
14
- from .schema import CLOUDWATCH_EMF_SCHEMA
15
13
16
- fastjsonschema = LazyLoader ("fastjsonschema" , globals (), "fastjsonschema" )
17
14
logger = logging .getLogger (__name__ )
18
15
19
16
MAX_METRICS = 100
17
+ MAX_DIMENSIONS = 9
20
18
21
19
22
20
class MetricUnit (Enum ):
@@ -180,6 +178,12 @@ def serialize_metric_set(self, metrics: Dict = None, dimensions: Dict = None, me
180
178
if self .service and not self .dimension_set .get ("service" ):
181
179
self .dimension_set ["service" ] = self .service
182
180
181
+ if len (metrics ) == 0 :
182
+ raise SchemaValidationError ("Must contain at least one metric." )
183
+
184
+ if self .namespace is None :
185
+ raise SchemaValidationError ("Must contain a metric namespace." )
186
+
183
187
logger .debug ({"details" : "Serializing metrics" , "metrics" : metrics , "dimensions" : dimensions })
184
188
185
189
metric_names_and_units : List [Dict [str , str ]] = [] # [ { "Name": "metric_name", "Unit": "Count" } ]
@@ -209,12 +213,6 @@ def serialize_metric_set(self, metrics: Dict = None, dimensions: Dict = None, me
209
213
** metric_names_and_values , # "single_metric": 1.0
210
214
}
211
215
212
- try :
213
- logger .debug ("Validating serialized metrics against CloudWatch EMF schema" )
214
- fastjsonschema .validate (definition = CLOUDWATCH_EMF_SCHEMA , data = embedded_metrics_object )
215
- except fastjsonschema .JsonSchemaException as e :
216
- message = f"Invalid format. Error: { e .message } , Invalid item: { e .name } " # noqa: B306, E501
217
- raise SchemaValidationError (message )
218
216
return embedded_metrics_object
219
217
220
218
def add_dimension (self , name : str , value : str ):
@@ -234,7 +232,10 @@ def add_dimension(self, name: str, value: str):
234
232
Dimension value
235
233
"""
236
234
logger .debug (f"Adding dimension: { name } :{ value } " )
237
-
235
+ if len (self .dimension_set ) == 9 :
236
+ raise SchemaValidationError (
237
+ f"Maximum number of dimensions exceeded ({ MAX_DIMENSIONS } ): Unable to add dimension { name } ."
238
+ )
238
239
# Cast value to str according to EMF spec
239
240
# Majority of values are expected to be string already, so
240
241
# checking before casting improves performance in most cases
@@ -295,7 +296,7 @@ def __extract_metric_unit_value(self, unit: Union[str, MetricUnit]) -> str:
295
296
if unit in self ._metric_unit_options :
296
297
unit = MetricUnit [unit ].value
297
298
298
- if unit not in self ._metric_units : # str correta
299
+ if unit not in self ._metric_units :
299
300
raise MetricUnitError (
300
301
f"Invalid metric unit '{ unit } ', expected either option: { self ._metric_unit_options } "
301
302
)
0 commit comments