Skip to main content

Moesif Middleware to automatically log API calls from GCP Cloud functions

Project description

Moesif GCP Function Middleware

Built For Software License Source Code

Middleware (Python) to automatically log API calls from GCP Cloud functions and sends to Moesif for API analytics and log analysis.

Designed for APIs that are hosted on GCP Cloud functions using Google API Gateway as a trigger.

How to install

pip install moesif_gcp_function

How to use

1. Add middleware to your Cloud function application.

from moesif_gcp_function.middleware import MoesifLogger
import functions_framework

moesif_options = {
    'LOG_BODY': True
}

@MoesifLogger(moesif_options)
@functions_framework.http
def hello_get(request):
  return "Hello World!"

2. Set MOESIF_APPLICATION_ID environment variable

Add a new environment variable with the name MOESIF_APPLICATION_ID and the value being your Moesif application id, which 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.

3. Trigger your API

Grab the URL to your API Gateway and make some calls using a tool like Postman or CURL.

Optional: Capturing outgoing API calls

If you want to capture all outgoing API calls from your Python Cloud function to third parties like Stripe or to your own dependencies, call start_capture_outgoing() to start capturing. This mechanism works by patching the Requests

from moesif_gcp_function.middleware import *
start_capture_outgoing(moesif_options) # moesif_options are the configuration options.

Configuration options

IDENTIFY_USER

Type: (request, response) => String

IDENTIFY_USER is a function that takes Flask request and response objects as arguments and returns a user_id. This enables Moesif to attribute API requests to individual unique users so you can understand who calling your API. This can be used simultaneously with IDENTIFY_COMPANY to track both individual customers and the companies their a part of.

def identify_user(request, response):
  # your code here, must return a string
  return 

IDENTIFY_COMPANY

Type: (request, response) => String

IDENTIFY_COMPANY is a function that takes Flask request and response objects as arguments and returns a company_id. If your business is B2B, this enables Moesif to attribute API requests to specific companies or organizations so you can understand which accounts are calling your API. This can be used simultaneously with IDENTIFY_USER to track both individual customers and the companies their a part of.

def identify_company(request, response):
  # your code here, must return a string
  return '7890'
}

GET_SESSION_TOKEN

Type: (request, response) => String

GET_SESSION_TOKEN a function that takes Flask request and response objects as arguments and returns a session token (i.e. such as an API key).

def get_session_token(request, response):
    # your code here, must return a string.
    return 'XXXXXXXXX'

GET_API_VERSION

Type: (request, response) => String

GET_API_VERSION is a function that takes Flask request and response objects as arguments and returns a string to tag requests with a specific version of your API.

def get_api_version(request, response):
  # your code here. must return a string.
  return '1.0.0'

GET_METADATA

Type: (request, response) => dict

GET_METADATA is a function that takes Flask request and response objects as arguments and returns an object that allows you to add custom metadata that will be associated with the request. The metadata must be a simple python object that can be converted to JSON. For example, you may want to save a function_name, or a trace_id with the request.

def get_metadata(request, response):
  # your code here:
  return {
        'trace_id': "trace_id_XX",
        'function_name': "function_name"
    }

SKIP

Type: (request, response) => Boolean

SKIP is a function that takes Flask request and response objects as arguments and returns true if the event should be skipped (i.e. not logged)

def should_skip(request, response):
    # your code here. must return a boolean.
    return "skip" in request.url

MASK_EVENT_MODEL

Type: MoesifEventModel => MoesifEventModel

MASK_EVENT_MODEL is a function that takes the final Moesif event model as an argument before being sent to Moesif. With maskContent, you can make modifications to headers or body such as removing certain header or body fields.

def mask_event(eventmodel):
  # remove any field that you don't want to be sent to Moesif.
  return eventmodel

DEBUG

Type: Boolean

Set to true to print debug logs if you're having integration issues.

LOG_BODY

Type: Boolean

LOG_BODY is default to true, set to false to remove logging request and response body to Moesif.

Options for logging outgoing calls

The options below are applied to outgoing API calls. The request and response objects passed in are Requests request and Response response objects.

SKIP_OUTGOING

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

IDENTIFY_USER_OUTGOING

(optional, but highly recommended) (req, res) => string, a function that takes Requests 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) (req, res) => string, a function that takes Requests request and response, and returns a string that is the company id for this event.

GET_METADATA_OUTGOING

(optional) (req, res) => dictionary, a function that takes Requests 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) (req, res) => string, a function that takes Requests 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.

LOG_BODY_OUTGOING

(optional) boolean, default True, Set to False to remove logging request and response body.

Update User

Update A Single User

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 user_id field is required. For details, visit the Python API Reference.

from moesif_gcp_function.middleware import *

moesif_options = {
    'LOG_BODY': True,
    'DEBUG': True,
}

