Getting Started
Server SDKs
Go

Getting Started with Go and Fiber

Stanza provides a Go SDK with adapters for easy integration with popular libraries.

Install the package

$ go get github.com/StanzaSystems/sdk-go

Initialize Stanza

Import the necessary dependencies

For example if you are using Fiber (opens in a new tab) and Zap (opens in a new tab), you can use the fiberstanza adapter:

import (
	"context"
	"encoding/json"
 
	"github.com/StanzaSystems/sdk-go/adapters/fiberstanza"
	"github.com/gofiber/contrib/fiberzap"
	"github.com/gofiber/fiber/v2"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

Initialize Stanza at the root of your application

const (
	name        = "MyService"
	release     = "0.1.0"
	environment = "dev"
	debug       = true
)
 
// Create a context before initialzing Stanza (or re-use an existing one)
ctx := context.Background()
 
shutdown, stanzaInitErr := fiberstanza.Init(ctx,
	fiberstanza.Client{
		APIKey:      "c6af1e6b-78f4-40c1-9428-2c890dcfdd7f", // API key from the Stanza UI
		Name:        name,        // the name of your service
		Release:     release,     // the release/version of your service
		Environment: environment, // the environment of your service 
	})
defer shutdown()
if stanzaInitErr != nil {
	logger.Error("stanza.init", zap.Error(stanzaInitErr))
}

Leverage Stanza

Inbound Guards

Stanza inbound guards intercept a service's incoming traffic to guard that service from overload. Services with inbound guards can be configured to shed load in the Stanza UI based on multiple factors, including requests per second, CPU saturation, and memory utilization.

To use an inbound guard, register it as middleware using one of the Stanza adapters. For example, if you are using Fiber (opens in a new tab), you would use the fiberstanza middleware:

// fiber: HTTP server
app := fiber.New()
 
if stanzaInitErr == nil {
	app.Use("/", fiberstanza.Middleware("RootGuard"))
	app.Use("/login", fiberstanza.Middleware("LoginGuard"))
}

Outbound Guards

Outbound guards guard another service from overload at the calling point of your service. They are useful for managing requests to services you do not control the code for. For example, outbound guards are a great way to manage rate limiting from third parties.

To use an outbound guard, wrap the call using the Stanza API provided functions. If you have already imported an adapter (fiberstanza for example), you will have many outbound wrappers available to use (HttpGet, HttpPost, etc).

The following example uses Stanza to call ZenQuotes. Because it is passing the optional Stanza feature, it can prioritize requests for higher priority features when approaching rate limits.

// resp, err := http.Get("https://zenquotes.io/api/random") // before Stanza looks like this
 
resp, err := fiberstanza.HttpGet("https://zenquotes.io/api/random",
				fiberstanza.Decorate("ZenQuotes", fiberstanza.GetFeatureFromContext(c)))
if err != nil {
	// Consider how you want to handle this error! This could be an error from ZenQuotes or a
	// "429 Too Many Requests" from Stanza. For example, instead of returning the error directly,
	// maybe display a user friendly "Something went wrong!" type of error page. Or if it's an
	// optional component of a larger page, just skip rendering it without returning an error.
	return err
}
defer resp.Body.Close()
;