Skip to main content

Moesif Middleware for AIOHTTP

Built For Latest Version Language Versions Software License Source Code

AIOHTTP middleware that automatically logs incoming API calls and sends to Moesif for API analytics and monitoring.

Source Code on GitHub

How to install

pip install moesif_aiohttp

Configuration options

For options that use the request and response as input arguments, these use the aiohttp web request or response objects.

Please note that incase of the streaming api, the response object is aiohttp_sse.EventSourceResponse

APPLICATION_ID

(required), string, is obtained via your Moesif Account, this is required.

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 a 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 a response, and returns a string that is the company id for this event.

GET_METADATA

(optional) (request, response) => dictionary, getMetadata is a function that returns an object that allows you to add custom metadata that will be associated with the event. The metadata must be a dictionary that can be converted to JSON. 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 a 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

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

EVENT_QUEUE_SIZE

(optional) int, default 1000000, the maximum number of event objects queued in memory pending upload to Moesif. If the queue is full additional calls to MoesifMiddleware will return immediately without logging the event, so this number should be set based on the expected event size and memory capacity

EVENT_WORKER_COUNT

(optional) int, default 2, the number of worker threads to use for uploading events to Moesif. If you have a large number of events being logged, increasing this number can improve upload performance.

BATCH_SIZE

(optional) int, default 100, Maximum batch size when sending events to Moesif when reading from the queue

EVENT_BATCH_TIMEOUT

(optional) int, default 2, Maximum time in seconds to wait before sending a batch of events to Moesif when reading from the queue

AUTHORIZATION_HEADER_NAME

(optional) string, A request header field name used to identify the User in Moesif. Default value is authorization. Also, supports a comma separated string. We will check headers in order like "X-Api-Key,Authorization".

AUTHORIZATION_USER_ID_FIELD

(optional) string, A field name used to parse the User from authorization header in Moesif. Default value is sub.

BASE_URI

(optional) string, A local proxy hostname when sending traffic via secure proxy. Please set this field when using secure proxy. For more details, refer secure proxy documentation.

Example:

def identify_user(request, response):
    # Your custom code that returns a user id string
    return "12345"

def identify_company(request, response):
    # Your custom code that returns a company id string
    return "67890"

def should_skip(request, response):
    # Your custom code that returns true to skip logging
    return "health/probe" in request.url

def get_token(request, response):
    # If you don't want to use the standard WSGI session token,
    # add your custom code that returns a string for session/API token
    return "XXXXXXXXXXXXXX"

def mask_event(eventmodel):
    # Your custom code to change or remove any sensitive fields
    if 'password' in eventmodel.response.body:
        eventmodel.response.body['password'] = None
    return eventmodel

def get_metadata(app, environ):
    return {
        'datacenter': 'westus',
        'deployment_version': 'v1.2.3',
    }

moesif_settings = {
    'APPLICATION_ID': 'Your Moesif Application Id',
    'DEBUG': False,
    'LOG_BODY': True,
    'IDENTIFY_USER': identify_user,
    'IDENTIFY_COMPANY': identify_company,
    'GET_SESSION_TOKEN': get_token,
    'SKIP': should_skip,
    'MASK_EVENT_MODEL': mask_event,
    'GET_METADATA': get_metadata,
    'CAPTURE_OUTGOING_REQUESTS': False
}

app = web.Application(
        middlewares=[MoesifMiddleware(moesif_settings)],
    )

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.

moesif_settings = {
    'APPLICATION_ID': 'Your Moesif Application Id',
}

# 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 = MoesifMiddleware(moesif_settings).update_user(user)

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.

moesif_settings = {
    'APPLICATION_ID': 'Your Moesif Application Id',
}

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 = MoesifMiddleware(moesif_settings).update_users_batch([userA, userB])

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.

moesif_settings = {
    'APPLICATION_ID': 'Your Moesif Application Id',
}

# 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 = MoesifMiddleware(moesif_settings).update_company(company)

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.

moesif_settings = {
    'APPLICATION_ID': 'Your Moesif Application Id',
}

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 = MoesifMiddleware(moesif_settings).update_companies_batch([companyA, companyB])

Other integrations

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

Metadata

Release files for moesif-aiohttp 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for moesif-aiohttp 1.1.0
File Size Uploaded Provenance Actions
moesif_aiohttp-1.1.0.tar.gz 18.4 kB Without provenance Details for moesif_aiohttp-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for moesif-aiohttp 1.1.0
File Interpreter ABI Platform Actions
moesif_aiohttp-1.1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details for moesif_aiohttp-1.1.0-py2.py3-none-any.whl

Total release size: 39.3 kB

Release files / moesif_aiohttp-1.1.0.tar.gz

Download URL moesif_aiohttp-1.1.0.tar.gz
Size 18.4 kB
Tags Source
SHA-256 checksum
How to use checksums
220f6ba30435fc50d2f33c0cfbc5ab95119a55d582f8d0452983cc91eda2b0be
BLAKE2b-256 checksum
How to use checksums
444ed73b3e19d57b23961686d929ab27f743b16c0e23f5044ed472ecde54ed00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / moesif_aiohttp-1.1.0-py2.py3-none-any.whl

Download URL moesif_aiohttp-1.1.0-py2.py3-none-any.whl
Size 20.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
950260791ef6627e8e8f44e0450ea63513be51b7e1f343513e223052f860a34a
BLAKE2b-256 checksum
How to use checksums
7c55c890c57f8e2a8df1b61e15fc51d5e78ef846df3a50c17e6e6eadbab6e01c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Security for moesif-aiohttp 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page