Go Middleware that logs API Calls and sends to Moesif for API analytics and log analysis.
Run the following commands:
moesifmiddleware-go
can be installed like any other Go library through go get:
go get github.com/moesif/moesifmiddleware-go
Or, if you are already using Go Modules, specify a version number as well:
go get github.com/moesif/moesifmiddleware-go@v1.2.3
Add middleware to your application.
import(
moesifmiddleware "github.com/moesif/moesifmiddleware-go"
)
func handle(w http.ResponseWriter, r *http.Request) {
// Your API Logic
}
var moesifOptions = map[string]interface{} {
"Application_Id": "Your Moesif Application Id",
"Log_Body": true,
}
http.Handle("/api", moesifmiddleware.MoesifMiddleware(http.HandlerFunc(handle), moesifOption))
Your Moesif Application Id can be found in the Moesif Portal. After signing up for a Moesif account, your Moesif Application Id will be displayed during the onboarding steps.
In addition to your own APIs, you can also start capturing calls out to third party services via the following method:
moesifmiddleware.StartCaptureOutgoing(moesifOption)
(required), HandlerFunc registers the handler function for the given pattern.
(required), map[string]interface{}, are the configuration options for your application. Please find the details below on how to configure options.
(required), string, is obtained via your Moesif Account, this is required. Your Moesif Application Id can be found in the Moesif Portal. After signing up for a Moesif account, your Moesif Application Id will be displayed during the onboarding steps.
You can always find your Moesif Application Id at any time by logging into the Moesif Portal, click on the top right menu, and then clicking Installation.
(optional) (request, response) => boolean, a function that takes a request and a response, and returns true if you want to skip this particular event.
(optional, but highly recommended) (request, response) => string, a function that takes a request and response, and returns a string that is the user id used by your system. While Moesif tries to identify users automatically, but different frameworks and your implementation might be very different, it would be helpful and much more accurate to provide this function.
(optional) (request, response) => string, a function that takes a request and response, and returns a string that is the company id for this event.
(optional) (request, response) => dictionary, a function that takes a request and response, and returns a dictionary (must be able to be encoded into JSON). This allows you to associate this event with custom metadata. For example, you may want to save a VM instance_id, a trace_id, or a tenant_id with the request.
(optional) (request, response) => string, a function that takes a request and response, and returns a string that is the session token for this event. Moesif tries to get the session token automatically, but if this doesn't work for your service, you should use this to identify sessions.
(optional) () => []string, a function that returns array of strings to mask specific request header fields.
(optional) () => []string, a function that returns array of strings to mask specific request body fields.
(optional) () => []string, a function that returns array of strings to mask specific response header fields.
(optional) () => []string, a function that returns array of strings to mask specific response body fields.
(optional) boolean, a flag to see debugging messages.
(optional) boolean, Default true. Set to false to remove logging request and response body to Moesif.
(optional) int, An optional field name which specify the maximum number of events to hold in queue before sending to Moesif. In case of network issues when not able to connect/send event to Moesif, skips adding new events to the queue to prevent memory overflow. Default value is 10000
.
(optional) int, An optional field name which specify the maximum batch size when sending to Moesif. Default value is 200
.
(optional) int, An optional field which specifies a time (every n seconds) how often background thread runs to send events to moesif. Default value is 2
seconds.
The options below are applied to outgoing API calls. The request and response objects passed in are Request request and Response response objects.
(optional) (request, response) => boolean, a function that takes a request and response, and returns true if you want to skip this particular event.
(optional, but highly recommended) (request, response) => string, a function that takes request and response, and returns a string that is the user id used by your system. While Moesif tries to identify users automatically, but different frameworks and your implementation might be very different, it would be helpful and much more accurate to provide this function.
(optional) (request, response) => string, a function that takes request and response, and returns a string that is the company id for this event.
(optional) (request, response) => dictionary, a function that takes request and response, and returns a dictionary (must be able to be encoded into JSON). This allows to associate this event with custom metadata. For example, you may want to save a VM instance_id, a trace_id, or a tenant_id with the request.
(optional) (request, response) => string, a function that takes request and response, and returns a string that is the session token for this event. Again, Moesif tries to get the session token automatically, but if you setup is very different from standard, this function will be very help for tying events together, and help you replay the events.
(optional) boolean, Default true. Set to false to remove logging request and response body to Moesif.
Create or update a user profile in Moesif.
The metadata field can be any customer demographic or other info you want to store.
Only the UserId
field is required.
This method is a convenient helper that calls the Moesif API lib.
For details, visit the Go API Reference.
import (
moesifmiddleware "github.com/moesif/moesifmiddleware-go"
)
func literalFieldValue(value string) *string {
return &value
}
var moesifOptions = map[string]interface{} {
"Application_Id": "Your Moesif Application Id",
"Log_Body": true,
}
// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#users for campaign schema
campaign := models.CampaignModel {
UtmSource: literalFieldValue("google"),
UtmMedium: literalFieldValue("cpc"),
UtmCampaign: literalFieldValue("adwords"),
UtmTerm: literalFieldValue("api+tooling"),
UtmContent: literalFieldValue("landing"),
}
// metadata can be any custom dictionary
metadata := map[string]interface{}{
"email": "john@acmeinc.com",
"first_name": "John",
"last_name": "Doe",
"title": "Software Engineer",
"sales_info": map[string]interface{}{
"stage": "Customer",
"lifetime_value": 24000,
"account_owner": "mary@contoso.com",
},
}
// Only UserId is required
user := models.UserModel{
UserId: "12345",
CompanyId: literalFieldValue("67890"), // If set, associate user with a company object
Campaign: &campaign,
Metadata: &metadata,
}
// Update User
moesifmiddleware.UpdateUser(&user, moesifOption)
Similar to UpdateUser, but used to update a list of users in one batch.
Only the UserId
field is required.
This method is a convenient helper that calls the Moesif API lib.
For details, visit the Go API Reference.
import (
moesifmiddleware "github.com/moesif/moesifmiddleware-go"
)
func literalFieldValue(value string) *string {
return &value
}
var moesifOptions = map[string]interface{} {
"Application_Id": "Your Moesif Application Id",
}
// List of Users
var users []*models.UserModel
// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#users for campaign schema
campaign := models.CampaignModel {
UtmSource: literalFieldValue("google"),
UtmMedium: literalFieldValue("cpc"),
UtmCampaign: literalFieldValue("adwords"),
UtmTerm: literalFieldValue("api+tooling"),
UtmContent: literalFieldValue("landing"),
}
// metadata can be any custom dictionary
metadata := map[string]interface{}{
"email": "john@acmeinc.com",
"first_name": "John",
"last_name": "Doe",
"title": "Software Engineer",
"sales_info": map[string]interface{}{
"stage": "Customer",
"lifetime_value": 24000,
"account_owner": "mary@contoso.com",
},
}
// Only UserId is required
userA := models.UserModel{
UserId: "12345",
CompanyId: literalFieldValue("67890"), // If set, associate user with a company object
Campaign: &campaign,
Metadata: &metadata,
}
users = append(users, &userA)
// Update User
moesifmiddleware.UpdateUsersBatch(users, moesifOption)
Create or update a company profile in Moesif.
The metadata field can be any company demographic or other info you want to store.
Only the CompanyId
field is required.
This method is a convenient helper that calls the Moesif API lib.
For details, visit the Go API Reference.
import (
moesifmiddleware "github.com/moesif/moesifmiddleware-go"
)
func literalFieldValue(value string) *string {
return &value
}
var moesifOptions = map[string]interface{} {
"Application_Id": "Your Moesif Application Id",
}
// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#update-a-company for campaign schema
campaign := models.CampaignModel {
UtmSource: literalFieldValue("google"),
UtmMedium: literalFieldValue("cpc"),
UtmCampaign: literalFieldValue("adwords"),
UtmTerm: literalFieldValue("api+tooling"),
UtmContent: literalFieldValue("landing"),
}
// metadata can be any custom dictionary
metadata := map[string]interface{}{
"org_name": "Acme, Inc",
"plan_name": "Free",
"deal_stage": "Lead",
"mrr": 24000,
"demographics": map[string]interface{}{
"alexa_ranking": 500000,
"employee_count": 47,
},
}
// Prepare company model
company := models.CompanyModel{
CompanyId: "67890", // The only required field is your company id
CompanyDomain: literalFieldValue("acmeinc.com"), // If domain is set, Moesif will enrich your profiles with publicly available info
Campaign: &campaign,
Metadata: &metadata,
}
// Update Company
moesifmiddleware.UpdateCompany(&company, moesifOption)
Similar to UpdateCompany, but used to update a list of companies in one batch.
Only the CompanyId
field is required.
This method is a convenient helper that calls the Moesif API lib.
For details, visit the Go API Reference.
import (
moesifmiddleware "github.com/moesif/moesifmiddleware-go"
)
func literalFieldValue(value string) *string {
return &value
}
var moesifOptions = map[string]interface{} {
"Application_Id": "Your Moesif Application Id",
}
// List of Companies
var companies []*models.CompanyModel
// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#update-a-company for campaign schema
campaign := models.CampaignModel {
UtmSource: literalFieldValue("google"),
UtmMedium: literalFieldValue("cpc"),
UtmCampaign: literalFieldValue("adwords"),
UtmTerm: literalFieldValue("api+tooling"),
UtmContent: literalFieldValue("landing"),
}
// metadata can be any custom dictionary
metadata := map[string]interface{}{
"org_name": "Acme, Inc",
"plan_name": "Free",
"deal_stage": "Lead",
"mrr": 24000,
"demographics": map[string]interface{}{
"alexa_ranking": 500000,
"employee_count": 47,
},
}
// Prepare company model
companyA := models.CompanyModel{
CompanyId: "67890", // The only required field is your company id
CompanyDomain: literalFieldValue("acmeinc.com"), // If domain is set, Moesif will enrich your profiles with publicly available info
Campaign: &campaign,
Metadata: &metadata,
}
companies = append(companies, &companyA)
// Update Companies
moesifmiddleware.UpdateCompaniesBatch(companies, moesifOption)
- An example app with Moesif integration is available on GitHub.
- An example for Google Cloud Platform Serverless: Google Cloud Functions
To view more documentation on integration options, please visit the Integration Options Documentation.