instrument

package
v0.64.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package instrument provides the tool to emit the events to the instrumentation destination. Currently it supports Segment or logger as a destination.

When enabling, make sure to follow the guideline specified in https://github.com/netlify/segment-events

In the config file, you can define the API key, as well as if it's enabled (use Segment) or not (use logger).

INSTRUMENT_ENABLED=true
INSTRUMENT_KEY=segment_api_key

To use, you can import this package:

import "github.com/netlify/netlify-commons/instrument"

You will likely need to import the Segment's analytics package as well, to create new traits and properties.

import "gopkg.in/segmentio/analytics-go.v3"

Then call the functions:

instrument.Track("userid", "service:my_event", analytics.NewProperties().Set("color", "green"))

For testing, you can create your own mock instrument and use it:

func TestSomething (t *testing.T) {
	log := testutil.TL(t)
	old := instrument.GetGlobalClient()
	t.Cleanup(func(){ instrument.SetGlobalClient(old) })
	instrument.SetGlobalClient(instrument.MockClient{Logger: log})
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alias

func Alias(previousID string, userID string) error

Alias sends an alias type message to a queue to be sent to Segment.

func Group

func Group(userID string, groupID string, traits analytics.Traits) error

Group sends a group type message to a queue to be sent to Segment.

func Identify

func Identify(userID string, traits analytics.Traits) error

Identify sends an identify type message to a queue to be sent to Segment.

func Init

func Init(conf Config, log logrus.FieldLogger) error

Init will initialize global client with a segment client

func Page

func Page(userID string, name string, properties analytics.Properties) error

Page sends a page type message to a queue to be sent to Segment.

func SetGlobalClient

func SetGlobalClient(client Client)

func Track

func Track(userID string, event string, properties analytics.Properties) error

Track sends a track type message to a queue to be sent to Segment.

Types

type Client

type Client interface {
	Identify(userID string, traits analytics.Traits) error
	Track(userID string, event string, properties analytics.Properties) error
	Page(userID string, name string, properties analytics.Properties) error
	Group(userID string, groupID string, traits analytics.Traits) error
	Alias(previousID string, userID string) error
}

func GetGlobalClient

func GetGlobalClient() Client

func NewClient

func NewClient(cfg *Config, logger logrus.FieldLogger) (Client, error)

type Config

type Config struct {
	// Write Key for the Segment source
	Key string `json:"key" yaml:"key"`
	// If this is false, instead of sending the event to Segment, emits verbose log to logger
	Enabled bool `json:"enabled" yaml:"enabled" default:"false"`
}

type MockClient

type MockClient struct {
	Logger logrus.FieldLogger
}

func (MockClient) Alias

func (c MockClient) Alias(previousID string, userID string) error

func (MockClient) Group

func (c MockClient) Group(userID string, groupID string, traits analytics.Traits) error

func (MockClient) Identify

func (c MockClient) Identify(userID string, traits analytics.Traits) error

func (MockClient) Page

func (c MockClient) Page(userID string, name string, properties analytics.Properties) error

func (MockClient) Track

func (c MockClient) Track(userID string, event string, properties analytics.Properties) error

Jump to

Keyboard shortcuts

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