Skip to content

Commit a0335a1

Browse files
authored
Merge pull request #2104 from stgraber/main
Include OS metrics on Incus OS
2 parents 34d77db + 29a3b2f commit a0335a1

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

cmd/incusd/api_metrics.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"io"
78
"net"
89
"net/http"
910
"runtime"
@@ -22,6 +23,7 @@ import (
2223
"github.com/lxc/incus/v6/internal/server/request"
2324
"github.com/lxc/incus/v6/internal/server/response"
2425
"github.com/lxc/incus/v6/internal/server/state"
26+
internalutil "github.com/lxc/incus/v6/internal/util"
2527
"github.com/lxc/incus/v6/shared/api"
2628
"github.com/lxc/incus/v6/shared/logger"
2729
)
@@ -367,5 +369,28 @@ func internalMetrics(ctx context.Context, daemonStartTime time.Time, tx *db.Clus
367369
out.AddSamples(metrics.GoStackSysBytes, metrics.Sample{Value: float64(ms.StackSys)})
368370
out.AddSamples(metrics.GoSysBytes, metrics.Sample{Value: float64(ms.Sys)})
369371

372+
// If on Incus OS, include OS metrics.
373+
if internalutil.IsIncusOS() {
374+
client := http.Client{}
375+
client.Transport = &http.Transport{
376+
DialContext: func(_ context.Context, network, addr string) (net.Conn, error) {
377+
return net.DialTimeout("tcp", "127.0.0.1:9100", 50*time.Millisecond)
378+
},
379+
DisableKeepAlives: true,
380+
ExpectContinueTimeout: time.Second * 3,
381+
ResponseHeaderTimeout: time.Second * 3,
382+
}
383+
384+
resp, err := client.Get("http://incus-os/metrics")
385+
if err == nil {
386+
defer resp.Body.Close()
387+
388+
osMetrics, err := io.ReadAll(resp.Body)
389+
if err == nil {
390+
out.AddRaw(osMetrics)
391+
}
392+
}
393+
}
394+
370395
return out
371396
}

cmd/incusd/api_os.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"github.com/lxc/incus/v6/internal/server/auth"
1111
"github.com/lxc/incus/v6/internal/server/response"
12-
"github.com/lxc/incus/v6/shared/util"
12+
internalutil "github.com/lxc/incus/v6/internal/util"
1313
)
1414

1515
var apiOS = APIEndpoint{
@@ -24,7 +24,7 @@ var apiOS = APIEndpoint{
2424

2525
func apiOSProxy(_ *Daemon, r *http.Request) response.Response {
2626
// Check if this is an Incus OS system.
27-
if !util.PathExists("/run/incus-os/unix.socket") {
27+
if !internalutil.IsIncusOS() {
2828
return response.BadRequest(errors.New("System isn't running Incus OS"))
2929
}
3030

internal/server/metrics/metrics.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ func (m *MetricSet) AddSamples(metricType MetricType, samples ...Sample) {
6565
m.set[metricType] = append(m.set[metricType], samples...)
6666
}
6767

68+
// AddRaw allows for adding extra metrics directly to the output without having to parse them first.
69+
func (m *MetricSet) AddRaw(rawData []byte) {
70+
m.suffix = append(m.suffix, rawData...)
71+
}
72+
6873
// Merge merges two MetricSets. Missing labels from m's samples are added to all samples in n.
6974
func (m *MetricSet) Merge(metricSet *MetricSet) {
7075
if metricSet == nil {
@@ -84,6 +89,10 @@ func (m *MetricSet) Merge(metricSet *MetricSet) {
8489
m.set[metricType] = append(m.set[metricType], sample)
8590
}
8691
}
92+
93+
if metricSet.suffix != nil {
94+
m.suffix = append(m.suffix, metricSet.suffix...)
95+
}
8796
}
8897

8998
func (m *MetricSet) String() string {
@@ -158,7 +167,12 @@ func (m *MetricSet) String() string {
158167
}
159168
}
160169

161-
_, err := out.WriteString("# EOF\n")
170+
_, err := out.Write(m.suffix)
171+
if err != nil {
172+
return ""
173+
}
174+
175+
_, err = out.WriteString("# EOF\n")
162176
if err != nil {
163177
return ""
164178
}

internal/server/metrics/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Sample struct {
1010
type MetricSet struct {
1111
set map[MetricType][]Sample
1212
labels map[string]string
13+
suffix []byte
1314
}
1415

1516
// MetricType is a numeric code identifying the metric.

internal/util/os.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package util
2+
3+
import (
4+
"github.com/lxc/incus/v6/shared/util"
5+
)
6+
7+
// IsIncusOS checks if the host system is running Incus OS.
8+
func IsIncusOS() bool {
9+
return util.PathExists("/run/incus-os/unix.socket")
10+
}

0 commit comments

Comments
 (0)