Moesif API Analytics
Last updated 09 September 2019
Table of Contents
Moesif is an add-on API Analytics service for: - Modern Customer and API Analytics - Real-time API Debugging - Intelligent API Monitoring
Support for REST, GraphQL, Ethereum Web3, JSON-RPC, SOAP, & more
Moesif helps you prevent minor issues from becoming major outages through deep API monitoring for anomalous behavior of your production API traffic. Learn more about Moesif features.
- Understand how your customers use your API and what drives engagement.
- Root cause API issues quickly with ML powered features like Smart Diff.
- Get Slack and PagerDuty alerts of anomalous API behavior that pings tests don’t catch
Moesif offers drop-in open-source SDKs for the following languages:
Provisioning the add-on
Moesif can be attached to a Heroku application via the CLI:
A list of all plans can be found here.
$ heroku addons:create moesif
-----> Adding moesif to sharp-mountain-4005... done, v18 (free)
During provisioning, a MOESIF_APPLICATION_ID
config var is set in your Heroku app’s configuration. It contains the write-only API token that identifies your application with Moesif. You can confirm the variable exists via the heroku config:get
command:
$ heroku config:get MOESIF_APPLICATION_ID
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
You now need to set up your language-specific SDK integration with this
MOESIF_APPLICATION_ID
.
Local setup
Environment setup
After you provision the add-on, you need to locally replicate your config vars so your development environment can operate against the service.
Use the Heroku Local command-line tool to configure, run, and manage process types specified in your app’s Procfile. Heroku Local reads configuration variables from a .env
file. To view all of your app’s config vars, type heroku config
. Use the following command for each value that you want to add to your .env
file:
$ heroku config:get MOESIF_APPLICATION_ID -s >> .env
Credentials and other sensitive values should not be committed to source-control. In Git exclude the .env
file with: echo .env >> .gitignore
.
For more information, see the Heroku Local article.
Using with Node.JS/Express
Install the moesif-express SDK.
$ npm install --save moesif-express
Add moesif-express middleware to your express app:
var express = require('express');
var app = express();
var moesifExpress = require('moesif-express');
// Set the options, the only required field is applicationId.
var options = {
applicationId: process.env.MOESIF_APPLICATION_ID,
identifyUser: function (req, res) {
if (req.user) {
return req.user.id;
}
return undefined;
},
getSessionToken: function (req, res) {
return req.headers['Authorization'];
}
};
// Load the Moesif middleware
app.use(moesifExpress(options));
If you’re not using the Express framework, you can still use this library. The library does not depend on Express, so you can still call the middleware from a basic HTTP server.
var moesifExpress = require('moesif-express');
const http = require('http');
var options = {
applicationId: process.env.MOESIF_APPLICATION_ID
};
var server = http.createServer(function (req, res) {
moesifExpress(options)(req, res, function () {
});
req.on('end', function () {
res.write(JSON.stringify({
message: "hello world!",
id: 2
}));
res.end();
});
});
server.listen(8080);
For a full list of available options, see this page.
Using with Rails 3.x
Install moesif_rack
middleware:
gem install moesif_rack
If you have a Gemfile
in your project, add this line to it:
gem 'moesif_rack', '~> 1.2.0'
Inside your Rails app, configure the Moesif options. See more configuration options here.
moesif_application_id = ENV['MOESIF_APPLICATION_ID']
moesif_options = {
'application_id' => moesif_application_id
}
Within config/application.rb
, add moesif_rack
as a midddleware:
class Application < Rails::Application
# snip
config.middleware.use "MoesifRack::MoesifMiddleware", moesif_options
# snip
end
Verify your integration by sending a few API calls through your app, and open Moesif Dashboard to see the data:
$ heroku addons:open moesif
Using with Python/Django
Install the moesifdjango
library:
$ pip install moesifdjango
Add Moesif to the Django middleware. The process for this differs depends on whether your version of Django is at least 1.10.
For Django 1.10 and later:
MIDDLEWARE = [
...
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'moesifdjango.middleware.moesif_middleware'
...
]
For Django 1.9 and earlier:
MIDDLEWARE_CLASSES = [
...
'moesifdjango.middleware_pre19.MoesifMiddlewarePre19',
...
# other middlewares
]
For all versions, add MOESIF_MIDDLEWARE
settings into your settings.py
file:
import os
moesif_id = os.environ.get('MOESIF_APPLICATION_ID')
MOESIF_MIDDLEWARE = {
'APPLICATION_ID': moesif_id,
# ...
# other options see https://github.com/Moesif/moesifdjango/
}
For list of all available options, see this page.
Verify your integration by sending a few API calls through your app, and open the Moesif dashboard to see the data:
$ heroku addons:open moesif
Using with Python/WSGI
If you are using Flask, Bottle, or another framework that is built on top of WSGI, install the moesifwsgi
library.
$ pip install moesifwsgi
Flask Example:
import os
from moesifwsgi import MoesifMiddleware
moesif_settings = {
'APPLICATION_ID': os.environ.get('MOESIF_APPLICATION_ID')
}
app.wsgi_app = MoesifMiddleware(app.wsgi_app, moesif_settings)
Bottle Example:
import os
from moesifwsgi import MoesifMiddleware
app = bottle.Bottle()
moesif_settings = {
'APPLICATION_ID': os.environ.get('MOESIF_APPLICATION_ID'),
}
bottle.run(app=MoesifMiddleware(app, moesif_settings))
For more options and examples for other frameworks see this page.
Using with Java
Install the moesif-servlet SDK.
For Maven users, add this dependency to your project’s pom.xml
file:
<dependency>
<groupId>com.moesif.servlet</groupId>
<artifactId>moesif-servlet</artifactId>
<version>1.2.0</version>
</dependency>
Assuming that you are using Spring Boot, which is the standard Java framework supported by Heroku, add the Moesif Servlet filter to your configuration:
import com.moesif.servlet.MoesifFilter;
import javax.servlet.Filter;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.context.annotation.*;
import org.springframework.http.converter.*;
@Configuration
public class MyConfig extends WebMvcConfigurerAdapter {
@Bean
public Filter moesifFilter() {
String moesifApplicationId = System.getenv().get("MOESIF_APPLICATION_ID");
return new MoesifFilter(moesifApplicatoinId);
}
}
Optionally, to customize the filter, pass in an object that implements MoesifConfiguration
, such as
MoesifConfigurationAdapter
.
@Configuration
public class MyConfig extends WebMvcConfigurerAdapter {
MoesifConfiguration config = new MoesifConfigurationAdapter() {
@Override
public String getSessionToken(HttpServletRequest request, HttpServletResponse response) {
return request.getHeader("Authorization");
}
};
@Bean
public Filter moesifFilter() {
return new MoesifFilter("your application id", config);
}
}
For more configuration options and integration options for other frameworks, see this page.
Using with PHP/Laravel
Install the Moesif PHP Laravel SDK
$ composer require moesif/moesif-laravel
Add service provider:
// within config/app.php
'providers' => [
//
Moesif\Middleware\MoesifLaravelServiceProvider::class,
];
Add to middleware:
// within App/Http/Kernel.php
protected $middleware = [
//
\Moesif\Middleware\MoesifLaravel::class,
];
Alternatively, add the midddleware to a specific route group:
protected $middlewareGroups = [
//
'api' => [
//
\Moesif\Middleware\MoesifLaravel::class,
],
];
Publish the package config file:
$ php artisan vendor:publish --provider="Moesif\Middleware\MoesifLaravelServiceProvider"
Set up config:
// within config/moesif.php
return [
//
'applicationId' => getenv('MOESIF_APPLICATION_ID'),
];
For complete list of available options, see this page.
Dashboard
For more information on the features available within the Moesif dashboard, please see the docs at https://www.moesif.com/docs.
The Moesif dashboard allows you to view your data, generate visualizations from our large library of charts, configure alert rules, and run machine-learning-powered jobs that perform root cause analysis.
You can access the dashboard via the CLI:
$ heroku addons:open moesif
Opening moesif for sharp-mountain-4005
or by visiting the Heroku Dashboard and selecting the application in question. Select Moesif from the Add-ons menu.
Migrating between plans
Use the heroku addons:upgrade
command to migrate to a new plan.
$ heroku addons:upgrade moesif:newplan
-----> Upgrading moesif:newplan to sharp-mountain-4005... done, v18 ($49/mo)
Your plan has been updated to: moesif:newplan
Removing the add-on
You can remove Moesif via the CLI:
This cannot be undone!
$ heroku addons:destroy moesif
-----> Removing moesif from sharp-mountain-4005... done, v20 (free)
Moesif Terms of Service
By provisioning a Moesif add-on, you are agreeing to Moesif’s Terms of Service and Privacy Policy, which can be found at https://www.moesif.com/terms and https://www.moesif.com/privacy.
Support
All Moesif support and runtime issues should be submitted via one of the Heroku Support channels. Any non-support-related issues or product feedback is welcome at support@moesif.com.