faro-web-sdk
faro-web-sdk copied to clipboard
type enforcement
Description
The MeasurementEvent
type specifies the value should be a number. The EventAttributes
specify the attribute value should be a string. The API will allow users to put anything for these values I believe. Is it possible to enforce these types on the client side before the data is sent to the receiver?
https://github.com/grafana/faro-web-sdk/blob/a462a95d24410ed90dd9b66c1226d50684fc74dc/packages/core/src/api/measurements/types.ts#L3 https://github.com/grafana/faro-web-sdk/blob/a462a95d24410ed90dd9b66c1226d50684fc74dc/packages/core/src/api/events/types.ts#L3
The current receiver implementation will fail to deserialize requests if these types aren't enforced: https://github.com/grafana/agent/blob/main/pkg/integrations/v2/app_agent_receiver/payload.go#L202 https://github.com/grafana/agent/blob/main/pkg/integrations/v2/app_agent_receiver/payload.go#L346
Proposed solution
On the client side, perhaps typeof
could be used to determine if the value is correct? I'm not 100% sure what the best solution would be.
In addition to enforcing this as much as possible on the client side, I propose that this is fixed in the receiver as well to allow any type that is sent because everything ends up stored in loki as a string.
Context
Here's an example:
let quantity = 100;
faro.api.pushEvent('add-to-cart', {
'productId': productId,
'quantity': quantity,
});
Here quantity is a number
. This results in a 400 from the receiver because it will fail to deserialize the request.