Gin Web Application
On this page, we help you install ShortLoop SDK in your Gin web application, so that it can automatically sample traffic.
Requirements
Go version - 1.17
or higher
Gin version - 1.4.0
or higher.
Install
Install ShortLoop Go SDK.
$ go get github.com/short-loop/shortloop-go@v0.0.6
ShortLoop SDK has following external dependency:
- sync (opens in a new tab) (Standard Go library for handling concurrency)
Configure
To configure the SDK, you need to add it as a middleware in your application.
Import the SDK from installed package on top of you root level main.go
file where you initialize your router.
import "github.com/short-loop/shortloop-go/shortloopgin"
Initialize SDK with options - ShortloopEndpoint
and ApplicationName
as shown in below example and register the middleware.
router := gin.Default()
shortloopSdk, err := shortloopgin.Init(shortloopgin.Options{
ShortloopEndpoint: "https://shortloop.company-name.com", // the shortloop url for your org. (Provided by ShortLoop team.)
ApplicationName: "service-name", // your application name here.
AuthKey: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // ShortLoop Auth Key. (Provided by ShortLoop team.)
Environment: "your-environment", // for e.g stage or prod
})
if err != nil {
fmt.Println("Error initializing shortloopgin: ", err)
} else {
router.Use(shortloopSdk.Filter())
}
Configure : Masking sensitive headers.
To mask sensitive headers, provide an array as MaskHeaders
field as shown in below example.
Note that MaskHeaders
field is optional and headers are case insensitive.
router := gin.Default()
shortloopSdk, err := shortloopgin.Init(shortloopgin.Options{
...
...
Environment: "your-environment", // for e.g stage or prod
MaskHeaders: []string{"cookie", "x-auth"}, // [Optional]. The following headers values will be masked at the SDK level itself. (Will not be sent to the server)
})
...
...
Configure : Samples capturing frequency
By default; ShortLoop SDK will capture a small percentage of traffic; so that the performance impact is almost nil. But for certain use-cases; or non production environments; traffic capturing can be configured to 'always'; in which case SDK will try to capture 100% of the Traffic.
router := gin.Default()
shortloopSdk, err := shortloopgin.Init(shortloopgin.Options{
...
...
Environment: "your-environment", // for e.g stage or prod
MaskHeaders: []string{"cookie", "x-auth"}, // [Optional]. The following headers values will be masked at the SDK level itself. (Will not be sent to the server)
Capture: "always" // Requesting SDK to try to capture all the API calls.
})
...
...
Setting the above property; doesn't guarantees that all the API calls will be captures. SDK will try to capture most of the Traffic; but if the traffic increases; the Samples will be dropped.
It is not recommended to set the above property in high traffic production environments which are performance and latency sensitive.
Verify
Quickly test if project is building after configuring SDK.
Build command maybe custom to your project.
go build main.go
After the changes, redeploy your Gin Application.
Feel free to email for a quick support, reporting bug or improvements suggestions. Sumit B Mulchandani (sumit@shortloop.dev)