analytics

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2023 License: MIT Imports: 13 Imported by: 8

README

analytics

This repository contains product analytics as defined in RFD#8.

Events

All events emitted are listed in the below table. Discussion of these is welcomed on RFD#8.

Name Emitted When Example Data
cf:groupidentify:deployment Deployment updated ./fixtures/cf-groupidentify-deployment.json
cf:identify:user_info Access Request created/updated ./fixtures/cf-identify-user-info.json
cf:request.created Access Request was created ./fixtures/cf-request-created.json
cf:request.reviewed Access Request was reviewed ./fixtures/cf-request-reviewed.json
cf:request.revoked Access Request was revoked ./fixtures/cf-request-revoked.json
cf:rule.archived Access Rule was archived ./fixtures/cf-rule-archived.json
cf:rule.created Access Rule was created ./fixtures/cf-rule-created.json
cf:rule.updated Access Rule was updated ./fixtures/cf-rule-updated.json

Usage

The acore package contains the core analytics client. The client is forked from the PostHog Go client (which itself appears to have been forked from Segment's Go SDK).

The library handles client-side hashing identifiers such as usr_123. We transform usr_123 using a SHA256 hash into usr_-CHh8_rdIqAotcBsP64GKQkfzW2hb1JDJ_6u7q4zom4 prior to events being dispatched. In the library this is controlled by the analytics:"usr" struct tag added to the event.

Adding new events

Check out CONTRIBUTING.md for a how-to guide.

Editing this README

This README is automatically generated from the template in README.tmpl. Edit that rather than this file.

Documentation

Index

Constants

View Source
const (
	DevEndpoint     = "https://t-dev.commonfate.io"
	DefaultEndpoint = "https://t.commonfate.io"
)

Variables

View Source
var (
	// Disabled disables analytics altogether.
	Disabled = Config{
		Endpoint: "",
		Enabled:  false,
		Verbose:  false,
	}
	// Development uses https://t-dev.commonfate.io as the endpoint.
	Development = Config{
		Endpoint: DevEndpoint,
		Enabled:  true,
		Verbose:  true,
	}
	// Default uses https://t.commonfate.io as the endpoint.
	Default = Config{
		Endpoint: DefaultEndpoint,
		Enabled:  true,
		Verbose:  false,
	}
)
View Source
var AllEvents = map[string]Event{}

Functions

func SetContext

func SetContext(ctx context.Context, client *Client) context.Context

SetContext sets the analytics in context

Types

type Client

type Client struct {

	// OnFailure is a callback fired if events are failed to be dispatched.
	OnFailure func(e Event)
	// contains filtered or unexported fields
}

func FromContext

func FromContext(ctx context.Context) *Client

FromContext loads an analytics client from context. analytics.SetContext must have been called. if there is no client in context, a new noop client is returned.

func New

func New(c Config) *Client

New creates an analytics client. Usage:

analytics.New(analytics.Development)

func (*Client) Close

func (c *Client) Close()

Close the client.

func (*Client) SetDeploymentID

func (c *Client) SetDeploymentID(depID string)

SetDeploymentID sets the deployment ID.

func (*Client) Track

func (c *Client) Track(e Event)

Track an event using the analytics client.

type Config

type Config struct {
	Endpoint string `json:"endpoint"`
	Enabled  bool   `json:"enabled"`
	Verbose  bool   `json:"verbose"`
}

Config is configuration for the analytics client.

func Env

func Env() Config

NewFromEnv sets up the analytics client based on the following parameters:

- URL is CF_ANALYTICS_URL, or falls back to the default URL if not provided - Disabled if CF_ANALYTICS_DISABLED is true

type DeploymentInfo

type DeploymentInfo struct {
	ID         string `json:"-"`
	Version    string `json:"version"`
	UserCount  int    `json:"user_count"`
	GroupCount int    `json:"group_count"`
	IDP        string `json:"idp"`
	Stage      string `json:"stage,omitempty"` // dev, prod, uat, etc.
}

func (*DeploymentInfo) EmittedWhen

func (d *DeploymentInfo) EmittedWhen() string

func (*DeploymentInfo) Type

func (d *DeploymentInfo) Type() string

type Event

type Event interface {

	// Type of the event, in the format 'namespace:entity.action.'
	//
	// namespace is the project namespace, like 'cf' for Common Fate.
	//
	// entity is the thing the event is related to, like 'scan'.
	//
	// action is the thing that happened in past tense, like 'created'.
	//
	// example type: "cf:scan.created"
	Type() string
	// Description of when the event is emitted.
	EmittedWhen() string
	// contains filtered or unexported methods
}

Event is a product analytics event that is tracked.

type Provider added in v0.2.0

type Provider struct {
	Publisher string `json:"publisher"`
	Name      string `json:"name"`
	Version   string `json:"version"`

	// Kind is only used for target groups
	Kind string `json:"kind,omitempty"`
}

type RequestCreated

type RequestCreated struct {
	RequestedBy     string   `json:"requested_by" analytics:"usr"`
	Provider        Provider `json:"provider"`
	BuiltInProvider string   `json:"built_in_provider"`
	PDKProvider     bool     `json:"pdk_provider"`
	RuleID          string   `json:"rule_id" analytics:"rul"`
	Timing          Timing   `json:"timing"`
	HasReason       bool     `json:"has_reason"`
	// RequiresApproval is true if a manual approval is required.
	RequiresApproval bool `json:"requires_approval"`
}

func (*RequestCreated) EmittedWhen

func (r *RequestCreated) EmittedWhen() string

func (*RequestCreated) Type

func (r *RequestCreated) Type() string

type RequestReviewed

type RequestReviewed struct {
	RequestedBy     string   `json:"requested_by" analytics:"usr"`
	ReviewedBy      string   `json:"reviewed_by" analytics:"usr"`
	Provider        Provider `json:"provider"`
	BuiltInProvider string   `json:"built_in_provider"`
	PDKProvider     bool     `json:"pdk_provider"`
	RuleID          string   `json:"rule_id" analytics:"rul"`
	Timing          Timing   `json:"timing"`
	OverrideTiming  *Timing  `json:"override_timing"`
	HasReason       bool     `json:"has_reason"`
	// PendingDurationSeconds is how long the request has been waiting for a review.
	PendingDurationSeconds float64 `json:"pending_duration_seconds"`
	// Review is APPROVE or DENY
	Review          string `json:"review"`
	ReviewerIsAdmin bool   `json:"reviewer_is_admin"`
}

func (*RequestReviewed) EmittedWhen

func (r *RequestReviewed) EmittedWhen() string

func (*RequestReviewed) Type

func (r *RequestReviewed) Type() string

type RequestRevoked

type RequestRevoked struct {
	RequestedBy     string   `json:"requested_by" analytics:"usr"`
	RevokedBy       string   `json:"revoked_by" analytics:"usr"`
	Provider        Provider `json:"provider"`
	BuiltInProvider string   `json:"built_in_provider"`
	PDKProvider     bool     `json:"pdk_provider"`
	RuleID          string   `json:"rule_id" analytics:"rul"`
	Timing          Timing   `json:"timing"`
	HasReason       bool     `json:"has_reason"`
}

func (*RequestRevoked) EmittedWhen

func (r *RequestRevoked) EmittedWhen() string

func (*RequestRevoked) Type

func (r *RequestRevoked) Type() string

type Role

type Role string
const (
	RoleEndUser Role = "end_user"
	RoleAdmin   Role = "admin"
)

type RuleArchived

type RuleArchived struct {
	RuleID     string `json:"rule_id" analytics:"rul"`
	ArchivedBy string `json:"archived_by" analytics:"usr"`
}

func (*RuleArchived) EmittedWhen

func (r *RuleArchived) EmittedWhen() string

func (*RuleArchived) Type

func (r *RuleArchived) Type() string

type RuleCreated

type RuleCreated struct {
	RuleID                string   `json:"rule_id" analytics:"rul"`
	CreatedBy             string   `json:"created_by" analytics:"usr"`
	UsesSelectableOptions bool     `json:"uses_selectable_options"`
	UsesDynamicOptions    bool     `json:"uses_dynamic_options"`
	Provider              Provider `json:"provider"`
	BuiltInProvider       string   `json:"built_in_provider"`
	PDKProvider           bool     `json:"pdk_provider"`
	MaxDurationSeconds    int      `json:"max_duration_seconds"`
	RequiresApproval      bool     `json:"requires_approval"`
	OptionCount           int      `json:"option_count"`
}

func (*RuleCreated) EmittedWhen

func (r *RuleCreated) EmittedWhen() string

func (*RuleCreated) Type

func (r *RuleCreated) Type() string

type RuleUpdated

type RuleUpdated struct {
	RuleID                string   `json:"rule_id" analytics:"rul"`
	UpdatedBy             string   `json:"updated_by" analytics:"usr"`
	UsesSelectableOptions bool     `json:"uses_selectable_options"`
	UsesDynamicOptions    bool     `json:"uses_dynamic_options"`
	Provider              Provider `json:"provider"`
	BuiltInProvider       string   `json:"built_in_provider"`
	PDKProvider           bool     `json:"pdk_provider"`
	MaxDurationSeconds    int      `json:"max_duration_seconds"`
	RequiresApproval      bool     `json:"requires_approval"`
	OptionCount           int      `json:"option_count"`
}

func (*RuleUpdated) EmittedWhen

func (r *RuleUpdated) EmittedWhen() string

func (*RuleUpdated) Type

func (r *RuleUpdated) Type() string

type Timing

type Timing struct {
	Mode            TimingMode `json:"mode"`
	DurationSeconds float64    `json:"duration_seconds"`
}

type TimingMode

type TimingMode string
var (
	TimingModeASAP      TimingMode = "asap"
	TimingModeScheduled TimingMode = "scheduled"
)

type UserInfo

type UserInfo struct {
	ID             string `json:"-"`
	GroupCount     int    `json:"group_count"`
	IsAdmin        bool   `json:"-"`
	AvailableRules int    `json:"available_rules,omitempty"`
}

func (*UserInfo) EmittedWhen

func (d *UserInfo) EmittedWhen() string

func (*UserInfo) Type

func (d *UserInfo) Type() string

Directories

Path Synopsis
Program readme generates the table of contents in the README for this package
Program readme generates the table of contents in the README for this package

Jump to

Keyboard shortcuts

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