Fiber Quick Start
Learn how to set up highlight.io monitoring and logging on your Go Fiber backend.
Configure client-side Highlight. (optional)
If you're using Highlight on the frontend for your application, make sure you've initialized it correctly and followed the fullstack mapping guide.
Install the Highlight Go SDK.
Install the highlight-go
package with go get
.
go get -u github.com/highlight/highlight/sdk/highlight-go
Initialize the Highlight Go SDK.
highlight.Start
starts a goroutine for recording and sending backend traces and errors. Setting your project id lets Highlight record errors for background tasks and processes that aren't associated with a frontend session.
import (
"github.com/highlight/highlight/sdk/highlight-go"
)
func main() {
// ...
highlight.SetProjectID("<YOUR_PROJECT_ID>")
highlight.Start(
highlight.WithServiceName("my-app"),
highlight.WithServiceVersion("git-sha"),
)
defer highlight.Stop()
// ...
}
Add the Highlight Fiber error handler.
highlightFiber.Middleware()
provides a Go Fiber middleware to automatically record and send errors to Highlight.
import (
highlightFiber "github.com/highlight/highlight/sdk/highlight-go/middleware/fiber"
)
func main() {
// ...
app := fiber.New()
app.Use(highlightFiber.Middleware())
// ...
}
Record custom errors. (optional)
If you want to explicitly send an error to Highlight, you can use the highlight.RecordError
method.
highlight.RecordError(ctx, err, attribute.String("key", "value"))
Verify your errors are being recorded.
Now that you've set up the Middleware, verify that the backend error handling works by consuming an error from your handler. This is as easy as having a route handler return an error.
Call logrus methods while passing the request context.
The request context allows highlight to associate logs with the incoming frontend session and network request.
logrus.WithContext(c.Context()).WithField("user", "bob").Infof("hello, %s!", "world")
Call the Highlight logging SDK.
Use our SDK to configure logrus, and use it as normal.
package main
import (
"context"
"github.com/highlight/highlight/sdk/highlight-go"
"github.com/highlight/highlight/sdk/highlight-go/log"
"github.com/sirupsen/logrus"
)
func main() {
// setup the highlight SDK
highlight.SetProjectID("<YOUR_PROJECT_ID>")
highlight.Start(
highlight.WithServiceName("my-fiber-app"),
highlight.WithServiceVersion("git-sha"),
)
defer highlight.Stop()
// setup highlight logrus hook
hlog.Init()
// if you don't want to get stdout / stderr output, add the following uncommented
// hlog.DisableOutput()
app := fiber.New()
app.Use(logger.New())
// setup go fiber to use the highlight middleware for header parsing
app.Use(highlightFiber.Middleware())
app.Get("/", func(c *fiber.Ctx) error {
// in handlers, use logrus with the UserContext to associate logs with the frontend session.
logrus.WithContext(c.Context()).Infof("hello from highlight.io")
return c.SendString("Hello, World!")
})
logrus.Fatal(app.Listen(":3456"))
}
Verify your backend logs are being recorded.
Visit the highlight logs portal and check that backend logs are coming in.
Verify your backend traces are being recorded.
Visit the highlight traces portal and check that backend traces are coming in.