Skip to content
This repository was archived by the owner on Dec 14, 2022. It is now read-only.

Commit 6817834

Browse files
author
Chris Wiechmann
authored
Merge pull request #22 from Axway-API-Management-Plus/memchached
Adding Memchached to the Logstash pipeline
2 parents 3550252 + ee92e48 commit 6817834

File tree

4 files changed

+67
-26
lines changed

4 files changed

+67
-26
lines changed

.env

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ API_MANAGER_PASSWORD=changeme
4949
# parameter.
5050
LOGSTASH=logstash:5044
5151

52+
# ----------------------------------------------------------------------------------------------
53+
# With this parameter you tell the Logstash processing pipeline which memchached to use. It is
54+
# used to cache the API-Details that has been looked up from the API-Manager via the API-Builder.
55+
# Even if the API-Builder is already caching the result, this improves ingest performance.
56+
# The default parameter works when using the docker-compose.yml
57+
MEMCACHED=memcached:11211
58+
5259
# ----------------------------------------------------------------------------------------------
5360
# This is an optional parameter used by Filebeat to set a proper name. This allows for instance
5461
# to identify the different Filebeat instances in the Kibana-Stack Monitoring dashboards.

.github/workflows/logstash.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ jobs:
1313
- 8080:8080
1414
env:
1515
MOCK_LOOKUP_API: true
16+
memcached:
17+
image: memcached:1.6.6-alpine
18+
ports:
19+
- 11211:11211
1620

1721
steps:
1822
- uses: actions/checkout@v2
@@ -30,6 +34,8 @@ jobs:
3034
- name: Test Logstash pipeline
3135
run: |
3236
echo Using API_BUILDER_URL: $API_BUILDER_URL
33-
./logstash-filter-verifier --diff-command="diff -y" --keep-env=API_BUILDER_URL ./logstash/test ./logstash/pipeline
37+
echo Using MEMCACHED: $MEMCACHED
38+
./logstash-filter-verifier --diff-command="diff -y" --keep-env=API_BUILDER_URL --keep-env=MEMCACHED ./logstash/test ./logstash/pipeline
3439
env:
3540
API_BUILDER_URL: 'http://localhost:8080'
41+
MEMCACHED: 'localhost:11211'

docker-compose.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ services:
7979
- PIPELINE_WORKERS=1
8080
- XPACK_MONITORING_ELASTICSEARCH_HOSTS=["elasticsearch1:9200"]
8181
- API_BUILDER_URL=${API_BUILDER_URL}
82+
- MEMCACHED=${MEMCACHED}
8283
ports:
8384
- 5044:5044
8485
volumes:
@@ -88,6 +89,15 @@ services:
8889
depends_on:
8990
- elasticsearch1
9091
- elk-traffic-monitor-api
92+
- memcached
93+
networks:
94+
- elastic
95+
96+
# Memcached is used by the Logstash pipeline to cache API-Lookup information that are used to get the API-Organization
97+
memcached:
98+
image: memcached:1.6.6-alpine
99+
links:
100+
- elasticsearch1
91101
networks:
92102
- elastic
93103

logstash/pipeline/pipeline.conf

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,33 +64,52 @@ filter {
6464
remove_field => "[transactionSummary][serviceContexts]"
6565
rename => ["[transactionSummary][serviceContext][org]", "[transactionSummary][serviceContext][appOrg]" ]
6666
}
67-
# Future plan is to perform side-calls only if not yet already cached
68-
#mutate {
69-
# add_field => { "apiCacheKey" => "%{[transactionSummary][serviceContext][service]}---%{[transactionSummary][path]}" }
70-
#}
71-
#memcached {
72-
# namespace => "api_details"
73-
# ttl => 300
74-
# get => { "apiCacheKey" => "[transactionSummary][serviceContext][serviceOrg]" }
75-
# remove_field => [ "apiCacheKey" ]
76-
#}
77-
#if ![transactionSummary][serviceContext][serviceOrg] {
78-
# Perform a side-call to load organization details for the API
67+
# If we have a ServiceConext we have to enrich it with the API-Details
7968
if([transactionSummary][serviceContext]) {
80-
http {
81-
url => "${API_BUILDER_URL}/api/elk/v1/api/lookup/api"
82-
query => {
83-
"apiName" => "%{[transactionSummary][serviceContext][service]}"
84-
"apiPath" => "%{[transactionSummary][path]}"
69+
# Create a key for the API
70+
mutate {
71+
add_field => { "apiCacheKey" => "%{[transactionSummary][path]}" }
72+
}
73+
# Lookup the cache with the created API-Key (API-Name---API-Path)
74+
memcached {
75+
hosts => "${MEMCACHED}"
76+
namespace => "api_details"
77+
get => { "%{apiCacheKey}" => "[apiDetails]" }
78+
}
79+
# If we have nothing in the cache, perform the Lookup via the HTTP-Builder API
80+
if !([apiDetails]) {
81+
http {
82+
url => "${API_BUILDER_URL}/api/elk/v1/api/lookup/api"
83+
query => {
84+
"apiName" => "%{[transactionSummary][serviceContext][service]}"
85+
"apiPath" => "%{[transactionSummary][path]}"
86+
}
87+
target_body => "apiDetails"
88+
add_field => { "[@metadata][updateAPICache]" => "true" }
8589
}
86-
target_body => "apiDetails"
87-
add_field => {
88-
"[transactionSummary][serviceContext][apiOrg]" => "%{[apiDetails][organizationName]}"
89-
"[transactionSummary][serviceContext][apiVersion]" => "%{[apiDetails][version]}"
90-
"[transactionSummary][serviceContext][apiDeprecated]" => "%{[apiDetails][deprecated]}"
91-
"[transactionSummary][serviceContext][apiState]" => "%{[apiDetails][state]}"
90+
}
91+
# At this point we should have the apiDetails either from the cache or looked up via HTTP
92+
if([apiDetails]) {
93+
mutate {
94+
add_field => {
95+
"[transactionSummary][serviceContext][apiOrg]" => "%{[apiDetails][organizationName]}"
96+
"[transactionSummary][serviceContext][apiVersion]" => "%{[apiDetails][version]}"
97+
"[transactionSummary][serviceContext][apiDeprecated]" => "%{[apiDetails][deprecated]}"
98+
"[transactionSummary][serviceContext][apiState]" => "%{[apiDetails][state]}"
99+
}
92100
}
93-
remove_field => [ "apiDetails", "headers" ]
101+
# If the API has been looked up add it to the cache
102+
if([@metadata][updateAPICache]=="true") {
103+
memcached {
104+
hosts => "${MEMCACHED}"
105+
namespace => "api_details"
106+
ttl => 3600
107+
set => { "[apiDetails]" => "%{apiCacheKey}" }
108+
}
109+
}
110+
}
111+
mutate {
112+
remove_field => [ "apiDetails", "headers", "updateAPICache", "apiCacheKey" ]
94113
}
95114
# If the API-Lookup failed - Clone the event which is send to an Error index and shown in Traffic-Monitor
96115
if("_httprequestfailure" in [tags]) {
@@ -111,7 +130,6 @@ filter {
111130
}
112131
}
113132
}
114-
#}
115133
}
116134
} else if([logtype] == "trace") {
117135
if [message] =~ /^#/ {

0 commit comments

Comments
 (0)