gcfwrapper

package
v0.0.0-...-951cea1 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

README

SignalFx Go Google Cloud Function Wrapper

SignalFx Golang Google Cloud Function Wrapper.

Usage

The SignalFx Go Google Cloud Function Wrapper is a wrapper around a Google Cloud Function Go function handler, used to instrument execution of the function and send metrics to SignalFx.

Installation

To install run the command:

$ go get https://github.com/signalfx/serverless-go

Configuring the ingest endpoint

By default, this function wrapper will send to the us0 realm. If you are not in this realm you will need to set the SIGNALFX_INGEST_ENDPOINT environment variable to the correct realm ingest endpoint (https://ingest.{REALM}.signalfx.com/v2/datapoint). To determine what realm you are in, check your profile page in the SignalFx web application (click the avatar in the upper right and click My Profile).

Environment Variable

Set the SIGNALFX_ACCESS_TOKEN environment variable with the appropriate SignalFx authentication token. Change the default values of the other variables accordingly if desired.

SIGNALFX_ACCESS_TOKEN=<SignalFx authentication token>

SIGNALFX_INGEST_ENDPOINT=https://ingest.{REALM}.signalfx.com/v2/datapoint

SIGNALFX_SEND_TIMEOUT_SECONDS=5

Wrapping a function

The SignalFx Go Google Cloud Function Wrapper wraps the user cloud function. Pass the cloud function to gcfwrapper.NewHandlerWrapper function to create the wrapper gcfwrapper.HandlerWrapper. Finally, invoke the wrapped function by calling Invoke method of the wrapper. See the example below.

import (
  ...
  "github.com/signalfx/serverless-go/gcfwrapper"
  ...
)
...

func userFunc(w http.ResponseWriter, r *http.Request) {
  ...  
}
...

func main(w http.ResponseWriter, r *http.Request) {
  ...
  wrapper := gcfwrapper.NewHandlerWrapper(userFunc)
  wrapper.Invoke(w, r)
  ...
}
...
Metrics and dimensions sent by the wrapper

The Google Cloud Function wrapper sends the following metrics to SignalFx:

Metric Name Type Description
function.invocations Counter Count number of Cloud Function invocations
function.cold_starts Counter Count number of cold starts
function.duration Gauge Milliseconds in execution time of underlying Cloud Function handler

The Cloud Function wrapper adds the following dimensions to all data points sent to SignalFx:

Dimension Description
gcf_region Google Cloud Function Region
gcf_project_id Google Cloud Function Project ID
gcf_function_name Google Cloud Function Name
gcf_function_version Google Cloud Function Version
function_wrapper_version SignalFx function wrapper qualifier (e.g. signalfx_gcf_go-0.0.1)
metric_source The literal value of 'gcf_wrapper'
Sending custom metric in the Google Cloud function

Use the method SendDatapoint() of gcfwrapper.HandlerWrapper to send custom metric datapoints to SignalFx from within your cloud function. A gcfwrapper.HandlerWrapper variable needs to be declared globally in order to be accessible from within your cloud function. See example below.

import (
  ...
  "github.com/signalfx/serverless-go/gcfwrapper"
  "github.com/signalfx/golib/datapoint"
  ...
)
...

var wrapper gcfwrapper.HandlerWrapper
...

func userFunc(w http.ResponseWriter, r *http.Request) {
  ...
  // Custom counter metric.
  dp := datapoint.Datapoint {
      Metric: "db_calls",
      Value: datapoint.NewIntValue(1),
      MetricType: datapoint.Counter,
      Dimensions: map[string]string{"db_name": "mysql1"},
  }
  // Sending custom metric to SignalFx.
  wrapper.SendDatapoints([]*datapoint.Datapoint{&dp})
  ...
}
...

func main(w http.ResponseWriter, r *http.Request) {
  ...
  wrapper := gcfwrapper.NewHandlerWrapper(handler)
  wrapper.Invoke(w, r)
  ...
}
...

License

Apache Software License v2. Copyright © 2019 SignalFx

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ColdStartsDatapoint = sfxcommon.ColdStartsDatapoint

ColdStartsDatapoint exposes a function from common library to create a datapoint to report function cold starts count to SignalFx

View Source
var DurationDatapoint = sfxcommon.DurationDatapoint

DurationDatapoint exposes a function from common library to create a datapoint to report function duration to SignalFx

View Source
var ErrorsDatapoint = sfxcommon.ErrorsDatapoint

ErrorsDatapoint exposes a function from common library to create a datapoint to report function errors count to SignalFx

View Source
var InvocationsDatapoint = sfxcommon.InvocationsDatapoint

InvocationsDatapoint exposes a function from common library to create a datapoint to report function invocations count to SignalFx

Functions

This section is empty.

Types

type HandlerWrapper

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

HandlerWrapper provides methods to invoke user function and send custom datapoints

func NewHandlerWrapper

func NewHandlerWrapper(h func(http.ResponseWriter, *http.Request)) HandlerWrapper

NewHandlerWrapper is a HandlerWrapper creating factory function.

func (*HandlerWrapper) Invoke

func (hw *HandlerWrapper) Invoke(w http.ResponseWriter, r *http.Request)

Invoke runs user function and records the invocation/duration of function

func (*HandlerWrapper) SendDatapoints

func (hw *HandlerWrapper) SendDatapoints(dps []*datapoint.Datapoint) error

SendDatapoints sends custom metric datapoints to SignalFx.

Jump to

Keyboard shortcuts

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