Repository navigation
feat(cloudwatch): serve CBOR, JSON and Query from one document (#785) - #791
Merged
Merged
Conversation
CloudWatch's service shape declares awsQuery, awsJson1_0, rpcv2Cbor and awsQueryCompatible at once, and its clients disagree about which to use. Substrate read query-form parameters and answered XML no matter what had been asked for, so aws-sdk-go-v2 failed every call with "deserialization failed, expected map for struct, got major type 1" and the AWS CLI printed nothing at all. Input is normalized rather than handlers rewritten: a JSON or CBOR body is flattened into the query protocol's spelling before dispatch, which is correct only because the model declares no xmlName and no xmlFlattened trait anywhere. Output moves the other way — a handler builds one ordered neutral document and three renderers consume it. The six smithy.api#Unit operations now answer 200 with no body and no Content-Type on the CBOR path, per the no_output protocol test's forbidHeaders. Two Query-path defects surfaced and are fixed with it: DescribeAlarmsForMetric borrowed DescribeAlarms' wrapper element names, and the two alarm-action operations emitted XML opening with a literal <placeholder> tag. StateReasonData was stored and never rendered. A test/e2e journey drives the real aws-sdk-go-v2 client through all ten operations — the tier whose absence let this ship. Closes #785
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This was referenced Sep 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CloudWatch's service shape,
com.amazonaws.cloudwatch#GraniteServiceVersion20100801, carriesaws.protocols#awsQuery,aws.protocols#awsJson1_0,smithy.protocols#rpcv2Cborandaws.protocols#awsQueryCompatibleat the same time, and its clients disagree about which touse. Substrate read query-form parameters and answered XML no matter what had been asked for.
#739 fixed the routing and v0.109.0 shipped an honest caveat saying so; #757 fixed the
errors. This is the success body, which is the half that made CloudWatch unusable.
Closes #785.
Measured, against a running emulator
Two binaries:
mainatae8d706built in a detached worktree, and this branch. Same script,same
substrate server --address :4678, retries off.aws-sdk-go-v2v1.47.0 / cloudwatch v1.72.0 — every operationdeserialization failed, expected map for struct, got major type 1(0x3C, the leading<, read as a CBOR head)Thresholdreads back80.5,EvaluationPeriods2,Period300put-metric-data,list-metrics,describe-alarms,describe-alarms-for-metric,get-metric-dataput-metric-alarm,set-alarm-stateAn error occurred (InvalidParameterValue) … AlarmName is required— the body was never parsedThe Unit-output rule, on the wire:
and the modeled output it used to make unreadable:
A malformed body is refused rather than silently answered:
mainanswered that same request200with aListMetricsResultXML document.The two files that carry the change
emulator/cloudwatch_input.goflattens a JSON or CBOR body intoreq.Paramsusing thequery protocol's own spelling (
MetricData.member.1.MetricName), so all ten handlers readreq.Paramsunchanged andparseMemberListis untouched. Zero handler-logic changes oninput. That direction is only correct because the CloudWatch model declares no
smithy.api#xmlNameand nosmithy.api#xmlFlattenedtrait anywhere — checked across thewhole model, not assumed — which is what makes a member's query key equal to its member name
and every list wrapped in
member. The other direction would have rewritten all ten handlers'parameter reads for no gain.
emulator/cloudwatch_render.gois one ordered neutral document (cwDoc/cwList) plusthree renderers. A handler has no idea which protocol it is answering — nothing in the file is
reachable from a handler except
cwRespondandcwUnitResponse— because the alternative, aper-protocol branch inside ten handlers, is thirty places for the serializations to drift
apart. The document is an ordered slice rather than a map for the same reason
cborMapis:ranging a Go map orders members by randomized iteration, so one response would encode two ways
and no recorded event could be replayed byte-for-byte.
XML member order is substrate's existing order, not the model's. Order is semantically
meaningless in all three protocols, so keeping the old one costs nothing and buys a byte-exact
assertion over the Query path a consumer already depends on — which is what guards a
ten-operation rewrite.
Unit outputs
Six of the ten are modeled
smithy.api#Unit:PutMetricAlarm,DeleteAlarms,SetAlarmState,EnableAlarmActions,DisableAlarmActions,PutMetricData. On CBOR theyanswer 200 with no body and no
Content-Type, carrying onlySmithy-Protocol. On JSONthey answer
{}—awsJson1_0has no equivalent rule, and botocore reads a zero-length JSONbody as a parse failure. On Query they answer a response wrapper with no result element.
This corrects what the issue asked for. #785 requests "a CBOR map — an empty map for
PutMetricData"; the empty map is tolerated byNoOutputClientAllowsEmptyCbor, which is arequirement on clients, not the conformant server answer. Verified that
server.go'swriteResponseadds no defaultContent-Type, so an empty body genuinelyproduces none.
Defects found while rewriting
DescribeAlarmsForMetricansweredDescribeAlarmsResponse/DescribeAlarmsResult,borrowed from
DescribeAlarms. The query protocol names both elements after the operation.EnableAlarmActionsandDisableAlarmActionsemitted malformed XML — astring-replacement hack left the opening tag as the literal
<placeholder>while the closingtag was the operation's:
Unitoutputs now, so the hack is gone rather than patched.StateReasonDatawas stored bySetAlarmStateand never rendered.GetMetricStatisticswas documented as supported and has never had a dispatch arm.Absent versus present-and-empty
The handler's explicit choice, and it matters to a typed client.
MetricAlarmsis alwayspresent (a byte change: an empty one used to be omitted).
Dimensionson a metric publishedwithout any is present-and-empty.
GetMetricDatareturns present-and-emptyMetricDataResultsandMessages. AMetricAlarmomits every member substrate does nottrack — the three timestamps,
Unit,ExtendedStatistic,DatapointsToAlarm,TreatMissingData— and an alarm with no OK actions omitsOKActions, so a client reads niland can tell it was never set.
Tests
emulator/cloudwatch_protocols_test.go(new, ~620 lines) — input flattening for JSON andCBOR against the same expected keys, tag-1 timestamps, indefinite-length input, three
malformed-body cases, and the assertion that the protocol decides rather than the body
(a JSON-looking body on the Query protocol is left alone). Rendering asserts the whole
XML byte string, JSON member order, CBOR types (
Thresholda double,EvaluationPeriodsand
Periodint64), empty-list encoding in all three, and theUnitrule in all three.Four end-to-end tiers post real HTTP through
httptest.test/e2e/journey_cloudwatch_protocols_test.go(new) — the missing tier. Drives the realaws-sdk-go-v2client through all ten operations behind a recordingRoundTripper, so itasserts the requests genuinely went out as
rpc-v2-cbor(a journey that silently fell backto another protocol would prove nothing), that each
Unitresponse carried no body and noContent-Type, thatThreshold/EvaluationPeriods/Periodkeep their modeled types, thatan unset
OKActionsis nil where an emptyDimensionsis a zero-length slice, and that arefusal deserializes into
*types.ResourceNotFoundException. It fails atae8d706:PutMetricData: … expected map for struct, got major type 1.TestCW_GetMetricData_SmithyRPCV2CBORno longer asserts a single0xa0byte; it decodes theresponse and asserts the members.
Every CloudWatch unit test before this posted a form body and read the XML back as a string,
and
test/e2ehad no CloudWatch journey, so the suite was green over a service no modernclient could use. That is the gap the journey closes.
Provenance
Content-Typerule for aUnitoutput comes from theno_outputtest insmithy-protocol-tests/model/rpcv2Cbor/empty-input-output.smithy, which listsContent-Typein its
forbidHeaders— not from the rpcv2Cbor spec page, which does not state it.EmptyInputOutput, a modeled empty structure rather thanUnit, does setContent-Typeand send
bf ff; the two are different cases.SerializationExceptionfor an undeserializable body is substrate's choice. Neither theprotocol nor the CloudWatch model names a shape for it.
can distinguish, stated in
docs/services.mdrather than left implicit.Compatibility
XML; the CLI and boto3 get JSON where they got silence.
MetricAlarmsandGetMetricData'sMessagesare now always present where an empty one was omitted,DescribeAlarmsForMetricuses its own wrapper element names, and the two alarm-actionoperations produce well-formed XML. A test asserting
<placeholder>would break; nothingshould have been asserting it.
GetMetricDataon CBOR previously returned a fixed0xa0; it now returns a real mapwith both empty lists. A fixture asserting the byte changes; a client reading the value gets
strictly more.
test/e2egainsservice/cloudwatch, which pullsaws-sdk-go-v2v1.45.1 → v1.47.0(plus two
internal/patches). Bothgo.modandgo.sumare tidy in this commit; the fulle2e suite passes on the bump.
Verification
Plus the fail-before/pass-after live run above, driven with
bash,AWS_MAX_ATTEMPTS=1, bothAWS_REGIONandAWS_DEFAULT_REGIONexported andAWS_PROFILEunset.