forked from newrelic/go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.go
64 lines (53 loc) · 1.34 KB
/
environment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package newrelic
import (
"encoding/json"
"reflect"
"runtime"
)
// environment describes the application's environment.
type environment struct {
Compiler string `env:"runtime.Compiler"`
GOARCH string `env:"runtime.GOARCH"`
GOOS string `env:"runtime.GOOS"`
Version string `env:"runtime.Version"`
NumCPU int `env:"runtime.NumCPU"`
}
var (
// sampleEnvironment is useful for testing.
sampleEnvironment = environment{
Compiler: "comp",
GOARCH: "arch",
GOOS: "goos",
Version: "vers",
NumCPU: 8,
}
)
// newEnvironment returns a new Environment.
func newEnvironment() environment {
return environment{
Compiler: runtime.Compiler,
GOARCH: runtime.GOARCH,
GOOS: runtime.GOOS,
Version: runtime.Version(),
NumCPU: runtime.NumCPU(),
}
}
// MarshalJSON prepares Environment JSON in the format expected by the collector
// during the connect command.
func (e environment) MarshalJSON() ([]byte, error) {
var arr [][]interface{}
val := reflect.ValueOf(e)
numFields := val.NumField()
arr = make([][]interface{}, numFields)
for i := 0; i < numFields; i++ {
v := val.Field(i)
t := val.Type().Field(i).Tag.Get("env")
arr[i] = []interface{}{
t,
v.Interface(),
}
}
return json.Marshal(arr)
}