-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-application.go
119 lines (104 loc) · 2.49 KB
/
api-application.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2021 冯立强 fenglq@tingyun.com. All rights reserved.
//Package tingyun3 听云性能采集探针(sdk)
package tingyun3
//面向api使用者的接口实现部分
import (
"fmt"
"os"
"github.com/goinline/agent/libs/tystring"
"github.com/goinline/agent/utils/logger"
)
//AppInit : 初始化听云探针
//参数:
// jsonFile: 听云配置文件路径,文件格式为json格式
func tingyunAppInit(jsonFile string) error {
if app == nil {
new_app, err := new(application).init(jsonFile)
if new_app == nil {
return err
}
app = new_app
}
return nil
}
//Running : 检测探针是否启动(为Frameworks提供接口)
//返回值: bool
func tingyunRunning() bool {
return app != nil
}
//AppStop : 停止听云探针
func tingyunAppStop() {
if app == nil {
return
}
app.stop()
app = nil
}
// ConfigRead : 读配置项
func ConfigRead(name string) (interface{}, bool) {
if app == nil {
return nil, false
}
return app.configs.Value(name)
}
// Log : 返回日志对象接口
func Log() *log.Logger {
if app == nil {
return nil
}
return app.logger
}
var configDisabled bool = false
var app *application = nil
var defaultAppName = "GoApp"
func getDefaultAppName() string {
return defaultAppName
}
func checkOneagent() bool {
if tystring.CaseCMP(os.Getenv("TINGYUN_ONEAGENT_GO"), "enable") != 0 {
return false
}
if !fileExist("/opt/tingyun-oneagent/conf/oneagent.conf") {
return false
}
if !fileExist("/opt/tingyun-oneagent/conf/go.conf") {
return false
}
return true
}
var isOneagent = false
func oneagentLogPath() string {
if isOneagent {
if containerID := getContainerID(); len(containerID) > 0 {
return fmt.Sprintf("/opt/tingyun-oneagent/logs/agent/golang-agent-%s-%d.log", containerID, os.Getpid())
} else {
return fmt.Sprintf("/opt/tingyun-oneagent/logs/agent/golang-agent-%d.log", os.Getpid())
}
}
return ""
}
func envGetAppName() string {
return os.Getenv("TINGYUN_GO_APP_NAME")
}
func init() {
listens.init()
//check user defined
configFile := os.Getenv("TINGYUN_GO_APP_CONFIG")
//check oneagent defined
if len(configFile) == 0 {
if checkOneagent() {
isOneagent = true
configFile = "/opt/tingyun-oneagent/conf/oneagent.conf:/opt/tingyun-oneagent/conf/go.conf"
}
}
//default
if len(configFile) == 0 {
configFile = "/etc/tingyun/go_app_config.json"
}
if appname := envGetAppName(); len(appname) > 0 {
defaultAppName = appname
} else {
defaultAppName = getExt(readLink("/proc/self/exe"), '/')
}
tingyunAppInit(configFile)
}