utils

package
v0.0.0-...-644905b Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2022 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const NegativeDivision = errorString("cannot divide a number into a number of parts <=0")

Variables

This section is empty.

Functions

func Divide

func Divide(numerator, denominator int) ([]int, error)

Divide Distributes numerator into denominator pieces as equally as possible

func EnvelopeFromBody

func EnvelopeFromBody(eventID string, sentAt time.Time, eventType string, extraHeaders map[string]string, body json.RawMessage) (*bytes.Buffer, error)

EnvelopeFromBody creates the body of a session shamelessly stolen and modified from sentry-go/transport.go

func ExponentialBackoff

func ExponentialBackoff(initial time.Duration, maximum time.Duration, factor float64) func() time.Duration

ExponentialBackoff returns an exponentially increasing Duration

the duration will increase until the maximum duration is reached after which it will return that duration forever. This is not thread safe and should only be called from one goroutine per backoff function.

func FileExists

func FileExists(fileName string) bool

FileExists returns True if a file exists ( it must be a file i.e. not a directory)

func GetAuthHeader

func GetAuthHeader(projectKey string) string

func GetExternalIPv4

func GetExternalIPv4() (string, error)

func GetStatsd

func GetStatsd(statsdAddr string) *statsd.Client

func LowerFirstLetter

func LowerFirstLetter(s string) string

LowerFirstLetter converts the first letter to lower case

func Min

func Min(a, b int) int

func PerSecond

func PerSecond(elements int64, interval time.Duration) (float64, error)

PerSecond converts a number of elements per random duration in elements per second

func PrivateKeyFromString

func PrivateKeyFromString(public string, private string) (ed25519.PrivateKey, error)

PrivateKeyFromString creates a private key (and does some basic validation) from base64URL encoded public/private key pair. The public private key pair is in the same format as Relay's public/private key pair (found in credentials.json).

func RandomChoice

func RandomChoice(choices []string, relativeWeights []int64) (string, error)

RandomChoice implements the weighted version of the python random.choices standard function

if relativeWeights is empty or smaller than choices weights of 1 will be considered for the missing weights, if more weights are passed they will be ignored

func RegisterProjectProvider

func RegisterProjectProvider(projectsFileName string) error

func RelayAuthSign

func RelayAuthSign(privateKey ed25519.PrivateKey, data []byte, timestamp time.Time) (string, error)

RelayAuthSign signs the given data for Relay authentication (the data should be the body of the request). The signature should be passed with the message in the Relay authentication header (X-Sentry-Relay-Signature).

func SimpleRandomChoice

func SimpleRandomChoice(choices []string) string

SimpleRandomChoice returns one of the given choices picked up randomly, with the same probability for each choice.

func UuidAsHex

func UuidAsHex(id uuid.UUID) string

UuidAsHex similar with uuid.hex from python ( returns the UUID as a hex string without - )

Types

type FileProjectProvider

type FileProjectProvider struct {
	// contains filtered or unexported fields
}

func LoadFileProjectProvider

func LoadFileProjectProvider(filePath string) (*FileProjectProvider, error)

LoadFileProjectProvider loads projects from a json file containing a list of projectId to projectKey mappings.

func (FileProjectProvider) GetNextProjectId

func (provider FileProjectProvider) GetNextProjectId(maxProjects int, currentProjectId string) string

func (FileProjectProvider) GetNumberOfProjects

func (provider FileProjectProvider) GetNumberOfProjects() int

func (FileProjectProvider) GetProjectId

func (provider FileProjectProvider) GetProjectId(maxProjects int) string

func (FileProjectProvider) GetProjectIdV2

func (provider FileProjectProvider) GetProjectIdV2(profiles []ProjectFreqProfile) (string, int, error)

func (FileProjectProvider) GetProjectInfo

func (provider FileProjectProvider) GetProjectInfo(projectId string) ProjectInfo

type ProjectFreqProfile

type ProjectFreqProfile interface {
	GetNumProjects() int
	GetRelativeFreqWeight() float64
}

ProjectFreqProfile defines the frequency relative to other profiles in which a project is picked. Example: [ {numProjects:5, relativeFreq:1}, {numProjects:3, relativeFreq:2}] With the example above the project provider will return 8 projects (5+3) and each of the 5 projects in the first group would be return 3 times less often than each of the 3 project in the second group.

type ProjectInfo

type ProjectInfo struct {
	ProjectId        string `json:"project_id" yaml:"project_id"`
	ProjectKey       string `json:"project_key" yaml:"project_key"`
	ProjectApiKey    string `json:"access_token,omitempty" yaml:"access_token,omitempty"`
	ProjectSlug      string `json:"project_slug,omitempty" yaml:"project_slug,omitempty"`
	OrganizationSlug string `json:"organization_slug,omitempty" yaml:"organization_slug,omitempty"`
}

type ProjectProvider

type ProjectProvider interface {
	// GetNumberOfProjects returns the number of projects that can be used
	GetNumberOfProjects() int
	// GetProjectId returns a random project id
	GetProjectId(maxProjects int) string
	// GetProjectIdV2 returns a random project id weighted by the specified project
	// profiles. With this function you are able to specify that some projects may
	// be called with a greater frequency than other projects. Profiles is a list
	// of elements containing the number of projects and their relative frequency ratio
	GetProjectIdV2(profiles []ProjectFreqProfile) (string, int, error)
	// GetNextProjectId returns the next project id given the last used project id
	GetNextProjectId(maxProjects int, currentProjectId string) string
	GetProjectInfo(projectId string) ProjectInfo
}

ProjectProvider can be used to get project Ids and keys for testing

func GetProjectProvider

func GetProjectProvider() ProjectProvider

GetProjectProvider returns the current project provider. Note: this function is not thread safe but since this is only initialised once at the start of the program, it should be fine.

type RandomProjectProvider

type RandomProjectProvider struct{}

func (RandomProjectProvider) GetNextProjectId

func (provider RandomProjectProvider) GetNextProjectId(maxProjects int, currentProjectId string) string

func (RandomProjectProvider) GetNumberOfProjects

func (provider RandomProjectProvider) GetNumberOfProjects() int

func (RandomProjectProvider) GetProjectId

func (provider RandomProjectProvider) GetProjectId(maxProjects int) string

func (RandomProjectProvider) GetProjectIdV2

func (provider RandomProjectProvider) GetProjectIdV2(profiles []ProjectFreqProfile) (string, int, error)

func (RandomProjectProvider) GetProjectInfo

func (provider RandomProjectProvider) GetProjectInfo(projectId string) ProjectInfo

type StringDuration

type StringDuration time.Duration

StringDuration a duration that serializes in Json/Yaml as a string e.g. "2m"

func (StringDuration) MarshalJSON

func (t StringDuration) MarshalJSON() ([]byte, error)

func (StringDuration) MarshalYAML

func (t StringDuration) MarshalYAML() (any, error)

func (*StringDuration) UnmarshalJSON

func (t *StringDuration) UnmarshalJSON(b []byte) error

func (*StringDuration) UnmarshalYAML

func (t *StringDuration) UnmarshalYAML(unmarshal func(any) error) error

Jump to

Keyboard shortcuts

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