---
title: Apigee
description: >-
  Collect Apigee proxy logs to track errors, request response time, duration,
  latency and monitor performance and issues of the proxies aggregated in one
  place.
breadcrumbs: Docs > Log Management > Logs Guides > Apigee
---

# Apigee

## Overview{% #overview %}

Collect Apigee proxy logs to track errors, response time, duration, latency, monitor performance, and proxy issues.

## Setup{% #setup %}

### Log collection{% #log-collection %}

{% callout %}
# Important note for users on the following Datadog sites: app.datadoghq.com, app.datadoghq.eu



There are two methods for collecting Apigee logs:

1. Use Apigee's [JavaScript policy](https://cloud.google.com/apigee/docs/api-platform/reference/policies/javascript-policy?hl=en) to send logs to Datadog.
1. If you already have a syslog server, use the Apigee [MessageLogging policy](https://cloud.google.com/apigee/docs/api-platform/reference/policies/message-logging-policy) type to log to a syslog account.

#### Syslog parameter{% #syslog-parameter %}

Use the MessageLogging policy type with the syslog parameter on your API to log custom messages to syslog. Replace `<site_intake_endpoint>` with  and `<site_port>` with , in the following example:

```json
<MessageLogging name="LogToSyslog">
    <DisplayName>datadog-logging</DisplayName>
    <Syslog>
        <Message><YOUR API KEY> test</Message>
        <Host><site_intake_endpoint></Host>
        <Port><site_port></Port>
        <Protocol>TCP</Protocol>
    </Syslog>
    <logLevel>ALERT</logLevel>
</MessageLogging>
```


{% /callout %}

#### JavaScript policy{% #javascript-policy %}

Send Apigee proxy logs to Datadog using Apigee's JavaScript policy. For detailed instructions, see the [Apigee documentation](https://docs.apigee.com/api-platform/reference/policies/javascript-policy).

1. Select the Apigee proxy from which you want to send logs to Datadog.
1. In the selected proxy overview page, click the **DEVELOP** tab.
1. Select **New Script**.
1. Select JavaScript and add flow variables into JavaScript from the [Apigee flow variable reference](https://docs.apigee.com/api-platform/reference/variables-reference).

{% collapsible-section %}
**Example JavaScript code snippet**
See the following example JavaScript code snippet. Replace `<DATADOG_API_KEY>` in the `dd_api_url` variable with your [Datadog API KEY](https://app.datadoghq.com/organization-settings/api-keys). The JavaScript has been configured to capture the essential flow variables as log attributes in Datadog. The attributes are named according to the list of standard attributes.

```java
// Set the Datadog API URL here.
var dd_api_url = "https://http-intake.logs.<YOUR_DATADOG_SITE>/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);
```

{% /collapsible-section %}

## Troubleshooting{% #troubleshooting %}

Need help? Contact [Datadog support](https://docs.datadoghq.com/help/).

## Further Reading{% #further-reading %}

- [Apigee JavaScript Policy](https://docs.apigee.com/api-platform/reference/policies/javascript-policy)
- [Log Management](https://docs.datadoghq.com/logs/)
