mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-31 22:23:14 +02:00
Apply metrics patch from main
This commit is contained in:
@@ -3,6 +3,8 @@ package period
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
)
|
||||
|
||||
type Entries[T any] struct {
|
||||
@@ -73,7 +75,7 @@ type entriesJSON[T any] struct {
|
||||
}
|
||||
|
||||
func (e *Entries[T]) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(entriesJSON[T]{
|
||||
return sonic.Marshal(entriesJSON[T]{
|
||||
Entries: e.Get(),
|
||||
Interval: e.interval,
|
||||
})
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/rs/zerolog/log"
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
"github.com/yusing/goutils/synk"
|
||||
@@ -96,7 +97,7 @@ func (p *Poller[T, AggregateT]) save() error {
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
err = json.NewEncoder(f).Encode(p.period)
|
||||
err = sonic.ConfigDefault.NewEncoder(f).Encode(p.period)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -152,19 +153,24 @@ func (s *SystemInfo) collectDisksInfo(ctx context.Context, lastResult *SystemInf
|
||||
}
|
||||
}
|
||||
|
||||
partitions, err := disk.PartitionsWithContext(ctx, false)
|
||||
partitions, err := disk.PartitionsWithContext(ctx, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.Disks = make(map[string]disk.UsageStat, len(partitions))
|
||||
errs := gperr.NewBuilder("failed to get disks info")
|
||||
for _, partition := range partitions {
|
||||
if !shouldCollectPartition(partition) {
|
||||
continue
|
||||
}
|
||||
diskInfo, err := disk.UsageWithContext(ctx, partition.Mountpoint)
|
||||
if err != nil {
|
||||
errs.Add(err)
|
||||
continue
|
||||
}
|
||||
s.Disks[partition.Device] = diskInfo
|
||||
key := diskKey(partition)
|
||||
s.Disks[key] = diskInfo
|
||||
}
|
||||
|
||||
if errs.HasError() {
|
||||
@@ -176,6 +182,41 @@ func (s *SystemInfo) collectDisksInfo(ctx context.Context, lastResult *SystemInf
|
||||
return nil
|
||||
}
|
||||
|
||||
func shouldCollectPartition(partition disk.PartitionStat) bool {
|
||||
if partition.Mountpoint == "/" {
|
||||
return true
|
||||
}
|
||||
|
||||
if partition.Mountpoint == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
// includes WSL mounts like /mnt/c, but exclude /mnt/ itself and /mnt/wsl*
|
||||
if len(partition.Mountpoint) >= len("/mnt/") &&
|
||||
strings.HasPrefix(partition.Mountpoint, "/mnt/") &&
|
||||
!strings.HasPrefix(partition.Mountpoint, "/mnt/wsl") {
|
||||
return true
|
||||
}
|
||||
|
||||
if strings.HasPrefix(partition.Device, "/dev/") {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func diskKey(partition disk.PartitionStat) string {
|
||||
if partition.Device == "" || partition.Device == "none" {
|
||||
return partition.Mountpoint
|
||||
}
|
||||
|
||||
if partition.Device == "/dev/root" {
|
||||
return partition.Mountpoint
|
||||
}
|
||||
|
||||
return partition.Device
|
||||
}
|
||||
|
||||
func (s *SystemInfo) collectNetworkInfo(ctx context.Context, lastResult *SystemInfo) error {
|
||||
networkIO, err := net.IOCountersWithContext(ctx, false)
|
||||
if err != nil {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/shirou/gopsutil/v4/disk"
|
||||
"github.com/shirou/gopsutil/v4/mem"
|
||||
"github.com/shirou/gopsutil/v4/net"
|
||||
@@ -80,12 +81,12 @@ var (
|
||||
|
||||
func TestSystemInfo(t *testing.T) {
|
||||
// Test marshaling
|
||||
data, err := json.Marshal(testInfo)
|
||||
data, err := sonic.Marshal(testInfo)
|
||||
expect.NoError(t, err)
|
||||
|
||||
// Test unmarshaling back
|
||||
var decoded SystemInfo
|
||||
err = json.Unmarshal(data, &decoded)
|
||||
err = sonic.Unmarshal(data, &decoded)
|
||||
expect.NoError(t, err)
|
||||
|
||||
// Compare original and decoded
|
||||
@@ -137,7 +138,7 @@ func TestSerialize(t *testing.T) {
|
||||
for _, query := range allQueries {
|
||||
t.Run(string(query), func(t *testing.T) {
|
||||
_, result := aggregate(entries, url.Values{"aggregate": []string{string(query)}})
|
||||
s, err := json.Marshal(result)
|
||||
s, err := sonic.Marshal(result)
|
||||
expect.NoError(t, err)
|
||||
var v []map[string]any
|
||||
expect.NoError(t, json.Unmarshal(s, &v))
|
||||
|
||||
@@ -2,13 +2,13 @@ package uptime
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"math"
|
||||
"net/url"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/lithammer/fuzzysearch/fuzzy"
|
||||
config "github.com/yusing/godoxy/internal/config/types"
|
||||
entrypoint "github.com/yusing/godoxy/internal/entrypoint/types"
|
||||
@@ -54,7 +54,7 @@ func getStatuses(ctx context.Context, _ StatusByAlias) (StatusByAlias, error) {
|
||||
}
|
||||
|
||||
func (s *Status) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(map[string]any{
|
||||
return sonic.Marshal(map[string]any{
|
||||
"status": s.Status.String(),
|
||||
"latency": s.Latency,
|
||||
"timestamp": s.Timestamp,
|
||||
@@ -158,5 +158,5 @@ func (rs RouteStatuses) aggregate(limit int, offset int) Aggregated {
|
||||
}
|
||||
|
||||
func (result Aggregated) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal([]RouteAggregate(result))
|
||||
return sonic.Marshal([]RouteAggregate(result))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user