event

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2022 License: MPL-2.0 Imports: 12 Imported by: 7

Documentation

Overview

Package event exposes a suite of utilities to populate Temporal workflows and activities of this ecosystem. Every inputs have a Context to provide useful context about an event.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {

	// Meta is free key-value dictionary related to the event.
	Meta map[string]any `json:"meta,omitempty"`

	// Flags represent feature flags leveraged by the client executing the event.
	Flags map[string]bool `json:"flags,omitempty"`

	// Params are additional parameters than can be passed to and apply by a
	// specification or an integration.
	Params url.Values `json:"params,omitempty"`

	// Library holds the details of the SDK used by the client executing the event.
	//
	// Note: This should be automatically populated using WithLibrary:
	//
	//   NewContext(client, WithLibrary())
	//
	Library *ContextLibrary `json:"library,omitempty"`

	// Git holds the details of the Git commit of the project from which the client
	// is executing the event.
	//
	// Note: This should be automatically populated using WithGit:
	//
	//   NewContext(client, WithGit())
	//
	Git *ContextGit `json:"git,omitempty"`

	// App holds the details about the application executing the event.
	App *ContextApp `json:"app,omitempty"`

	// Cloud holds the details about the cloud provider from which the event is
	// executed.
	Cloud *ContextCloud `json:"cloud,omitempty"`

	// Subscription holds the details about the account/customer for which the
	// event has been trigger.
	Subscription ContextSubscription `json:"subscription,omitempty"`

	// Campaign holds the details about the marketing campaign from which the event
	// is triggered from.
	Campaign ContextCampaign `json:"campaign,omitempty"`

	// Referrer holds the details about the marketing referrer from which the event
	// is triggered from.
	Referrer ContextReferrer `json:"referrer,omitempty"`

	// Device holds the details about the user's device.
	Device ContextDevice `json:"device,omitempty"`

	// OS holds the details about the user's OS.
	OS ContextOS `json:"os,omitempty"`

	// Location holds the details about the user's location.
	Location ContextLocation `json:"location,omitempty"`

	// Network holds the details about the user's network.
	Network ContextNetwork `json:"network,omitempty"`

	// Page holds the details about the webpage from which the event is triggered
	// from.
	Page ContextPage `json:"page,omitempty"`

	// Screen holds the details about the app's screen from which the event is
	// triggered from.
	Screen ContextScreen `json:"screen,omitempty"`

	// IP is the current user’s IP address.
	IP net.IP `json:"ip,omitempty"`

	// Locale is the locale string for the current user.
	//
	// Example: "en-US"
	Locale string `json:"locale,omitempty"`

	// Timezone is the user timezone information.
	//
	// Example: "America/New_York"
	Timezone string `json:"timezone,omitempty"`

	// UserAgent is the user agent of the device executing the event.
	UserAgent string `json:"user_agent,omitempty"`
}

Context is a dictionary of extra information that provides useful context about an event.

The context is always present in the inputs of workflows and activities. However, it is never included in the outputs. This design allows activity to not alter the context from one activity to another.

Pointer fields are not event-specific and should be set only once per process:

gctx, _ := NewContext(client, WithLibrary(), WithGit())
gctx.Cloud = &event.ContextCloud{
  // ...
}

Non-pointer fields are event-specific and should be merged with global ones when executing a workflow or activity from a client:

gctx, _ := NewContext(client, WithLibrary(), WithGit())
client.ExecuteWorkflow(ctx, opts, "workflow", AnyInput{
  Context: gctx.MergeWith(event.Context{
    // ...
  }),
})

This is heavily inspired by the following references, and was adapted to better fit this ecosystem:

func NewContext

func NewContext(client client.Client, attachments ...With) (Context, error)

NewContext creates a new context, populated with attachments if desired. This context could then be used in the Temporal workflows and activities' input.

func (Context) MergeWith

func (ctx Context) MergeWith(input Context) Context

MergeWith returns a new merged Context.

type ContextApp

type ContextApp struct {
	Name    string `json:"name,omitempty"`
	Version string `json:"version,omitempty"`
	BuildID string `json:"build_id,omitempty"`
}

ContextApp holds the details about the client application executing the event.

type ContextCampaign

type ContextCampaign struct {
	Name    string `json:"name,omitempty"`
	Source  string `json:"source,omitempty"`
	Medium  string `json:"medium,omitempty"`
	Term    string `json:"term,omitempty"`
	Content string `json:"content,omitempty"`
}

ContextCampaign holds the details about the marketing campaign from which a client is executing the event from.

type ContextCloud

