✨ Capture traces
This commit is contained in:
		| @@ -1,6 +1,7 @@ | ||||
| package hypertext | ||||
|  | ||||
| import ( | ||||
| 	"github.com/spf13/viper" | ||||
| 	"math/rand" | ||||
| 	"regexp" | ||||
|  | ||||
| @@ -82,7 +83,7 @@ func UseProxies(app *fiber.App) { | ||||
|  | ||||
| 				// Passing all the rules means the site is what we are looking for. | ||||
| 				// Let us respond to our client! | ||||
| 				return makeResponse(ctx, &dest) | ||||
| 				return makeResponse(ctx, region, &location, &dest) | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| @@ -93,7 +94,7 @@ func UseProxies(app *fiber.App) { | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func makeResponse(ctx *fiber.Ctx, dest *navi.Destination) error { | ||||
| func makeResponse(ctx *fiber.Ctx, region *navi.Region, location *navi.Location, dest *navi.Destination) error { | ||||
| 	// Modify request | ||||
| 	for _, transformer := range dest.Transformers { | ||||
| 		if err := transformer.TransformRequest(ctx); err != nil { | ||||
| @@ -111,5 +112,26 @@ func makeResponse(ctx *fiber.Ctx, dest *navi.Destination) error { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// Collect trace | ||||
| 	if viper.GetBool("telemetry.capture_traces") { | ||||
| 		var message string | ||||
| 		if err != nil { | ||||
| 			message = err.Error() | ||||
| 		} | ||||
|  | ||||
| 		go navi.R.AddTrace(navi.RoadTrace{ | ||||
| 			Region:      region.ID, | ||||
| 			Location:    location.ID, | ||||
| 			Destination: dest.ID, | ||||
| 			Uri:         ctx.OriginalURL(), | ||||
| 			IpAddress:   ctx.IP(), | ||||
| 			UserAgent:   ctx.Get(fiber.HeaderUserAgent), | ||||
| 			Error:       navi.RoadTraceError{ | ||||
| 				IsNull: err == nil, | ||||
| 				Message: message, | ||||
| 			}, | ||||
| 		}) | ||||
| 	} | ||||
|  | ||||
| 	return err | ||||
| } | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| package navi | ||||
|  | ||||
| import ( | ||||
| 	"github.com/spf13/viper" | ||||
| 	"io" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| @@ -13,6 +14,7 @@ var R *RoadApp | ||||
| func ReadInConfig(root string) error { | ||||
| 	instance := &RoadApp{ | ||||
| 		Regions: make([]*Region, 0), | ||||
| 		Traces: make([]RoadTrace, 0, viper.GetInt("performance.traces_limit")), | ||||
| 	} | ||||
|  | ||||
| 	if err := filepath.Walk(root, func(fp string, info os.FileInfo, _ error) error { | ||||
|   | ||||
							
								
								
									
										25
									
								
								pkg/navi/metrics.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								pkg/navi/metrics.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | ||||
| package navi | ||||
|  | ||||
| import "github.com/spf13/viper" | ||||
|  | ||||
| type RoadTrace struct { | ||||
| 	Region      string         `json:"region"` | ||||
| 	Location    string         `json:"location"` | ||||
| 	Destination string         `json:"destination"` | ||||
| 	Uri         string         `json:"uri"` | ||||
| 	IpAddress   string         `json:"ip_address"` | ||||
| 	UserAgent   string         `json:"user_agent"` | ||||
| 	Error       RoadTraceError `json:"error"` | ||||
| } | ||||
|  | ||||
| type RoadTraceError struct { | ||||
| 	IsNull  bool   `json:"is_null"` | ||||
| 	Message string `json:"message"` | ||||
| } | ||||
|  | ||||
| func (v *RoadApp) AddTrace(trace RoadTrace) { | ||||
| 	v.Traces = append(v.Traces, trace) | ||||
| 	if len(v.Traces) > viper.GetInt("performance.traces_limit") { | ||||
| 		v.Traces = v.Traces[1:] | ||||
| 	} | ||||
| } | ||||
| @@ -7,7 +7,8 @@ import ( | ||||
| ) | ||||
|  | ||||
| type RoadApp struct { | ||||
| 	Regions []*Region `json:"regions"` | ||||
| 	Regions []*Region    `json:"regions"` | ||||
| 	Traces  []RoadTrace `json:"traces"` | ||||
| } | ||||
|  | ||||
| func (v *RoadApp) Forward(ctx *fiber.Ctx, dest *Destination) error { | ||||
|   | ||||
							
								
								
									
										10
									
								
								pkg/sideload/metrics.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								pkg/sideload/metrics.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | ||||
| package sideload | ||||
|  | ||||
| import ( | ||||
| 	"code.smartsheep.studio/goatworks/roadsign/pkg/navi" | ||||
| 	"github.com/gofiber/fiber/v2" | ||||
| ) | ||||
|  | ||||
| func getTraces(c *fiber.Ctx) error { | ||||
| 	return c.JSON(navi.R.Traces) | ||||
| } | ||||
| @@ -53,6 +53,7 @@ func InitSideload() *fiber.App { | ||||
| 	cgi := app.Group("/cgi").Name("CGI") | ||||
| 	{ | ||||
| 		cgi.Get("/metadata", getMetadata) | ||||
| 		cgi.Get("/traces", getTraces) | ||||
| 		cgi.Get("/statistics", getStatistics) | ||||
| 		cgi.Get("/sites", getRegions) | ||||
| 		cgi.Get("/sites/cfg/:id", getRegionConfig) | ||||
|   | ||||
| @@ -21,8 +21,12 @@ max_qps = -1 | ||||
| [paths] | ||||
| configs = "./config" | ||||
|  | ||||
| [performance] | ||||
| [telemetry] | ||||
| request_logging = true | ||||
| capture_traces = true | ||||
|  | ||||
| [performance] | ||||
| traces_limit = 256 | ||||
| network_timeout = 3_000 | ||||
| prefork = false | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user