You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The MetricFactory#createMetricSearch method explicitly calls out being able to pass in an object where the values are undefined for the dimensionsMap argument so that searches are expanded to "any value" for that dimension. However the call signature uses the DimensionsMap type from aws-cloudwatch which marks all values as required forcing users to type their values as undefined as unknown as string. This is cumbersome and not quite clear for users unfamiliar with this intended behavior.
The workaround is even documented in the doc comment for the parameter itself:
* @param dimensionsMap dimensions, further narrowing the search results; use `undefined` if you want to represent "any value" (in TS: `undefined as any as string`)
More specifically, I have a use case where I compose my queries using an object that has optional values that I perform some transformations on, but in order to appease the typings I must typecast all undefineds when I do so.
Example
constdimensions: cw.DimensionsMap=Object.fromEntries(Object.entries(props.dimensions??{}).map(([name,value])=>[name,value?.equals||undefinedasunknownasstring,// <- weird that I have to cast this, but I must use `undefined` so the values get filtered out in the query]),);factory.createMetricSearch(`MetricName=SomeMetric`,dimensions,MetricStatistic.AVERAGE);
I think instead of using the cloudwatch DimensionsMap this library can expose a new type like OptionalDimensionsMap, that can be used for the createMetricSearch and possibly createMetric methods. Something along the lines of:
// interface and explicit undefined should be jsii compatibleinterfaceOptionalDimensionsMap{[key: string]: string|undefined}
I'd be happy to submit a PR if this is something you'd like to add.
The text was updated successfully, but these errors were encountered:
Feature scope
MetricFactory
Describe your suggested feature
The MetricFactory#createMetricSearch method explicitly calls out being able to pass in an object where the values are
undefined
for thedimensionsMap
argument so that searches are expanded to "any value" for that dimension. However the call signature uses theDimensionsMap
type fromaws-cloudwatch
which marks all values asrequired
forcing users to type their values asundefined as unknown as string
. This is cumbersome and not quite clear for users unfamiliar with this intended behavior.The workaround is even documented in the doc comment for the parameter itself:
cdk-monitoring-constructs/lib/common/metric/MetricFactory.ts
Line 131 in 1f8ca08
More specifically, I have a use case where I compose my queries using an object that has optional values that I perform some transformations on, but in order to appease the typings I must typecast all
undefined
s when I do so.Example
Ideal
Proposal
I think instead of using the cloudwatch
DimensionsMap
this library can expose a new type likeOptionalDimensionsMap
, that can be used for thecreateMetricSearch
and possiblycreateMetric
methods. Something along the lines of:I'd be happy to submit a PR if this is something you'd like to add.
The text was updated successfully, but these errors were encountered: