Friday, October 10, 2014

Integrating WSO2 API Manager with BAM and CEP - (Real time data analyzing for WSO2 API Manager) : Part 2 - Configuring CEP to send a mail when there are more than 5 API calls within a minute by a user

Hope you did follow all the instruction given in part 1.

To clear on what I am doing next you better take a look on the basic flow of events through the components in CEP and best if you can go through [1].

CEP-Flow-New-Page.png

API Manager stat publisher publishing three event streams.

  1. org.wso2.apimgt.statistics.request
  2. org.wso2.apimgt.statistics.response
  3. org.wso2.apimgt.statistics.fault

Here I am only show you how to configure for org.wso2.apimgt.statistics.request event stream and you can do the same for other two event streams as well.

Now we need to define two event stream definition for import and export the stream to the execution plan. You can create a event stream in Main -> Manage -> Event Processor -> Event Streams.

Import Event stream

7.png


Full definition

{
 "name": "org.wso2.apimgt.statistics.request",
 "version": "1.0.0",
 "nickName": "API Manager Request Data",
 "description": "Request Data",
 "metaData": [
   {
     "name": "clientType",
     "type": "STRING"
   }
 ],
 "payloadData": [
   {
     "name": "consumerKey",
     "type": "STRING"
   },
   {
     "name": "context",
     "type": "STRING"
   },
   {
     "name": "api_version",
     "type": "STRING"
   },
   {
     "name": "api",
     "type": "STRING"
   },
   {
     "name": "resource",
     "type": "STRING"
   },
   {
     "name": "method",
     "type": "STRING"
   },
   {
     "name": "version",
     "type": "STRING"
   },
   {
     "name": "request",
     "type": "INT"
   },
   {
     "name": "requestTime",
     "type": "LONG"
   },
   {
     "name": "userId",
     "type": "STRING"
   },
   {
     "name": "tenantDomain",
     "type": "STRING"
   },
   {
     "name": "hostName",
     "type": "STRING"
   },
   {
     "name": "apiPublisher",
     "type": "STRING"
   },
   {
     "name": "applicationName",
     "type": "STRING"
   },
   {
     "name": "applicationId",
     "type": "STRING"
   }
 ]
}

Now its all about configuring CEP in order to send a mail when there are more than 5 API calls within a minute by a user

Lets start with adding a Out Event Adaptor
Event Adaptor Name : AMEmailEventAdaptor
Event Adaptor Type : email

3.png

Now we can create the output email event stream

{
 "name": "org.wso2.apimgt.statistics.request.email",
 "version": "1.0.0",
 "nickName": "",
 "description": "",
 "payloadData": [
   {
     "name": "myCount",
     "type": "LONG"
   },
   {
     "name": "api_version",
     "type": "STRING"
   },
   {
     "name": "api",
     "type": "STRING"
   },
   {
     "name": "consumerKey",
     "type": "STRING"
   }
 ]
}

13.png

Now we have the out event stream. So we need to compose the mail out of this stream. We can do that by defining a Event Formatter by clicking Out-Flows in stream list view.

Event Formatter Name : AMStatEmailEventFormatter
Output Event Adaptor Name : AMEmailEventAdaptor
Subject : More than 5 API invocations happen within 1min
Email Address : eranda@abc.com
Output Type : text
Text Mapping :
API name : {{api}},
Version : {{api_version}},
ConsumerKey : {{consumerKey}}

18.png

Only thing left to do is creating the Event Execution Plan. That can be done in Main -> Manage -> Event Processor -> Execution Plans.

Execution Plan Name : APIMEmailExecutionPlan

Import Stream
Stream Id : org.wso2.apimgt.statistics.request:1.0.0
As : AMRequest

Siddi Query :
from AMRequest#window.time(1 min)
select count(consumerKey) as myCount,api_version,api,consumerKey
group by consumerKey,api_version,api
having (myCount > 5)
insert into outStream;

Export Stream
Value Of : outStream
Stream Id : org.wso2.apimgt.statistics.request.email:1.0.0

20.png

Done.

References :

No comments:

Post a Comment