Measuring Elapsed Time in Go

Measuring Elapsed Time in Go

To measure elapsed time in Go, you can use following code.

package main

import (
    "fmt"
    "time"
    
    "go.opentelemetry.io/otel/metric"
)

func expensiveCall() { }

func main() {
    // setup meter
    opDurationHistogration, _ := meter.Int64Histogram("operation_duration", metric.WithDescription("Operation duration"), metric.WithUnit("ms"))

    start := time.Now()

    expensiveCall()

    elapsed := time.Since()

    fmt.Printf("Took %s\n", elapsed)
    opDurationHistogration.Record(context.TODO(), elapsed)
}

Output

Took 11.583µs