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 1 commit
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
67 changes: 67 additions & 0 deletions instrumentation/opentelemetry/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package metrics

import (
"context"
"fmt"
"go.opentelemetry.io/otel"
varkey98 marked this conversation as resolved.
Show resolved Hide resolved
"go.opentelemetry.io/otel/metric"
"log"
)

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

type systemMetrics interface {
getMemory() (float64, error)
getCPU() (float64, error)
getCurrentMetrics() error
}

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#L24

Added line #L24 was 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#L30

Added line #L30 was 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#L34

Added line #L34 was 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#L38

Added line #L38 was 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 := newSystemMetrics()
err := sysMetrics.getCurrentMetrics()
varkey98 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L46 was not covered by tests
}
cpus, err := sysMetrics.getCPU()
if err != nil {
return err

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L50 was not covered by tests
}
mem, err := sysMetrics.getMemory()
if err != nil {
return err

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L54 was not covered by tests
}
result.ObserveFloat64(cpuSeconds, cpus)
result.ObserveFloat64(memory, mem)
return nil
},
cpuSeconds, memory,
)
if err != nil {
log.Fatalf("failed to register callback: %v", err)
return err

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

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/metrics.go#L63-L64

Added lines #L63 - L64 were not covered by tests
}
return nil
}
100 changes: 100 additions & 0 deletions instrumentation/opentelemetry/internal/metrics/metricsImpl.go
varkey98 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//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 {
return &linuxMetrics{}
}

func (lm *linuxMetrics) getCurrentMetrics() error {
stats, err := lm.processStatsFromPid(os.Getpid())
if err != nil {
return err

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

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/metricsImpl.go#L42

Added line #L42 was not covered by tests
}
lm.memory = stats.rss * pageSize
lm.cpuSecondsTotal = (stats.stime + stats.utime + stats.cstime + stats.cutime) / clkTck
return nil
}

func (lm *linuxMetrics) getMemory() (float64, error) {
return lm.memory, nil
}

func (lm *linuxMetrics) getCPU() (float64, error) {
return lm.cpuSecondsTotal, nil
}

func (lm *linuxMetrics) processStatsFromPid(pid int) (*processStats, error) {
procFilepath := filepath.Join("/proc", strconv.Itoa(pid), "stat")
var err error
if procStatFileBytes, err := os.ReadFile(filepath.Clean(procFilepath)); err == nil {
if stat, err := lm.parseProcStatFile(procStatFileBytes, procFilepath); err == nil {
if err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/metricsImpl.go#L63

Added line #L63 was not covered by tests
}
return stat, nil
}
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/metricsImpl.go#L67

Added line #L67 was not covered by tests
}
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/metricsImpl.go#L69

Added line #L69 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) {
infos := strings.Split(string(bytesArr), " ")
if len(infos) != procStatArrayLength {
return nil, fmt.Errorf("%s file could not be parsed", procFilepath)

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

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/metricsImpl.go#L76

Added line #L76 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 97 in instrumentation/opentelemetry/internal/metrics/metricsImpl.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/metricsImpl.go#L97

Added line #L97 was not covered by tests
}
return float64(clktck)
}
21 changes: 21 additions & 0 deletions instrumentation/opentelemetry/internal/metrics/metricsNoop.go
varkey98 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build !linux

package metrics

type noopMetrics struct{}

func newSystemMetrics() systemMetrics {
return &noopMetrics{}
}

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

func (nm *noopMetrics) getCPU() (float64, error) {
return 0, nil
}

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

This file was deleted.

Loading