Containers / Generic

Currently, you can only instrument one function per trace within Go applications. If a function contains multiple operations, each operation will be traced.

To start tracing, connect your Application with epsagon.NewTracerConfig.
epsagon.NewTracerConfig requires arguments:

  1. Epsagon Application Name.
  2. Epsagon Token.

Now you can trace functions using epsagon.GoWrapper.
epsagon.GoWrapper requires arguments:

  1. Instance of epsagon.NewTracerConfig()
  2. Function to Trace
  3. [optional] Custom Function Name. Function will be referred to by this name in your Epsagon dashboard.

An example of a Traced function can be seen here:

import (
  "fmt"
  "github.com/epsagon/epsagon-go/epsagon"
)

// any function can be traced
func Double (a int) (int, err) {
  return a * 2, nil 
}

func main () {
  
  // connect to Epsagon Application
  config := epsagon.NewTracerConfig(
    <APP-NAME-STAGE>, 
    <EPSAGON-TOKEN>,
  )
  config.Debug = true
  
  // wrap function with GoWrapper
  // call it with int
  response = epsagon.GoWrapper(config, Double, "Double-Go") (10)
}

To trace multiple functions in the same environment, use epsagon.ConcurrentGoWrapper.
epsagon.ConcurrentGoWrapper requires the same parameters as epsagon.GoWrapper, but must be called asynchronously as a goroutine.

Functions wrapped with epsagon.ConcurrentGoWrapper must receive a context as its first parameter, and a pointer to a WaitGroup.

An example of multiple Traced goroutines can be seen below:

import (
  "context"
  "github.com/epsagon/epsagon-go/epsagon"
  "log"
  "sync"
  "time"
)

func Multitask (ctx context.Context, i int, wg *sync.WaitGroup) (int, error) {

  // notifies WaitGroup when goroutine returns
  defer wg.Done()
  return i + 1, nil
}

func main () {

  // connect to Epsagon Application
  config := epsagon.NewTracerConfig(
    <APP-NAME-STAGE>, 
    <EPSAGON-TOKEN>,
  )
  config.Debug = true
  
  // wg is a pointer to sync.WaitGroup instance
  wg := &sync.WaitGroup{}

  for i := 0; i < 3; i++ {
  
    // add 1 to WaitGroup before every job
        wg.Add(1)
    
    // wrap goroutine with ConcurrentGoWrapper
    // call it with int and pointer to WaitGroup
    // do not pass in context, Epsagon does it for you
        go epsagon.ConcurrentGoWrapper(config, Multitask) (i, wg)
    }
  
  // wait for WaitGroup to finish jobs
    wg.Wait()
    time.Sleep(2 * time.Second)
}

Did this page help you?