# Only user_id is required.
# 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
# metadata can be any custom object
user = {
  'user_id': '12345',
  'company_id': '67890', # If set, associate user with a company object
  'campaign': {
    'utm_source': 'google',
    'utm_medium': 'cpc',
    'utm_campaign': 'adwords',
    'utm_term': 'api+tooling',
    'utm_content': 'landing'
  },
  'metadata': {
    'email': 'john@acmeinc.com',
    'first_name': 'John',
    'last_name': 'Doe',
    'title': 'Software Engineer',
    'sales_info': {
        'stage': 'Customer',
        'lifetime_value': 24000,
        'account_owner': 'mary@contoso.com'
    },
  }
}

update_user(user, moesif_options)

Update Users in Batch

Similar to update_user, but used to update a list of users in one batch. Only the user_id field is required. For details, visit the Python API Reference.

from moesif_gcp_function.middleware import *

moesif_options = {
    'LOG_BODY': True,
    'DEBUG': True,
}

userA = {
  'user_id': '12345',
  'company_id': '67890', # If set, associate user with a company object
  'metadata': {
    'email': 'john@acmeinc.com',
    'first_name': 'John',
    'last_name': 'Doe',
    'title': 'Software Engineer',
    'sales_info': {
        'stage': 'Customer',
        'lifetime_value': 24000,
        'account_owner': 'mary@contoso.com'
    },
  }
}

userB = {
  'user_id': '54321',
  'company_id': '67890', # If set, associate user with a company object
  'metadata': {
    'email': 'mary@acmeinc.com',
    'first_name': 'Mary',
    'last_name': 'Jane',
    'title': 'Software Engineer',
    'sales_info': {
        'stage': 'Customer',
        'lifetime_value': 48000,
        'account_owner': 'mary@contoso.com'
    },
  }
}
update_users_batch([userA, userB], moesif_options)

Update Company

Update A Single Company

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 company_id field is required. For details, visit the Python API Reference.

from moesif_gcp_function.middleware import *

moesif_options = {
    'LOG_BODY': True,
    'DEBUG': True,
}

# Only company_id is required.
# 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
# metadata can be any custom object
company = {
  'company_id': '67890',
  'company_domain': 'acmeinc.com', # If domain is set, Moesif will enrich your profiles with publicly available info
  'campaign': {
    'utm_source': 'google',
    'utm_medium': 'cpc',
    'utm_campaign': 'adwords',
    'utm_term': 'api+tooling',
    'utm_content': 'landing'
  },
  'metadata': {
    'org_name': 'Acme, Inc',
    'plan_name': 'Free',
    'deal_stage': 'Lead',
    'mrr': 24000,
    'demographics': {
        'alexa_ranking': 500000,
        'employee_count': 47
    },
  }
}

update_company(company, moesif_options)

Update Companies in Batch

Similar to update_company, but used to update a list of companies in one batch. Only the company_id field is required. For details, visit the Python API Reference.

from moesif_gcp_function.middleware import *

moesif_options = {
    'LOG_BODY': True,
    'DEBUG': True,
}

companyA = {
  'company_id': '67890',
  'company_domain': 'acmeinc.com', # If domain is set, Moesif will enrich your profiles with publicly available info
  'metadata': {
    'org_name': 'Acme, Inc',
    'plan_name': 'Free',
    'deal_stage': 'Lead',
    'mrr': 24000,
    'demographics': {
        'alexa_ranking': 500000,
        'employee_count': 47
    },
  }
}

companyB = {
  'company_id': '09876',
  'company_domain': 'contoso.com', # If domain is set, Moesif will enrich your profiles with publicly available info
  'metadata': {
    'org_name': 'Contoso, Inc',
    'plan_name': 'Free',
    'deal_stage': 'Lead',
    'mrr': 48000,
    'demographics': {
        'alexa_ranking': 500000,
        'employee_count': 53
    },
  }
}

update_companies_batch([companyA, companyB], moesif_options)

Examples

Other integrations

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

moesif_gcp_function-0.0.1.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

moesif_gcp_function-0.0.1-py2.py3-none-any.whl (17.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file moesif_gcp_function-0.0.1.tar.gz.

File metadata

  • Download URL: moesif_gcp_function-0.0.1.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for moesif_gcp_function-0.0.1.tar.gz
Algorithm Hash digest
SHA256 67f283349708ccfaf009f51d5674edb9ce6ada711eeeff3772767e29244dfe62
MD5 dbeac5a9ee3799d6c893cae5e8b5ad32
BLAKE2b-256 7e2ce6c1569ca4025fb3a08b5a2d84e1bc55e3fee85ee4f055584553fe1a8d54

See more details on using hashes here.

File details

Details for the file moesif_gcp_function-0.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for moesif_gcp_function-0.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 395823d5b0e9c2acd737dd11d0baefd4156aa811b77597adce0458a1515f35c8
MD5 d88697e01000684b6799eb87ccbed18d
BLAKE2b-256 faee7e06b2f3ad7c9bcc347cdbbfbceff3f3caff8e3e9491511e9583ac4280b2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page