Why the value of context.getVariable('client.sent.end.timestamp') is always coming as -1

Hi,

I am using JavaScript to send APIGEE proxy details as logs to Datadog. I have noticed that the value of context.getVariable('client.sent.end.timestamp') is always -1. It is timestamp and should not be -1. What is the issue here?

Here is the script below -

var logging = "true";




if (logging == "true") {




    // Get variable from previous KVM configuration lookup
    // var logServerURL = context.getVariable("exco.loggingUrl");




    // Set the Datadog API URL here.
    var dd_api_url = "https://http-intake.logs.datadoghq.com/v1/input/ddapikey?ddsource=apigee";




    // Debug
    print('LOGGING ' + dd_api_url);
    print('Name of the flow: ' + context.flow);




    // calculate response times for client, target and total
    var request_start_time = context.getVariable('client.received.start.timestamp');
    var request_end_time = context.getVariable('client.received.end.timestamp');
    var request_end_time2 = context.getVariable('client.sent.end.timestamp');
    var target_start_time = context.getVariable('target.sent.start.timestamp');
    var target_end_time = context.getVariable('target.received.end.timestamp');
    var total_request_time = request_end_time - request_start_time;
    var total_target_time = target_end_time - target_start_time;
    var total_client_time = total_request_time - total_target_time;




    var timestamp = crypto.dateFormat('YYYY-MM-dd HH:mm:ss.SSS');
    var organization = context.getVariable("organization.name");
    var networkClientIP = context.getVariable("client.ip");
    var httpPort = context.getVariable("client.port");
    var environment = context.getVariable("environment.name");
    var apiProduct = context.getVariable("apiproduct.name");
    var apigeeProxyName = context.getVariable("apiproxy.name");
    var apigeeProxyRevision = context.getVariable("apiproxy.revision");
    var appName = context.getVariable("developer.app.name");
    var httpMethod = context.getVariable("request.verb");
    var httpUrl = '' + context.getVariable("client.scheme") + '://' + context.getVariable("request.header.host") + context.getVariable("request.uri");
    var httpStatusCode = context.getVariable("message.status.code");
    var statusResponse = context.getVariable("message.reason.phrase");
    var clientLatency = total_client_time;
    var targetLatency = total_target_time;
    var totalLatency = total_request_time;
    var userAgent = context.getVariable('request.header.User-Agent');
    var messageContent = context.getVariable('message.content');
    




    // Log attributes
    var logObject = {
        "timestamp": timestamp,
        "organization": organization,
        "network.client.ip": networkClientIP,
        "env": environment,
        "apiProduct": apiProduct,
        "apigee_proxy.name": apigeeProxyName,
        "apigee_proxy.revision": apigeeProxyRevision,
        "appName": appName,
        "http.method": httpMethod,
        "http.url": httpUrl,
        "http.status_code": httpStatusCode,
        "http.port": httpPort,
        "status": statusResponse,
        "clientLatency": clientLatency,
        "targetLatency": targetLatency,
        "totalLatency": totalLatency,
        "http.client.start_time_ms": request_start_time,
        "http.client.end_time_ms": request_end_time,
        "user-agent": userAgent,
        "message": messageContent,
        "client_sent_end_timestamp": request_end_time2,
    };








    var headers = {
        'Content-Type': 'application/json'
    };








    // Debug
    print('LOGGING OBJECT' + JSON.stringify(logObject));




    var myLoggingRequest = new Request(dd_api_url, "POST", headers, JSON.stringify(logObject));








    httpClient.send(myLoggingRequest);
}
Solved Solved
0 3 641
1 ACCEPTED SOLUTION

Unless you're executing this AFTER the response has been sent to the client (ie, in PostClientFlow), this variable won't have been set yet. Please see https://docs.apigee.com/api-platform/reference/variables-reference#client.

Assuming you're executing this close to the end of the processing a good approximation is to use the current timestamp.

Also, this JS code seems to be used for sending log information to a logging system. If your system is syslog compatible, you could use the MessageLogging policy (which can be executed in PostClientFlow)

View solution in original post

3 REPLIES 3

Unless you're executing this AFTER the response has been sent to the client (ie, in PostClientFlow), this variable won't have been set yet. Please see https://docs.apigee.com/api-platform/reference/variables-reference#client.

Assuming you're executing this close to the end of the processing a good approximation is to use the current timestamp.

Also, this JS code seems to be used for sending log information to a logging system. If your system is syslog compatible, you could use the MessageLogging policy (which can be executed in PostClientFlow)

OK, I have added the script in in the ProxyEndpoint response's PostClientFlow. Still getting -1 for client.sent.end.timestamp variable. Here is the gif - https://a.cl.ly/wbu7Y11N.

You can only add a MessageLogging policy to the PostClientFlow. No guarantees that your JS code will execute then (and its results).

My recommendation is that you use an approximation to this value (current timestamp)