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 policies.

Syntax
getMetricData()
Guidelines
Returns metric information in an array about the processed policies in the assembly flow. This API is meaningful when you enable the log policy to capture transaction data and put the GatewayScript policy as the last action in the assembly flow. In other words, add the GatewayScript policy as the last action of postprocessing to the configuration of each API collection.
The following table explains the metric information.
Name Description
type The type of the processed action.
name The name of the configured action.
correlationPath The correlation path of the action in the OpenAPI document, which it gets from the corrleation-path property of the action.
assemblyType The location of the action in the assembly flow.
pre
Preprocessing
main
Processing
post
Postprocessing
startTime The starting time of the action from the beginning of the transaction. The measure is milliseconds.
endTime The ending time of the action from the beginning of the transaction. The measure is milliseconds.
targetURL The target URL to send in the invoke action.
requestTime The time to send the request to the target server. The measure is milliseconds.
responseTime The time when the response comes back to the service. The measure is milliseconds.
requestSize The request size to send to the target server. The measure is bytes.
responseSize The response size returned from the target server, including headers and payload. The value exists only when the Content-Length header 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 log variable 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.