splunk-hec-logging

Description

The splunk-hec-logging Plugin is used to forward logs to Splunk HTTP Event Collector (HEC) for analysis and storage.

When the Plugin is enabled, APISIX will serialize the request context information to Splunk Event Data format and submit it to the batch queue. When the maximum batch size is exceeded, the data in the queue is pushed to Splunk HEC. See batch processor for more details.

Attributes

NameRequiredDefaultDescription
endpointTrueSplunk HEC endpoint configurations.
endpoint.uriTrueSplunk HEC event collector API endpoint.
endpoint.tokenTrueSplunk HEC authentication token.
endpoint.channelFalseSplunk HEC send data channel identifier. Read more: About HTTP Event Collector Indexer Acknowledgment.
endpoint.timeoutFalse10Splunk HEC send data timeout in seconds.
ssl_verifyFalsetrueWhen set to true enables SSL verification as per OpenResty docs.
log_formatFalseLog format declared as key value pairs in JSON format. Values only support strings. APISIX or Nginx variables can be used by prefixing the string with $.

This Plugin supports using batch processors to aggregate and process entries (logs/data) in a batch. This avoids the need for frequently submitting the data. The batch processor submits data every 5 seconds or when the data in the queue reaches 1000. See Batch Processor for more information or setting your custom configuration.

Metadata

You can also set the format of the logs by configuring the Plugin metadata. The following configurations are available:

NameTypeRequiredDefaultDescription
log_formatobjectFalse{“host”: “$host”, “@timestamp”: “$time_iso8601”, “client_ip”: “$remote_addr”}Log format declared as key value pairs in JSON format. Values only support strings. APISIX or Nginx variables can be used by prefixing the string with $.
splunk-hec-logging - 图1IMPORTANT

Configuring the Plugin metadata is global in scope. This means that it will take effect on all Routes and Services which use the splunk-hec-logging Plugin.

The example below shows how you can configure through the Admin API:

  1. curl http://127.0.0.1:9180/apisix/admin/plugin_metadata/splunk-hec-logging -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "log_format": {
  4. "host": "$host",
  5. "@timestamp": "$time_iso8601",
  6. "client_ip": "$remote_addr"
  7. }
  8. }'

With this configuration, your logs would be formatted as shown below:

  1. [{"time":1673976669.269,"source":"apache-apisix-splunk-hec-logging","event":{"host":"localhost","client_ip":"127.0.0.1","@timestamp":"2023-01-09T14:47:25+08:00","route_id":"1"},"host":"DESKTOP-2022Q8F-wsl","sourcetype":"_json"}]

Enabling the Plugin

Full configuration

The example below shows a complete configuration of the Plugin on a specific Route:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins":{
  4. "splunk-hec-logging":{
  5. "endpoint":{
  6. "uri":"http://127.0.0.1:8088/services/collector",
  7. "token":"BD274822-96AA-4DA6-90EC-18940FB2414C",
  8. "channel":"FE0ECFAD-13D5-401B-847D-77833BD77131",
  9. "timeout":60
  10. },
  11. "buffer_duration":60,
  12. "max_retry_count":0,
  13. "retry_delay":1,
  14. "inactive_timeout":2,
  15. "batch_max_size":10
  16. }
  17. },
  18. "upstream":{
  19. "type":"roundrobin",
  20. "nodes":{
  21. "127.0.0.1:1980":1
  22. }
  23. },
  24. "uri":"/splunk.do"
  25. }'

Minimal configuration

The example below shows a bare minimum configuration of the Plugin on a Route:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins":{
  4. "splunk-hec-logging":{
  5. "endpoint":{
  6. "uri":"http://127.0.0.1:8088/services/collector",
  7. "token":"BD274822-96AA-4DA6-90EC-18940FB2414C"
  8. }
  9. }
  10. },
  11. "upstream":{
  12. "type":"roundrobin",
  13. "nodes":{
  14. "127.0.0.1:1980":1
  15. }
  16. },
  17. "uri":"/splunk.do"
  18. }'

Example usage

Once you have configured the Route to use the Plugin, when you make a request to APISIX, it will be logged in your Splunk server:

  1. curl -i http://127.0.0.1:9080/splunk.do?q=hello

You should be able to login and search these logs from your Splunk dashboard:

splunk hec search view

Disable Plugin

To disable the splunk-hec-logging Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "uri": "/hello",
  4. "plugins": {},
  5. "upstream": {
  6. "type": "roundrobin",
  7. "nodes": {
  8. "127.0.0.1:1980": 1
  9. }
  10. }
  11. }'