Consulta el siguiente fragmento de código JavaScript de ejemplo. Sustituye <DATADOG_API_KEY>
en la variable dd_api_url
por tu clave de API Datadog. JavaScript se ha configurado para capturar las variables de flujo esenciales como atributos de logs en Datadog. Los atributos se nombran de acuerdo a la lista de atributos estándar.
// Set the Datadog API URL here.
var dd_api_url = "https://http-intake.logs.<code class="js-region-param region-param" data-region-param="dd_site"></code>/api/v2/logs?dd-api-key=<DATADOG_API_KEY>&ddsource=apigee";
// Debug
// print(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 system_timestamp = context.getVariable('system.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 = system_timestamp - 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("response.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');
// Datadog log attributes
var logObject = {
"timestamp": timestamp,
"organization": organization,
"network.client.ip": networkClientIP,
"env": environment,
"apiProduct": apiProduct,
"apigee_proxy.name": apigeeProxyName,
"apigee_proxy.revision": apigeeProxyRevision,
"service": 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,
"http.useragent": userAgent,
"message": messageContent,
};
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));
// Send logs to Datadog
httpClient.send(myLoggingRequest);