context.metric object for the API gateway
The context.metric object provides APIs to collect data from assembly processing by the API gateway.
The
context.metric object provides the following APIs to collect data from
assembly processing. Generally you call these APIs during postprocessing to send to the analytics
endpoint.
getMetricData()
Returns metric information about the processed assembly actions.
- Syntax
getMetricData()- Guidelines
- Returns metric information in an array about the processed assembly actions in the assembly
flow. This API is meaningful when you enable the log assembly action to capture transaction data and
put the GatewayScript assembly action as the last action in the assembly flow. In other words, add
the GatewayScript assembly action as the last action of postprocessing to the configuration of each
API collection.The following table explains the metric information.
Name Description typeThe type of the processed action. nameThe name of the configured action. correlationPathThe correlation path of the action in the OpenAPI document, which it gets from the corrleation-pathproperty of the action.assemblyTypeThe location of the action in the assembly flow. pre- Preprocessing
main- Processing
post- Postprocessing
startTimeThe starting time of the action from the beginning of the transaction. The measure is milliseconds. endTimeThe ending time of the action from the beginning of the transaction. The measure is milliseconds. targetURLThe target URL to send in the invoke action. requestTimeThe time to send the request to the target server. The measure is milliseconds. responseTimeThe time when the response comes back to the service. The measure is milliseconds. requestSizeThe request size to send to the target server. The measure is bytes. responseSizeThe response size returned from the target server, including headers and payload. The value exists only when the Content-Lengthheader exists in the response or message buffering is off. The measure is bytes.The following sample illustrates the returned data.[ { "type": "assembly-parse", "name": "parse-request-payload", "correlationPath": "$.x-ibm-configuration.assembly.execute[0]", "assemblyType": "pre", "startTime": 10, "endTime": 20 }, { "type": "assembly-invoke", "name": "simpleinvoke", "correlationPath": "$.x-ibm-configuration.assembly.execute[1]", "assemblyType": "main", "startTime": 22, "targetURL": "https://foo.bar?baz=3", "requestTime": 35, "requestSize": 1003, "responseTime": 1000, "responseSize": 2005, "endTime": 1000 }, { "type": "assembly-setvar", "name": "setvar-in-error", "correlationPath": "$.x-ibm-configuration.assembly.execute[3]", "assemblyType": "main", "startTime": 1003, "endTime": 1010 }, { "type": "assembly-gatewayscript", "name": "metric", "correlationPath": "$.x-ibm-configuration.assembly.execute[4]", "assemblyType": "post", "startTime": 1015 } ] - Example
- Add the metric data to the
logvariable in the API context. The data is sent to the analytics endpoint.context.set('log.metric', context.metric.getMetricData());
getRequestSize()
Returns the size of the request that is sent from the client.
- Syntax
getRequestSize()- Guidelines
- The getRequestSize() API returns the size in byes of the request that is sent from the client. The value is 0 until the payload stream is consumed by the assembly.