11package diagnostic
22
3- import "context"
3+ import (
4+ "context"
5+ "encoding/json"
6+ "errors"
7+ "strings"
8+ )
9+
10+ type SystemInformationError struct {
11+ Err error `json:"error"`
12+ RawInfo string `json:"rawInfo"`
13+ }
14+
15+ func (err SystemInformationError ) Error () string {
16+ return err .Err .Error ()
17+ }
18+
19+ func (err SystemInformationError ) MarshalJSON () ([]byte , error ) {
20+ s := map [string ]string {
21+ "error" : err .Err .Error (),
22+ "rawInfo" : err .RawInfo ,
23+ }
24+
25+ return json .Marshal (s )
26+ }
27+
28+ type SystemInformationGeneralError struct {
29+ OperatingSystemInformationError error
30+ MemoryInformationError error
31+ FileDescriptorsInformationError error
32+ DiskVolumeInformationError error
33+ }
34+
35+ func (err SystemInformationGeneralError ) Error () string {
36+ builder := & strings.Builder {}
37+ builder .WriteString ("errors found:" )
38+
39+ if err .OperatingSystemInformationError != nil {
40+ builder .WriteString (err .OperatingSystemInformationError .Error () + ", " )
41+ }
42+
43+ if err .MemoryInformationError != nil {
44+ builder .WriteString (err .MemoryInformationError .Error () + ", " )
45+ }
46+
47+ if err .FileDescriptorsInformationError != nil {
48+ builder .WriteString (err .FileDescriptorsInformationError .Error () + ", " )
49+ }
50+
51+ if err .DiskVolumeInformationError != nil {
52+ builder .WriteString (err .DiskVolumeInformationError .Error () + ", " )
53+ }
54+
55+ return builder .String ()
56+ }
57+
58+ func (err SystemInformationGeneralError ) MarshalJSON () ([]byte , error ) {
59+ data := map [string ]SystemInformationError {}
60+
61+ var sysErr SystemInformationError
62+ if errors .As (err .OperatingSystemInformationError , & sysErr ) {
63+ data ["operatingSystemInformationError" ] = sysErr
64+ }
65+
66+ if errors .As (err .MemoryInformationError , & sysErr ) {
67+ data ["memoryInformationError" ] = sysErr
68+ }
69+
70+ if errors .As (err .FileDescriptorsInformationError , & sysErr ) {
71+ data ["fileDescriptorsInformationError" ] = sysErr
72+ }
73+
74+ if errors .As (err .DiskVolumeInformationError , & sysErr ) {
75+ data ["diskVolumeInformationError" ] = sysErr
76+ }
77+
78+ return json .Marshal (data )
79+ }
480
581type DiskVolumeInformation struct {
682 Name string `json:"name"` // represents the filesystem in linux/macos or device name in windows
@@ -17,17 +93,19 @@ func NewDiskVolumeInformation(name string, maximum, current uint64) *DiskVolumeI
1793}
1894
1995type SystemInformation struct {
20- MemoryMaximum uint64 `json:"memoryMaximum"` // represents the maximum memory of the system in kilobytes
21- MemoryCurrent uint64 `json:"memoryCurrent"` // represents the system's memory in use in kilobytes
22- FileDescriptorMaximum uint64 `json:"fileDescriptorMaximum"` // represents the maximum number of file descriptors of the system
23- FileDescriptorCurrent uint64 `json:"fileDescriptorCurrent"` // represents the system's file descriptors in use
24- OsSystem string `json:"osSystem"` // represents the operating system name i.e.: linux, windows, darwin
25- HostName string `json:"hostName"` // represents the system host name
26- OsVersion string `json:"osVersion"` // detailed information about the system's release version level
27- OsRelease string `json:"osRelease"` // detailed information about the system's release
28- Architecture string `json:"architecture"` // represents the system's hardware platform i.e: arm64/amd64
29- CloudflaredVersion string `json:"cloudflaredVersion"` // the runtime version of cloudflared
30- Disk []* DiskVolumeInformation `json:"disk"`
96+ MemoryMaximum uint64 `json:"memoryMaximum,omitempty"` // represents the maximum memory of the system in kilobytes
97+ MemoryCurrent uint64 `json:"memoryCurrent,omitempty"` // represents the system's memory in use in kilobytes
98+ FileDescriptorMaximum uint64 `json:"fileDescriptorMaximum,omitempty"` // represents the maximum number of file descriptors of the system
99+ FileDescriptorCurrent uint64 `json:"fileDescriptorCurrent,omitempty"` // represents the system's file descriptors in use
100+ OsSystem string `json:"osSystem,omitempty"` // represents the operating system name i.e.: linux, windows, darwin
101+ HostName string `json:"hostName,omitempty"` // represents the system host name
102+ OsVersion string `json:"osVersion,omitempty"` // detailed information about the system's release version level
103+ OsRelease string `json:"osRelease,omitempty"` // detailed information about the system's release
104+ Architecture string `json:"architecture,omitempty"` // represents the system's hardware platform i.e: arm64/amd64
105+ CloudflaredVersion string `json:"cloudflaredVersion,omitempty"` // the runtime version of cloudflared
106+ GoVersion string `json:"goVersion,omitempty"`
107+ GoArch string `json:"goArch,omitempty"`
108+ Disk []* DiskVolumeInformation `json:"disk,omitempty"`
31109}
32110
33111func NewSystemInformation (
@@ -40,7 +118,9 @@ func NewSystemInformation(
40118 osVersion ,
41119 osRelease ,
42120 architecture ,
43- cloudflaredVersion string ,
121+ cloudflaredVersion ,
122+ goVersion ,
123+ goArchitecture string ,
44124 disk []* DiskVolumeInformation ,
45125) * SystemInformation {
46126 return & SystemInformation {
@@ -54,17 +134,17 @@ func NewSystemInformation(
54134 osRelease ,
55135 architecture ,
56136 cloudflaredVersion ,
137+ goVersion ,
138+ goArchitecture ,
57139 disk ,
58140 }
59141}
60142
61143type SystemCollector interface {
62144 // If the collection is successful it will return `SystemInformation` struct,
63- // an empty string, and a nil error.
64- // In case there is an error a string with the raw data will be returned
65- // however the returned string not contain all the data points.
145+ // and a nil error.
66146 //
67147 // This function expects that the caller sets the context timeout to prevent
68148 // long-lived collectors.
69- Collect (ctx context.Context ) (* SystemInformation , string , error )
149+ Collect (ctx context.Context ) (* SystemInformation , error )
70150}
0 commit comments