项目作者: srinandan

项目描述 :
Forward log messages to fluentd
高级语言:
项目地址: git://github.com/srinandan/apigee-fluentd-logger.git
创建时间: 2020-03-26T03:23:41Z
项目社区:https://github.com/srinandan/apigee-fluentd-logger

开源协议:Apache License 2.0

下载


Using Apigee Message Logger with fluentd

Use the Message Logger policy to log messages to fluentd.

Scenario

Apigee’s Message Logger policy allows users to forward log messages (parts or whole of the request and/or response) to a remote syslog server (or the file system in Apigee Private Cloud). Enterprises may have Splunk or Dynatrace as their logging standard and want to integrate Apigee Message Logger with such standards.

Fluentd is an open source data collector for unified logging layer and has a rich set of plugins that allows enterprises to integrate with. This example will show how to setup fluentd to consume messages from Message Logger.

fluentd setup

Here is the sample configuration used for fluentd. The configuration for fluentd is stored in a ConfigMap with the following details:

  1. # Takes the messages sent over UDP
  2. <source>
  3. @type syslog
  4. tag apigee
  5. port 5140
  6. bind 0.0.0.0
  7. <parse>
  8. message_format rfc5424
  9. </parse>
  10. </source>
  11. <match apigee.**>
  12. @type stdout
  13. </match>

This configuration listens on UDP on port 5140. Install the fluentd service with the command:

  1. kubectl apply -f fluentd-logger.yaml

Message Logger Policy

Configure the Message Logger policy to send syslog events to the fluentd service. A sample API proxy is included here

  1. <MessageLogging async="false" continueOnError="false" enabled="true" name="Log-Message">
  2. <DisplayName>Log Message</DisplayName>
  3. <Syslog>
  4. <Message>Response message: {response.content}</Message>
  5. <Host>apigee-fluentd.apps.svc.cluster.local</Host>
  6. <Port>5140</Port>
  7. <FormatMessage>true</FormatMessage>
  8. </Syslog>
  9. </MessageLogging>

Output

If the setup was successful, you will see logs in fluentd’s pod

Access the pod’s log

  1. kubectl logs -n apps apigee-fluentd-5bc99d9d9f-j9bb

OUTPUT:

  1. 20xx-xx-xx 00:41:45 +0000 [info]: starting fluentd-1.3.2 pid=6 ruby="2.5.2"
  2. 20xx-xx-xx 00:41:45 +0000 [info]: spawn command to main: cmdline=["/usr/bin/ruby", "-Eascii-8bit:ascii-8bit", "/usr/bin/fluentd", "-c", "/fluentd/etc/fluent.conf", "-p", "/fluentd/plugins", "--under-supervisor"]
  3. 20xx-xx-xx 00:41:45 +0000 [info]: gem 'fluentd' version '1.3.2'
  4. 20xx-xx-xx 00:41:45 +0000 [info]: adding match pattern="apigee.**" type="stdout"
  5. 20xx-xx-xx 00:41:45 +0000 [info]: adding source type="syslog"
  6. 20xx-xx-xx 00:41:45 +0000 [info]: #0 starting fluentd worker pid=16 ppid=6 worker=0
  7. 20xx-xx-xx 00:41:45 +0000 [info]: #0 listening syslog socket on 0.0.0.0:5140 with udp
  8. 20xx-xx-xx 00:41:45 +0000 [info]: #0 fluentd worker is now running worker=0
  9. 20xx-xx-xx 00:41:56.849000000 +0000 apigee.user.info: {"host":"9178f6a4-6e9b-49d8-be5a-e9ac1af1a106","ident":"Apigee-Edge","pid":"-","msgid":"-","extradata":"-","message":"Response message: Hello, Guest!\u0000"}

Stackdriver Logging

To send the Message Logger policy output to Stackdriver logs, use the google-fluentd image.

  1. containers:
  2. - name: fluentd
  3. image: google/apigee-stackdriver-logging-agent:1.6.8

Mount a service account with Log Write role.

  1. kubectl create secret -n apps generic logging-svc-account --from-file client_secret.json

In the deployment spec:

  1. - name: svc-account-volume
  2. secret:
  3. defaultMode: 420
  4. secretName: logging-svc-account

Finally, change the fluentd configuration to look like:

  1. fluent.conf: |-
  2. <source>
  3. @type syslog
  4. tag apigee
  5. port 5140
  6. bind 0.0.0.0
  7. <parse>
  8. message_format rfc5424
  9. </parse>
  10. </source>
  11. <filter **>
  12. @type add_insert_ids
  13. insert_id_key apigee-message-logger
  14. </filter>
  15. <match **>
  16. @type google_cloud
  17. project_id xxx
  18. </match>

Viewing the logs

Check the fluentd logs to see the instance Id for the logs:

Example:

  1. 20xx-xx-xx 21:03:19 +0000 [info]: adding match pattern="**" type="google_cloud"
  2. 20xx-xx-xx 21:03:19 +0000 [info]: #0 Detected GCE platform
  3. 20xx-xx-xx 21:03:19 +0000 [info]: #0 Logs viewer address: https://console.cloud.google.com/logs/viewer?project=xxxx&resource=container/instance_id/3266770578670000001
  4. 20xx-xx-xx 21:03:19 +0000 [info]: adding source type="syslog"

Open the link in your browser and view logs

alt text

Thank you Sukruth for helping with these intructions.


Support

This is not an officially supported Google product