Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Why APIGEE Cloud Logging in Message logging json payload is not able to log?

Hi

The Message logging policy is not logging any json payload .

For Example request json and response json

policy details

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<MessageLogging continueOnError="false" enabled="true" name="ML-CloudLogging">
<DisplayName>ML-CloudLogging</DisplayName>
<CloudLogging>
<LogName>projects/{organization.name}/logs/apigee</LogName>
<Message contentType="application/json">
{
"organization": "{organization.name}",
"environment": "{environment.name}",
"apiProxy": "{apiproxy.name}",
"apiProxyRevision": "{apiproxy.revision}",
"apiProduct": "{apiproduct.name}",
"developerApp": "{apiproduct.name}",
"clientId": "{client_id}",
"developerId": "{developer.id}",
"requestUri": "{request.uri}",
"requestUrl": "{request.url}",
"verb": "{request.verb}",
"correlationId": "{messageid}",
"proxyRequestReceived": "{client.received.end.timestamp}",
"proxyResponseSent": "{client.sent.end.timestamp}",
"targetResponseReceived": "{target.received.end.timestamp}",
"targetRequestSent": "{target.sent.end.timestamp}",
"targetResponseCode": "{message.status.code}",
"proxyResponseCode": "{response.status.code}",
"clientReceived": "{client.received.start.timestamp}",
"clientSent": "{client.sent.start.timestamp}",
"faultName":"{fault.name}",
"requestData":"{flow.requestPayload}",
"requestId":"{flow.requestId}"
}
</Message>
<Labels>
<Label><Key>apiProxy</Key>
<Value>jsonUpload</Value>
</Label>
</Labels>
<ResourceType>apigee.googleapis.com/Environment</ResourceType>
</CloudLogging>
</MessageLogging>

 

when I remove the "requestData":"{flow.requestPayload}",

Its then working fine

@dchiesa1  

 

Solved Solved
1 2 322
1 ACCEPTED SOLUTION

What is contained within flow.requestPayload ?

If that variable contains double-quotes, it may result in an invalid JSON payload being send to Cloud Logging. The expression {flow.requestPayload} in that policy tells Apigee to perform simple string replacement, placing the value of the variable into the place where the expression is.

So if your flow.requestPayload variable contains "abc", "123" (including the quotes), then the result of

 

{
  "requestData":"{flow.requestPayload}",
  "requestId":"{flow.requestId}"
}

 

...will be something like

 

{
  "requestData": ""abc", "123"" 
  "requestId":"iafhjdsjkksdvnl"
}

 

..and as you can see, that is not valid JSON. When Cloud Logging receives a request with an invalid JSON payload, it will reject/ignore it, and no log message will get logged.

Even if the variable contains valid JSON, and the result is valid JSON, Cloud Logging may reject it, because it is expecting requestData to be a STRING, not a nested JSON. I am not sure about this, but it is possible.

The way to avoid this is to escape the thing that may have double-quotes in it. So you must do:

 

{
   ...
  "requestData":"{escapeJSON(flow.requestPayload)}",
  "requestId":"{flow.requestId}"
}

 

And you should do the same for any variable that may contain double-quotes.

View solution in original post

2 REPLIES 2

What is contained within flow.requestPayload ?

If that variable contains double-quotes, it may result in an invalid JSON payload being send to Cloud Logging. The expression {flow.requestPayload} in that policy tells Apigee to perform simple string replacement, placing the value of the variable into the place where the expression is.

So if your flow.requestPayload variable contains "abc", "123" (including the quotes), then the result of

 

{
  "requestData":"{flow.requestPayload}",
  "requestId":"{flow.requestId}"
}

 

...will be something like

 

{
  "requestData": ""abc", "123"" 
  "requestId":"iafhjdsjkksdvnl"
}

 

..and as you can see, that is not valid JSON. When Cloud Logging receives a request with an invalid JSON payload, it will reject/ignore it, and no log message will get logged.

Even if the variable contains valid JSON, and the result is valid JSON, Cloud Logging may reject it, because it is expecting requestData to be a STRING, not a nested JSON. I am not sure about this, but it is possible.

The way to avoid this is to escape the thing that may have double-quotes in it. So you must do:

 

{
   ...
  "requestData":"{escapeJSON(flow.requestPayload)}",
  "requestId":"{flow.requestId}"
}

 

And you should do the same for any variable that may contain double-quotes.

That working now.

Thanks @dchiesa1