type ContextCloud struct {
	Provider  string `json:"provider,omitempty"`
	Service   string `json:"service,omitempty"`
	Region    string `json:"region,omitempty"`
	ProjectID string `json:"project_id,omitempty"`
	AccountID string `json:"account_id,omitempty"`
}

ContextCloud holds the details about the cloud provider from which the client is executing the event.

type ContextDevice

type ContextDevice struct {
	ID            string `json:"id,omitempty"`
	Manufacturer  string `json:"manufacturer,omitempty"`
	Model         string `json:"model,omitempty"`
	Name          string `json:"name,omitempty"`
	Type          string `json:"type,omitempty"`
	Version       string `json:"version,omitempty"`
	AdvertisingID string `json:"advertising_id,omitempty"`
}

ContextDevice holds the details about the user's device.

type ContextGit

type ContextGit struct {
	Branch    string    `json:"branch,omitempty"`
	Hash      string    `json:"hash,omitempty"`
	Message   string    `json:"message,omitempty"`
	Timestamp time.Time `json:"timestamp,omitempty"`
}

ContextGit holds the details of the Git commit of the project from which the client is executing the event.

type ContextLibrary

type ContextLibrary struct {
	Name    string `json:"name,omitempty"`
	Version string `json:"version,omitempty"`
}

ContextLibrary holds the details of the SDK used by the client executing the event.

type ContextLocation

type ContextLocation struct {
	City      string  `json:"city,omitempty"`
	Country   string  `json:"country,omitempty"`
	Region    string  `json:"region,omitempty"`
	Latitude  float64 `json:"latitude,omitempty"`
	Longitude float64 `json:"longitude,omitempty"`
	Speed     float64 `json:"speed,omitempty"`
}

ContextLocation holds the details about the user's location.

type ContextNetwork

type ContextNetwork struct {
	Bluetooth bool   `json:"bluetooth,omitempty"`
	Cellular  bool   `json:"cellular,omitempty"`
	WIFI      bool   `json:"wifi,omitempty"`
	Carrier   string `json:"carrier,omitempty"`
}

ContextNetwork holds the details about the user's network.

type ContextOS

type ContextOS struct {
	Name    string `json:"name,omitempty"`
	Arch    string `json:"arch,omitempty"`
	Version string `json:"version,omitempty"`
}

ContextOS holds the details about the user's OS or the current runtime if automatically populated via WithOS().

type ContextPage

type ContextPage struct {
	Path     string `json:"path,omitempty"`
	Referrer string `json:"referrer,omitempty"`
	Search   string `json:"search,omitempty"`
	Title    string `json:"title,omitempty"`
	URL      string `json:"url,omitempty"`
}

ContextPage holds the details about the webpage from which the event is triggered from.

type ContextReferrer

type ContextReferrer struct {
	Type string `json:"type,omitempty"`
	Name string `json:"name,omitempty"`
	URL  string `json:"url,omitempty"`
	Link string `json:"link,omitempty"`
}

ContextReferrer holds the details about the marketing referrer from which a client is executing the event from.

type ContextScreen

type ContextScreen struct {
	Density int `json:"density,omitempty"`
	Width   int `json:"width,omitempty"`
	Height  int `json:"height,omitempty"`
}

ContextScreen holds the details about the app's screen from which the event is triggered from.

type ContextSubscription

type ContextSubscription struct {
	AccountID      string  `json:"account_id,omitempty"`
	SubscriptionID string  `json:"subscription_id,omitempty"`
	ProductID      string  `json:"product_id,omitempty"`
	Event          string  `json:"event,omitempty"`
	Value          string  `json:"value,omitempty"`
	IncrementBy    float32 `json:"increment_by,omitempty"`
}

ContextSubscription holds the details about the account/customer from which the event has been trigger. It's useful for tracking API usage of customers, such as in a SaaS application.

type With

type With func(Context) error

With allows to add defaults to a context that could then be used in a worker process as a "global context" across all specifications and integrations.

For example, one might want to populate all calls with Git info:

func NewContext(client, WithGit())

func WithGit

func WithGit() With

WithGit populates a Context with information related to the Git commit of the application, which is found in the ".git" directory. This is used when creating a "global context":

func NewContext(client, WithGit())

func WithLibrary

func WithLibrary() With

WithLibrary populates a Context with information related to the Temporal's client in use. This is used when creating a "global context":

func NewContext(client, WithLibrary())

func WithOS added in v0.11.0

func WithOS() With

WithOS populates a Context with information related to the OS calling this function. This is used when creating a "global context":

func NewContext(client, WithOS())

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL