Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make metrics build specific #237

Merged
merged 5 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions instrumentation/opentelemetry/internal/metrics/linux_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//go:build linux

package metrics

import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/tklauser/go-sysconf"
)

const procStatArrayLength = 52

var (
clkTck = getClockTicks()
pageSize = float64(os.Getpagesize())
)

type processStats struct {
utime float64
stime float64
cutime float64
cstime float64
rss float64
}

type linuxMetrics struct {
memory float64
cpuSecondsTotal float64
}

func newSystemMetrics() (systemMetrics, error) {
lm := &linuxMetrics{}
stats, err := lm.processStatsFromPid(os.Getpid())
if err != nil {
return nil, err

Check warning on line 39 in instrumentation/opentelemetry/internal/metrics/linux_metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/linux_metrics.go#L35-L39

Added lines #L35 - L39 were not covered by tests
}
lm.memory = stats.rss * pageSize
lm.cpuSecondsTotal = (stats.stime + stats.utime + stats.cstime + stats.cutime) / clkTck
return lm, nil

Check warning on line 43 in instrumentation/opentelemetry/internal/metrics/linux_metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/linux_metrics.go#L41-L43

Added lines #L41 - L43 were not covered by tests
}

func (lm *linuxMetrics) getMemory() float64 {
return lm.memory

Check warning on line 47 in instrumentation/opentelemetry/internal/metrics/linux_metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/linux_metrics.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}

func (lm *linuxMetrics) getCPU() float64 {
return lm.cpuSecondsTotal

Check warning on line 51 in instrumentation/opentelemetry/internal/metrics/linux_metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/linux_metrics.go#L50-L51

Added lines #L50 - L51 were not covered by tests
}

func (lm *linuxMetrics) processStatsFromPid(pid int) (*processStats, error) {
Copy link
Contributor

@puneet-traceable puneet-traceable Jul 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no tests for this function? It's fine for this but in general for testing such scenarios

  1. either file should become a parameter to the function and then you can create custom files to test.
  2. can pass an io.Reader to the function to read file. (*os.File) implements an io.Reader. During test you can use a mock version.

procFilepath := filepath.Join("/proc", strconv.Itoa(pid), "stat")
procStatFileBytes, err := os.ReadFile(filepath.Clean(procFilepath))
if err != nil {
return nil, err

Check warning on line 58 in instrumentation/opentelemetry/internal/metrics/linux_metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/linux_metrics.go#L54-L58

Added lines #L54 - L58 were not covered by tests
}
stat, err := lm.parseProcStatFile(procStatFileBytes, procFilepath)
if err != nil {
return nil, err

Check warning on line 62 in instrumentation/opentelemetry/internal/metrics/linux_metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/linux_metrics.go#L60-L62

Added lines #L60 - L62 were not covered by tests
}
return stat, nil

Check warning on line 64 in instrumentation/opentelemetry/internal/metrics/linux_metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/linux_metrics.go#L64

Added line #L64 was not covered by tests
}

// ref: /proc/pid/stat section of https://man7.org/linux/man-pages/man5/proc.5.html
func (lm *linuxMetrics) parseProcStatFile(bytesArr []byte, procFilepath string) (*processStats, error) {
varkey98 marked this conversation as resolved.
Show resolved Hide resolved
infos := strings.Split(string(bytesArr), " ")
if len(infos) != procStatArrayLength {
return nil, fmt.Errorf("%s file could not be parsed", procFilepath)

Check warning on line 71 in instrumentation/opentelemetry/internal/metrics/linux_metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/linux_metrics.go#L71

Added line #L71 was not covered by tests
}
return &processStats{
utime: parseFloat(infos[13]),
stime: parseFloat(infos[14]),
cutime: parseFloat(infos[15]),
cstime: parseFloat(infos[16]),
rss: parseFloat(infos[23]),
}, nil
}

func parseFloat(val string) float64 {
floatVal, _ := strconv.ParseFloat(val, 64)
return floatVal
}

// sysconf for go. claims to work without cgo or external binaries
// https://pkg.go.dev/github.com/tklauser/go-sysconf@v0.3.14#section-readme
func getClockTicks() float64 {
clktck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
if err != nil {
return float64(100)

Check warning on line 92 in instrumentation/opentelemetry/internal/metrics/linux_metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/linux_metrics.go#L92

Added line #L92 was not covered by tests
}
return float64(clktck)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build linux

package metrics

import (
"testing"

"github.com/stretchr/testify/assert"
)

const (
// Mock data simulating /proc/[pid]/stat content
mockData = "85 (md) I 2 0 0 0 -1 698880 0 0 0 0 100 200 300 400 0 -20 1 0 223 0 550 18437615 0 0 0 0 0 0 0 2647 0 1 0 0 17 1 0 0 0 0 0 0 0 0 0 0 0 0 0"
)

func TestParseProcStatFile(t *testing.T) {
lm := &linuxMetrics{}
procFilepath := "/proc/123/stat"

stat, err := lm.parseProcStatFile([]byte(mockData), procFilepath)
assert.NoError(t, err, "unexpected error while parsing proc stat file")

assert.Equal(t, 100.0, stat.utime, "utime does not match expected value")
assert.Equal(t, 200.0, stat.stime, "stime does not match expected value")
assert.Equal(t, 300.0, stat.cutime, "cutime does not match expected value")
assert.Equal(t, 400.0, stat.cstime, "cstime does not match expected value")
assert.Equal(t, 550.0, stat.rss, "rss does not match expected value")
}
58 changes: 58 additions & 0 deletions instrumentation/opentelemetry/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package metrics

import (
"context"
"fmt"
"log"

"go.opentelemetry.io/otel"
varkey98 marked this conversation as resolved.
Show resolved Hide resolved
"go.opentelemetry.io/otel/metric"
)

const meterName = "goagent.hypertrace.org/metrics"

type systemMetrics interface {
getMemory() float64
getCPU() float64
}

func InitializeSystemMetrics() {
meterProvider := otel.GetMeterProvider()
meter := meterProvider.Meter(meterName)
err := setUpMetricRecorder(meter)
if err != nil {
log.Printf("error initializing metrics, failed to setup metric recorder: %v\n", err)

Check warning on line 24 in instrumentation/opentelemetry/internal/metrics/metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/metrics.go#L19-L24

Added lines #L19 - L24 were not covered by tests
}
}

func setUpMetricRecorder(meter metric.Meter) error {
if meter == nil {
return fmt.Errorf("error while setting up metric recorder: meter is nil")

Check warning on line 30 in instrumentation/opentelemetry/internal/metrics/metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/metrics.go#L28-L30

Added lines #L28 - L30 were not covered by tests
}
cpuSeconds, err := meter.Float64ObservableCounter("hypertrace.agent.cpu.seconds.total", metric.WithDescription("Metric to monitor total CPU seconds"))
if err != nil {
return fmt.Errorf("error while setting up cpu seconds metric counter: %v", err)

Check warning on line 34 in instrumentation/opentelemetry/internal/metrics/metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/metrics.go#L32-L34

Added lines #L32 - L34 were not covered by tests
}
memory, err := meter.Float64ObservableGauge("hypertrace.agent.memory", metric.WithDescription("Metric to monitor memory usage"))
if err != nil {
return fmt.Errorf("error while setting up memory metric counter: %v", err)

Check warning on line 38 in instrumentation/opentelemetry/internal/metrics/metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/metrics.go#L36-L38

Added lines #L36 - L38 were not covered by tests
}
// Register the callback function for both cpu_seconds and memory observable gauges
_, err = meter.RegisterCallback(
func(ctx context.Context, result metric.Observer) error {
sysMetrics, err := newSystemMetrics()
if err != nil {
return err

Check warning on line 45 in instrumentation/opentelemetry/internal/metrics/metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/metrics.go#L41-L45

Added lines #L41 - L45 were not covered by tests
}
result.ObserveFloat64(cpuSeconds, sysMetrics.getCPU())
result.ObserveFloat64(memory, sysMetrics.getMemory())
return nil

Check warning on line 49 in instrumentation/opentelemetry/internal/metrics/metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/metrics.go#L47-L49

Added lines #L47 - L49 were not covered by tests
},
cpuSeconds, memory,
)
if err != nil {
log.Fatalf("failed to register callback: %v", err)
return err

Check warning on line 55 in instrumentation/opentelemetry/internal/metrics/metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/metrics.go#L53-L55

Added lines #L53 - L55 were not covered by tests
}
return nil

Check warning on line 57 in instrumentation/opentelemetry/internal/metrics/metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/metrics.go#L57

Added line #L57 was not covered by tests
}
17 changes: 17 additions & 0 deletions instrumentation/opentelemetry/internal/metrics/noop_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build !linux

package metrics

type noopMetrics struct{}

func newSystemMetrics() (systemMetrics, error) {
return &noopMetrics{}, nil
}

func (nm *noopMetrics) getMemory() float64 {
return 0
}

func (nm *noopMetrics) getCPU() float64 {
return 0
}
120 changes: 0 additions & 120 deletions instrumentation/opentelemetry/internal/metrics/system_metrics.go

This file was deleted.

Loading