Skip to content

Commit 4591c19

Browse files
authored
simple span (#11)
1 parent af2cf71 commit 4591c19

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

Diff for: _examples/basic/main.go

+11
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package main
33
import (
44
"context"
55
"log/slog"
6+
"math/rand"
67
"net/http"
78
"os"
89
"os/signal"
910
"strconv"
1011
"syscall"
12+
"time"
1113

1214
"github.com/go-chi/chi/v5"
1315
"github.com/go-chi/chi/v5/middleware"
@@ -61,6 +63,15 @@ func main() {
6163
AppMetrics.RecordAppGauge(floatValue)
6264
w.Write([]byte("Gauge recorded!"))
6365
})
66+
r.Get("/compute", func(w http.ResponseWriter, r *http.Request) {
67+
span := AppMetrics.RecordSpan("compute", nil)
68+
defer span.Stop()
69+
70+
// do random work for random tie,,
71+
time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
72+
73+
w.Write([]byte("Span recorded!"))
74+
})
6475
})
6576

6677
sig := make(chan os.Signal, 1)

Diff for: telemetry.go

+4
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ func (n *Scope) RecordDurationWithResolution(measurement string, tags map[string
172172
record.RecordDuration(elapsed)
173173
}
174174

175+
func (n *Scope) RecordSpan(measurement string, tags map[string]string) tally.Stopwatch {
176+
return n.scope.Timer(measurement + "_span").Start()
177+
}
178+
175179
func newRootScope(opts tally.ScopeOptions, interval time.Duration) (tally.Scope, io.Closer) {
176180
opts.CachedReporter = reporter
177181
opts.Separator = prometheus.DefaultSeparator

0 commit comments

Comments
 (0)