-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (104 loc) · 3.21 KB
/
main.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
120
121
122
123
124
125
package main
import (
"errors"
"flag"
"github.com/aliyun/aliyun-log-jaeger/sls_store"
"github.com/hashicorp/go-hclog"
"github.com/jaegertracing/jaeger/plugin/storage/grpc"
"github.com/jaegertracing/jaeger/plugin/storage/grpc/shared"
"github.com/spf13/viper"
"time"
)
const (
DefaultLookBack = 6
)
var configPath string
type Configuration struct {
Endpoint string `yaml:"endpoint"`
AccessKeyID string `yaml:"accessKeyId"`
AccessSecret string `yaml:"accessSecret"`
Project string `yaml:"project"`
Instance string `yaml:"instance"`
MaxLookBack time.Duration `yaml:maxLookBack`
}
var logger = hclog.New(&hclog.LoggerOptions{
Level: hclog.Info,
Name: "aliyun-log-jaeger-plugin",
JSONFormat: true,
})
func main() {
flag.StringVar(&configPath, "config", "", "Path to the alibaba log jaeger plugin's configuration file")
flag.Parse()
configuration, err := initialParameters(configPath, logger)
if err != nil {
logger.Error("Fatal error config file: %w\n", err)
return
}
var plugin = sls_store.NewSLSStorageForJaegerPlugin(
configuration.Endpoint,
configuration.AccessKeyID,
configuration.AccessSecret,
configuration.Project,
configuration.Instance,
configuration.MaxLookBack,
logger,
)
grpc.Serve(&shared.PluginServices{
Store: plugin,
})
logger.Info("SLS jaeger plugin initialized Successfully")
}
func initialParameters(configPath string, logger hclog.Logger) (*Configuration, error) {
v := viper.New()
v.AutomaticEnv()
if configPath != "" {
v.SetConfigFile(configPath)
if err := v.ReadInConfig(); err != nil {
logger.Error("Failed to read file.", "Exception", err)
return nil, err
}
}
configuration := &Configuration{}
if err := configuration.InitFromViper(v); err != nil {
return nil, err
} else {
return configuration, nil
}
}
func (c *Configuration) InitFromViper(v *viper.Viper) error {
c.AccessSecret = v.GetString("ACCESS_KEY_SECRET")
if c.AccessSecret == "" {
logger.Error("The AccessSecret can't be empty")
return errors.New("The AccessSecret can't be empty")
}
c.AccessKeyID = v.GetString("ACCESS_KEY_ID")
if c.AccessKeyID == "" {
logger.Error("The access key id can't be empty")
return errors.New("The access key id can't be empty")
}
c.Project = v.GetString("PROJECT")
if c.Project == "" {
logger.Error("The project name can't be empty")
return errors.New("The project name can't be empty")
}
c.Endpoint = v.GetString("ENDPOINT")
if c.Endpoint == "" {
logger.Error("The endpoint can't be empty")
return errors.New("The endpoint can't be empty")
}
c.Instance = v.GetString("INSTANCE")
if c.Instance == "" {
logger.Error("The instance can't be empty")
return errors.New("The instance can't be empty")
}
lookBack := v.GetInt32("MAX_LOOK_BACK")
if lookBack == 0 {
lookBack = DefaultLookBack
}
if lookBack > 3*24 {
logger.Warn("Setting a larger value for MAX_LOOK_BACK will affect the query efficiency.", "MAX_LOOK_BACK", lookBack)
}
c.MaxLookBack = time.Duration(lookBack) * time.Hour
logger.Info("Parameters", "AccessSecret", c.AccessSecret, "AccessKeyID", c.AccessKeyID, "Project", c.Project, "Instance", c.Instance, "Endpoint", c.Endpoint, "MaxLookBack", c.MaxLookBack)
return nil
}