Integration Guide - Go SDK

Built For Software License Source Code

Go Middleware that logs API Calls and sends to Moesif for API analytics and log analysis.

Source Code on GitHub

How to install

Run the following commands:

go get github.com/moesif/moesifmiddleware-go

How to use

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))

Optional: Capturing outgoing API calls

In addition to your own APIs, you can also start capturing calls out to third party services via the following method:

moesifmiddleware.StartCaptureOutgoing(moesifOption)

handler func(ResponseWriter, *Request)

(required), HandlerFunc registers the handler function for the given pattern.

moesifOption

(required), map[string]interface{}, are the configuration options for your application. Please find the details below on how to configure options.

Configuration options

Application_Id

(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.

Should_Skip

(optional) (request, response) => boolean, a function that takes a request and a response, and returns true if you want to skip this particular event.

Identify_User

(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.

Identify_Company

(optional) (request, response) => string, a function that takes a request and response, and returns a string that is the company id for this event.

Get_Metadata

(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.

Get_Session_Token

(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.

Mask_Event_Model

(optional) (EventModel) => EventModel, a function that takes an EventModel and returns an EventModel with desired data removed. The return value must be a valid EventModel required by Moesif data ingestion API. For details regarding EventModel please see the Moesif Golang API Documentation.

Debug

(optional) boolean, a flag to see debugging messages.

Log_Body

(optional) boolean, Default true. Set to false to remove logging request and response body to Moesif.

Capture_Outoing_Requests

(optional) boolean, Default False. Set to True to capture all outgoing API calls from your app to third parties like Stripe or to your own dependencies while using net/http package. The options below is applied to outgoing API calls. When the request is outgoing, for options functions that take request and response as input arguments, the request and response objects passed in are Request request and Response response objects.

Should_Skip_Outgoing

(optional) (request, response) => boolean, a function that takes a request and response, and returns true if you want to skip this particular event.

Identify_User_Outgoing

(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.

Identify_Company_Outgoing

(optional) (request, response) => string, a function that takes request and response, and returns a string that is the company id for this event.

Get_Metadata_Outgoing

(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.

Get_Session_Token_Outgoing

(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.

Mask_Event_Model_Outgoing

(optional) (EventModel) => EventModel, a function that takes an EventModel and returns an EventModel with desired data removed. The return value must be a valid EventModel required by Moesif data ingestion API. For details regarding EventModel please see the Moesif Golang API Documentation.

Log_Body_Outgoing

(optional) boolean, Default true. Set to false to remove logging request and response body to Moesif.

Update User

UpdateUser method

A method is attached to the moesif middleware object to update the user profile or metadata. The metadata field can be any custom data you want to set on the user. The UserId field is required.

import (
	moesifmiddleware "github.com/moesif/moesifmiddleware-go"
)

var moesifOptions = map[string]interface{} {
	"Application_Id": "Your Moesif Application Id",
	"Log_Body": true,
}

// Modified Time
modifiedTime := time.Now().UTC()

// User Metadata
metadata := map[string]interface{}{
	"email": "johndoe1@acmeinc.com",
	"Key1": "metadata",
	"Key2": 42,
	"Key3": map[string]interface{}{
		"Key3_1": "SomeValue",
	},
}

// Prepare user model
user := models.UserModel{
	ModifiedTime: 	  &modifiedTime,
	SessionToken:     nil,
	IpAddress:		  nil,
	UserId:			  "golangapiuser",	
	UserAgentString:  nil,
	Metadata:		  &metadata,
}

// Update User
moesifmiddleware.UpdateUser(&user, moesifOption)

UpdateUsersBatch method

A method is attached to the moesif middleware object to update the users profile or metadata in batch. The metadata field can be any custom data you want to set on the user. The UserId field is required.


import (
	moesifmiddleware "github.com/moesif/moesifmiddleware-go"
)

var moesifOptions = map[string]interface{} {
	"Application_Id": "Moesif Application Id",
}

// Batch Users
var users []*models.UserModel

// Modified Time
modifiedTime := time.Now().UTC()

// User Metadata
metadata := map[string]interface{}{
	"email": "johndoe1@acmeinc.com",
	"Key1": "metadata",
	"Key2": 42,
	"Key3": map[string]interface{}{
		"Key3_1": "SomeValue",
	},
}

// Prepare user model
userA := models.UserModel{
	ModifiedTime: 	  &modifiedTime,
	SessionToken:     nil,
	IpAddress:		  nil,
	UserId:			  "golangapiuser",	
	UserAgentString:  nil,
	Metadata:		  &metadata,
}

users = append(users, &userA)

// Update User
moesifmiddleware.UpdateUsersBatch(users, moesifOption)

Update Company

UpdateCompany method

A method is attached to the moesif middleware object to update the company profile or metadata. The metadata field can be any custom data you want to set on the company. The CompanyId field is required.

import (
	moesifmiddleware "github.com/moesif/moesifmiddleware-go"
)

var moesifOptions = map[string]interface{} {
	"Application_Id": "Moesif Application Id",
}

// Modified Time
modifiedTime := time.Now().UTC()

// User Metadata
metadata := map[string]interface{}{
	"email": "johndoe1@acmeinc.com",
	"Key1": "metadata",
	"Key2": 42,
	"Key3": map[string]interface{}{
		"Key3_1": "SomeValue",
	},
}

// Prepare company model
company := models.CompanyModel{
	ModifiedTime: 	  &modifiedTime,
	SessionToken:     nil,
	IpAddress:		  nil,
	CompanyId:		  "1",	
	CompanyDomain:    nil,
	Metadata:		  &metadata,
}

// Update Company
moesifmiddleware.UpdateCompany(&company, moesifOption)

UpdateCompaniesBatch method

A method is attached to the moesif middleware object to update the companies profile or metadata in batch. The metadata field can be any custom data you want to set on the company. The CompanyId field is required.


import (
	moesifmiddleware "github.com/moesif/moesifmiddleware-go"
)

var moesifOptions = map[string]interface{} {
	"Application_Id": "Moesif Application Id",
}

// Batch Companies
var companies []*models.CompanyModel

// Modified Time
modifiedTime := time.Now().UTC()

// Company Metadata
metadata := map[string]interface{}{
	"email": "johndoe1@acmeinc.com",
	"Key1": "metadata",
	"Key2": 42,
	"Key3": map[string]interface{}{
		"Key3_1": "SomeValue",
	},
}

// Prepare company model
companyA := models.CompanyModel{
	ModifiedTime: 	  &modifiedTime,
	SessionToken:     nil,
	IpAddress:		  nil,
	CompanyId:		  "1",	
	CompanyDomain:    nil,
	Metadata:		  &metadata,
}

companies = append(companies, &companyA)

// Update Companies
moesifmiddleware.UpdateCompaniesBatch(companies, moesifOption)

Example

An example app with Moesif integration is available on GitHub.

Other integrations

To view more more documentation on integration options, please visit the Integration Options Documentation.