Skip to content

Instantly share code, notes, and snippets.

@lmoorejc
Last active December 13, 2022 21:37
Show Gist options
  • Save lmoorejc/45ccae28fe945dcaf068bbe22abf7e0c to your computer and use it in GitHub Desktop.
Save lmoorejc/45ccae28fe945dcaf068bbe22abf7e0c to your computer and use it in GitHub Desktop.

Adding Event Queue

Table of Contents:

  1. Configuration
  2. Initialization
  3. Usage
  4. Best Practices

Configuration

Environment variables will need to be placed into a couple different (potential) places:

  1. app.yaml
  2. ci.env
  3. Any other .env files desired (such as custom workstation files)

Common-Go

It is best practice have a parent prefix key, such as EVENTS_ to avoid collisions with other potentially needed Queue configurations. The Event code in common-go uses a small handful of variables, The most important of which is the TYPE var which drives how the others are used. Those being LOG_PATH, STREAM_NAME, and QUEUE_URL

For the sake of configuring an SQS queue, we will be focusing on TYPE=QUEUE. When using the QUEUE type, we are also required to specify the QUEUE_URL variable.

app.yaml

Origin will automatically populate the needed variables by providing the following configuration in the app.yaml:

configuration:
  # Generate events
  - name: events
    type: events

Per the documentation, this defaults to a queue with the same name as the app, but we can customize this to push events into the desired queue by specifying the pipeline: <desired queue pipeline>, should it differ from the default.

ci.env

We can set defaults in code to avoid needing to specify them the .env files used by workstation and the CI builds. SCIM takes this approach, and defaults to FILE for the queue type. Example pull from the jumpcloud-scim defaults:

KeyEvents: map[string]interface{}{
	jcconfig.KeyEventType:    jcconfig.FileEventType,
	jcconfig.KeyEventLogPath: "/var/log/jumpcloud-events/scim-events.log",
},

It seems recommended to let our services default to a file for event logs, and configure it to push to a Queue in staging/production.

Usage

Loading the environment variables is done by passing a source into jcconfig.NewEvents(...), which will load the appropriate values and perform input verification.

With an EventsLoggerConfig, this can be passed in to jcevents.NewEventLogger(...) to get the full logger created.

From there, passing the logger to the needed places is all that's needed before logging events:

err = s.eventLogger.Log(...)
if err != nil {
	fmt.Printf("error logging event: %v", err)
}

Best Practices

We'll need to build out our generic map, or potentially have some sort of JSON object we build into the event body?

  • Do we want a dedicated object for a given event? Maybe that consistency would be good to have pinned down (as opposed to the free-form fun of the JS world?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment