Skip to content

Commit

Permalink
Merge pull request #22 from raj-prince/add-ttfb-latency
Browse files Browse the repository at this point in the history
feat: exporting first byte read latency
  • Loading branch information
raj-prince authored Nov 2, 2024
2 parents 5716613 + 781a2c6 commit 3cb1cdf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var (
// Cloud profiler.
enableCloudProfiler = flag.Bool("enable-cloud-profiler", false, "Enable cloud profiler")
enableHeap = flag.Bool("heap", false, "enable heap profile collection")
enableCpu = flag.Bool("cpu", true, "enable heap profile collection")
enableCPU = flag.Bool("cpu", true, "enable cpu profile collection")
enableHeapAlloc = flag.Bool("heap_alloc", false, "enable heap allocation profile collection")
enableThread = flag.Bool("thread", false, "enable thread profile collection")
enableContention = flag.Bool("contention", false, "enable contention profile collection")
Expand Down Expand Up @@ -122,9 +122,8 @@ func CreateHTTPClient(ctx context.Context, isHTTP2 bool) (client *storage.Client
Min: time.Second,
TargetPercentile: 0.99,
}))
} else {
return storage.NewClient(ctx, option.WithHTTPClient(httpClient))
}
return storage.NewClient(ctx, option.WithHTTPClient(httpClient))
}

// CreateGrpcClient creates grpc client.
Expand Down Expand Up @@ -153,6 +152,8 @@ func ReadObject(ctx context.Context, workerID int, bucketHandle *storage.BucketH
if err != nil {
return fmt.Errorf("while creating reader object: %v", err)
}
firstByteTime := time.Since(start)
stats.Record(ctx, firstByteReadLatency.M(float64(firstByteTime.Milliseconds())))

// Calls Reader.WriteTo implicitly.
_, err = io.Copy(io.Discard, rc)
Expand Down Expand Up @@ -198,7 +199,7 @@ func main() {
Service: "custom-go-benchmark",
ServiceVersion: *version,
ProjectID: *projectID,
NoCPUProfiling: !*enableCpu,
NoCPUProfiling: !*enableCPU,
NoHeapProfiling: !*enableHeap,
NoAllocProfiling: !*enableHeapAlloc,
NoGoroutineProfiling: !*enableThread,
Expand Down Expand Up @@ -234,6 +235,7 @@ func main() {

// Enable stack-driver exporter.
registerLatencyView()
registerFirstByteLatencyView()

err = enableSDExporter()
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions metrics_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
var (
// The restaurant rating in number of stars.
readLatency = stats.Float64("readLatency", "Complete read latency", stats.UnitMilliseconds)
firstByteReadLatency = stats.Float64("firstByteReadLatency", "First byte read latency", stats.UnitMilliseconds)
)

var sdExporter *stackdriver.Exporter
Expand All @@ -32,6 +33,19 @@ func registerLatencyView() {
log.Fatalf("Failed to register the readLatency view: %v", err)
}
}
func registerFirstByteLatencyView() {
v := &view.View{
Name: "princer_go_client_first_byte_read_latency",
Measure: firstByteReadLatency,
Description: "First byte read latency for a given go-client",
TagKeys: []tag.Key{tag.MustNewKey("princer_first_byte_read_latency")},
Aggregation: ochttp.DefaultLatencyDistribution,
}

if err := view.Register(v); err != nil {
log.Fatalf("Failed to register the firstByteReadLatency view: %v", err)
}
}

func enableSDExporter() (err error) {
sdExporter, err := stackdriver.NewExporter(stackdriver.Options{
Expand Down

0 comments on commit 3cb1cdf

Please sign in to comment.