-
Notifications
You must be signed in to change notification settings - Fork 68
/
config.go
121 lines (109 loc) · 3.72 KB
/
config.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
// Copyright (c) 2018 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"time"
sls "github.com/aliyun/aliyun-log-go-sdk"
)
// LogstoreType describes the type of a logstore
type LogstoreType int
const (
// SpanType indicates the logstore is used to store span
SpanType LogstoreType = iota
// DependencyType indicates the logstore is used to store dependency
DependencyType
)
const initEcsTokenTryMax = 10
const initEcsTokenSleep = time.Second * 5
// Configuration describes the configuration properties needed to connect to an AliCloud Log Service cluster
type Configuration struct {
Project string
Endpoint string
AliCloudK8SFlag bool
AccessKeyID string
AccessKeySecret string
SpanLogstore string
SpanAggLogstore string
SpanDepLogstore string
DependencyLogstore string
MaxQueryDuration time.Duration
InitResourceFlag bool
TagAppendRuleFile string
KindRewriteRuleFile string
}
// LogstoreBuilder creates new sls.ClientInterface
type LogstoreBuilder interface {
// NewClient return client, project, logstore, error
NewClient(logstoreType LogstoreType) (sls.ClientInterface, string, string, string, bool, string, string, error)
GetMaxQueryDuration() time.Duration
}
// ApplyDefaults copies settings from source unless its own value is non-zero.
func (c *Configuration) ApplyDefaults(source *Configuration) {
if c.Project == "" {
c.Project = source.Project
}
if c.Endpoint == "" {
c.Endpoint = source.Endpoint
}
if c.AccessKeyID == "" {
c.AccessKeyID = source.AccessKeyID
}
if c.AccessKeySecret == "" {
c.AccessKeySecret = source.AccessKeySecret
}
if c.SpanLogstore == "" {
c.SpanLogstore = source.SpanLogstore
}
if c.SpanAggLogstore == "" {
c.SpanAggLogstore = source.SpanAggLogstore
}
if c.DependencyLogstore == "" {
c.DependencyLogstore = source.DependencyLogstore
}
if c.MaxQueryDuration == 0 {
c.MaxQueryDuration = source.MaxQueryDuration
}
if c.TagAppendRuleFile == "" {
c.TagAppendRuleFile = source.TagAppendRuleFile
}
if c.KindRewriteRuleFile == "" {
c.KindRewriteRuleFile = source.KindRewriteRuleFile
}
}
// NewClient return client, project, logstore, error
func (c *Configuration) NewClient(logstoreType LogstoreType) (client sls.ClientInterface, project string, logstore string, aggLogstore string, initResourceFlag bool, tagAppendRuleFile string, kindRewriteRuleFile string,err error) {
if c.AliCloudK8SFlag {
shutdown := make(chan struct{}, 1)
for i := 0; i < initEcsTokenTryMax; i++ {
client, err = sls.CreateTokenAutoUpdateClient(c.Endpoint, UpdateTokenFunction, shutdown)
if err == nil {
break
}
}
if err != nil {
return nil, "", "", "", true, "", "", err
}
} else {
client = sls.CreateNormalInterface(c.Endpoint, c.AccessKeyID, c.AccessKeySecret, "")
}
// @todo set user agent
//p.UserAgent = userAgent
if logstoreType == SpanType {
return client, c.Project, c.SpanLogstore, c.SpanAggLogstore, c.InitResourceFlag, c.TagAppendRuleFile, c.KindRewriteRuleFile,nil
}
return client, c.Project, c.DependencyLogstore, c.SpanAggLogstore, c.InitResourceFlag, c.TagAppendRuleFile, c.KindRewriteRuleFile,nil
}
func (c *Configuration) GetMaxQueryDuration() time.Duration {
return c.MaxQueryDuration
}