Star us on GitHub
Star
Company
Open Source
Mission & Values
Getting Started
Overview
Fullstack Mapping
Backend / Server
Frontend / Client
Fullstack Frameworks
Session Replay
Console Messages
HTML iframe Recording
Identifying Users
Live Mode
Network DevTools
Privacy
Rage Clicks
Recording Network Requests and Responses
Session Sharing
Session Shortcut
Tracking Events
Versioning Sessions
Error Monitoring
Error Sharing
Grouping Errors
Sourcemaps
Versioning Errors
Product Features
Alerts
Analytics
Canvas
Comments
Cross Origin Iframe Recording
Digests
Environments
Frontend Observability
Keyboard Shortcuts
Performance Data
Segments
Session Search
Team Management
User Feedback
Web Vitals
WebGL
Integrations
Amplitude Integration
Clearbit Integration
ClickUp Integration
Discord Integration
Electron Support
Front Plugin
Height Integration
Intercom Integration
Linear Integration
Mixpanel Integration
React.js Integration
Segment Integration
Sentry Integration
Slack Integration
Vercel Integration
highlight.run Changelog
5.0.0
5.0.1
5.1.0
5.1.1
5.1.2
5.1.3
5.1.4
5.1.5
5.1.6
5.1.7
5.1.8
5.2.0
5.2.1
5.2.2
5.2.3
Tips
Content-Security-Policy
Local Development
Monkey Patches
Performance Impact
Proxying Highlight
Session Search Deep Linking
Troubleshooting
Upgrading Highlight
Menu
Docs / Welcome to Highlight / Getting Started / Backend / Server / Go Backend

Go Backend

Highlight supports several server frameworks written in Go.

go-chi/chi gin-gonic/gin

Usage

First, install and import the go package in your entrypoint.

go get -u github.com/highlight/highlight/sdk/highlight-go
Copy

Add the following lines to your application's main (func main) function:

import ( "github.com/highlight/highlight/sdk/highlight-go" ) func main() { //...application logic... highlight.Start() defer highlight.Stop() highlight.SetProjectID("YOUR_PROJECT_ID") //...application logic... }
Copy

This configures highlight to transmit any relevant events or errors as they may happen. You can also customize highlight by using the public highlight methods before calling Start(). However, we still need to associate your users' sessions with potential backend errors. We provide middleware packages that help set this up:

Middleware

Add the following middleware to your router:

// with chi import ( highlightChi "github.com/highlight/highlight/sdk/highlight-go/middleware/chi" ) func main() { //... r := chi.NewRouter() r.Use(highlightChi.Middleware) //... } // with gin import ( highlightGin "github.com/highlight/highlight/sdk/highlight-go/middleware/gin" ) func main() { //... r := gin.Default() r.Use(highlightGin.Middleware()) //... }
Copy
Instrumenting Handlers

Great! Now we've configured the highlight client and can track sessions from the frontend to the backend. All we need to do now is instrument our backend code to transmit events or errors where relevant.

package main // with chi import ( "errors" "github.com/go-chi/cors" "github.com/highlight/highlight/sdk/highlight-go" "net/http" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" highlightChi "github.com/highlight/highlight/sdk/highlight-go/middleware/chi" ) func main() { highlight.Start() defer highlight.Stop() highlight.SetProjectID("YOUR_PROJECT_ID") r := chi.NewRouter() r.Use(middleware.Logger) r.Use(highlightChi.Middleware) // setup cors if necessary for your frontend r.Use(cors.Handler(cors.Options{ AllowedOrigins: []string{"http://localhost:3001"}, AllowedHeaders: []string{"X-Highlight-Request"}, })) r.Get("/ping", PingHandler) http.ListenAndServe("0.0.0.0:8080", r) } var appHealthy = false func PingHandler(w http.ResponseWriter, r *http.Request) { // we can instrument our handlers directly to record events or error if !appHealthy { highlight.ConsumeError(r.Context(), errors.New("a health check failure occured!")) } w.Write([]byte("welcome")) } // with gin import ( "errors" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/highlight/highlight/sdk/highlight-go" highlightGin "github.com/highlight/highlight/sdk/highlight-go/middleware/gin" ) func main() { highlight.Start() defer highlight.Stop() highlight.SetProjectID("YOUR_PROJECT_ID") r := gin.Default() r.Use(highlightGin.Middleware()) // setup cors if necessary for your frontend r.Use(cors.New(cors.Config{ AllowOrigins: []string{"http://localhost:3001"}, AllowHeaders: []string{"X-Highlight-Request"}, })) r.GET("/ping", PingHandler) r.Run("0.0.0.0:8080") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") } var appHealthy = false func PingHandler(c *gin.Context) { // we can instrument our handlers directly to record events or error if !appHealthy { highlight.ConsumeError(c, errors.New("a health check failure occured!")) } c.JSON(200, gin.H{ "message": "Hello, World!", }) }
Copy