Skip to main content

Small Python package to add GCP metadata to pub/sub messages

Project description

CodeFactor

Description

Gobits is a very small module, which acts as a metadata carrier within an event-driven architecture on the Google Cloud Platform (GCP). It automatically adds fields that may be useful downstream to determine the origin of a message.

Usage

Class attributes:

class Gobits:
    """
    A small class that gathers information (bits) for a pub/sub message payload.

    Attributes:
        _request                Holds information about the cloud function request.
        _context                Holds information about the cloud function context.
        envelope                The envelope containing a pub/sub message.
        message                 The pub/sub message to be processed.
        processed               Time of processing (UTC).
        gcp_project             The source GCP project.
        execution_id            The id of the execution.
        execution_type          Type of the service processing the message.
        execution_trigger_type  Type of the trigger invoking the processor.
        function_name           Name of the processing cloud function.
        function_version        Version of the processing cloud function.
        event_id                The id of the trigger event.
        message_id              The pub/sub message id.
        message_publish_time    Time of publishing to pub/sub (UTC).
        source_subscription     The originating pub/sub subscription.
    """

Cloud function with HTTP trigger:

from gobits import Gobits


def handler(request):

    metadata = Gobits.from_request(request=request)

    message = {
      'gobits': [metadata.to_json()],
      'data': []
    }

Cloud function with other trigger:

from gobits import Gobits


def handler(data, context):

    metadata = Gobits.from_context(context=context)

    message = {
      'gobits': [metadata.to_json()],
      'data': []
    }

Testing

Sometimes you want to test a cloud function locally. Be sure to pass mock objects containing the correct fields (if applicable).

from main import handler
from unittest.mock import Mock

mock_data = Mock()
mock_context = Mock()
mock_context.event_id = "12345678"

handler(mock_data, mock_context)

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

gobits-1.0.10.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

gobits-1.0.10-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file gobits-1.0.10.tar.gz.

File metadata

  • Download URL: gobits-1.0.10.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for gobits-1.0.10.tar.gz
Algorithm Hash digest
SHA256 59d29811e61117c9d738a6d1d1d4697edce3844d44ccbb63c49be58e38e60c15
MD5 9af048a9959681bc6dcf44391e1a88d0
BLAKE2b-256 0a12ad7d4708f286e575e2023e97e7d2abc022f5089e6628204a12a513195367

See more details on using hashes here.

File details

Details for the file gobits-1.0.10-py3-none-any.whl.

File metadata

  • Download URL: gobits-1.0.10-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for gobits-1.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 65355a894d127c3a52a98f732b1da054d0e68ecac139c652d1e50b1f99043ff3
MD5 85da60bb96f7a87fbeb23a1d804dbce6
BLAKE2b-256 47a6303ed3ecf6567bb41c9dd3e6781214aa8c501d9f3145e9924d74d2d3a7a4

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