How to Monitor 3Scale API Gateway Logs with Moesif

Updated:
2 minute read

There is no suprise that the applications in the mordern microservice architecture communicate by using all sorts of APIs, RESTful, GraphQL and more. API Gateways provides authentication, rate-limiting and access control between applications that expose their APIs and the consumer of the APIs are the bedrock of API infrastructure. Simple architecture which bundles API related functionality inside a single component provides load balancing, caching and ability to scale to ensure high availability.

Nginx is a high‑performance, highly scalable, highly available web server, reverse proxy server, and web accelerator (combining the features of an HTTP load balancer, content cache, and more). NGINX has a modular, event‑driven, asynchronous, single-threaded architecture that scales extremely well on generic server hardware and across multi-processor systems.

3Scale 3scale API Management Platform is basically a set of modular components that can be deployed in various topologies from being hosted in the public cloud to being deployed on-premise, and companies can use this product to control their APIs and literally bring them under control of some sort of governance that they can implement using the 3scale platform.

Setting Up Moesif API Analytics with 3Scale

Moesif has a plugin available in the Luarocks that enables you to understand API usage and monitor your API traffic. With Moesif, you’re able to understand how your API is used and by what customers, identify which customers are running into integration issues, and monitor for endpoints that need optimization.

The recommended way to install Moesif is via Luarocks:

luarocks install --server=http://luarocks.org/manifests/moesif lua-resty-moesif

How to use

lua_shared_dict moesif_conf 2m;

init_by_lua_block {
   local config = ngx.shared.moesif_conf;
   config:set("application_id", "Your Moesif Application Id")
   config:set("3scale_domain", "YOUR_ACCOUNT-admin.3scale.net")
   config:set("3scale_access_token", "Your 3scale Access Token")
}

lua_package_cpath ";;${prefix}?.so;${prefix}src/?.so;/usr/share/lua/5.1/lua/resty/moesif/?.so;/usr/share/lua/5.1/?.so;/usr/lib64/lua/5.1/?.so;/usr/lib/lua/5.1/?.so;/usr/local/openresty/luajit/share/lua/5.1/lua/resty?.so";
lua_package_path ";;${prefix}?.lua;${prefix}src/?.lua;/usr/share/lua/5.1/lua/resty/moesif/?.lua;/usr/share/lua/5.1/?.lua;/usr/lib64/lua/5.1/?.lua;/usr/lib/lua/5.1/?.lua;/usr/local/openresty/luajit/share/lua/5.1/lua/resty?.lua";

server {
  listen 80;
  resolver 8.8.8.8;

  # Customer identity variables that Moesif will read downstream
  # Set automatically from 3scale management API
  set $moesif_user_id nil;
  set $moesif_company_id nil;
  set $moesif_req_body nil;
  set $moesif_res_body nil;

  access_by_lua_file /usr/share/lua/5.1/lua/resty/moesif/read_req_body.lua;
  body_filter_by_lua_file /usr/share/lua/5.1/lua/resty/moesif/read_res_body.lua;
  log_by_lua_file /usr/share/lua/5.1/lua/resty/moesif/send_event_3Scale.lua;

  # Sample Hello World API
  location /api {
    add_header Content-Type "application/json";
    return 200 '{\r\n  \"message\": \"Hello World\",\r\n  \"completed\": true\r\n}';
  }
}

Moesif recommends using the same Moesif Application Id for all services and routes configured in Nginx. However, it’s recommended to use separate Moesif Application Ids for each isolated environment such as a production and development environment.

Insight into User behavior

API analytics revolve around how users (or companies if your B2B) are experiencing your application. Also known as user behavioral analytics, every event or action can be linked back to an individual customer and behavioral trends can be discovered by looking at multiple events together.

By focusing on customer centric usage, we can find product issues such as why users stop using your API or which features or endpoints they engage with the most.

Moesif automatically fetches customer context from 3scale admin API. By default, id field is used to identify the user from the 3Scale’s application XML entity. We could override the default field with other fields including user_account_id, service_id and more. Moesif support both authentication mode - API Key(user_key) and App_ID and App_Key Pair for your customers to authenticate your API and link that event to an individual customer.

To see the 3Scale integration with Moesif in action, you can git clone and run this example app from GitHub

Conclusion

In this way, this plugin will capture API requests and responses and log to Moesif for easy inspecting and real-time debugging of your API traffic via 3Scale.

Get User-Centric API Monitoring for 3Scale

Learn More
Get User-Centric API Monitoring for 3Scale

Leave a comment