Skip to content
This repository was archived by the owner on Jun 14, 2023. It is now read-only.

Commit 78ce775

Browse files
authored
support environment variable (#148)
1 parent 4fe3cf3 commit 78ce775

File tree

18 files changed

+756
-74
lines changed

18 files changed

+756
-74
lines changed

.github/workflows/e2e.yaml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#
2+
# Copyright 2022 SkyAPM org
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
name: E2E
18+
19+
on:
20+
pull_request:
21+
push:
22+
branches:
23+
- master
24+
25+
jobs:
26+
EnvironmentVariable:
27+
name: Environment Variable
28+
runs-on: ubuntu-latest
29+
env:
30+
e2e_file: "test/e2e/cases/environment-variable/e2e.yaml"
31+
steps:
32+
- name: Set up Go 1.16
33+
uses: actions/setup-go@v1
34+
with:
35+
go-version: 1.16
36+
id: go
37+
- uses: actions/checkout@v2
38+
with:
39+
submodules: true
40+
- uses: apache/skywalking-infra-e2e@main
41+
with:
42+
e2e-file: ${e2e_file}
43+
- name: Show Container Logs
44+
if: ${{ failure() }}
45+
run: docker ps -a | grep -v CONTAINER | awk '{print $1}' | xargs -i docker logs {}
46+
- name: Cleanup
47+
if: ${{ failure() }}
48+
run: e2e cleanup -c ${e2e_file}

README.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,33 @@ contextString := logContext.String()
228228

229229
Go to go2sky-plugins repo to see all the plugins, [click here](https://github.com/SkyAPM/go2sky-plugins).
230230

231+
## Supported Environment Variables
232+
233+
Below is the full list of supported environment variables you can set to customize the agent behavior, please read the descriptions for what they can achieve.
234+
235+
| Environment Variable | Description | Default |
236+
|:------------------------------------------------------:|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|:------------------:|
237+
| `SW_AGENT_NAME` | The name of the Go service | unset |
238+
| `SW_AGENT_INSTANCE_NAME` | The name of the Go service instance | Randomly generated |
239+
| `SW_AGENT_SAMPLE` | Sample rate, 1 setting it to 1 means full sampling | 1 |
240+
| `SW_AGENT_COLLECTOR_BACKEND_SERVICES` | The backend OAP server address | unset |
241+
| `SW_AGENT_AUTHENTICATION` | The authentication token to verify that the agent is trusted by the backend OAP, as for how to configure the backend, refer to [the yaml](https://github.com/apache/skywalking/blob/4f0f39ffccdc9b41049903cc540b8904f7c9728e/oap-server/server-bootstrap/src/main/resources/application.yml#L155-L158). | unset |
242+
| `SW_AGENT_COLLECTOR_HEARTBEAT_PERIOD` | Agent heartbeat report period. Unit, second | 20 |
243+
| `SW_AGENT_COLLECTOR_GET_AGENT_DYNAMIC_CONFIG_INTERVAL` | Sniffer get agent dynamic config interval. Unit, second | 20 |
244+
| `SW_AGENT_COLLECTOR_MAX_SEND_QUEUE_SIZE` | Send span queue buffer length | 30000 |
245+
246+
247+
231248
## CDS - Configuration Discovery Service
232249

233250
Configuration Discovery Service provides the dynamic configuration for the agent, defined in gRPC and stored in the backend.
234251

235-
## Available key(s) and value(s) in Golang Agent.
252+
### Available key(s) and value(s) in Golang Agent.
236253
Golang agent supports the following dynamic configurations.
237254

238-
| Config Key | Value Description | Value Format Example |
239-
| :-----------------------: | :----------------------------------------------------------: | :-------------------: |
240-
| agent.sample_rate |The percentage of trace when sampling. It's `[0, 1]`, Same with `WithSampler` parameter.|0.1|
255+
| Config Key | Value Description | Value Format Example |
256+
|:-----------------:|:----------------------------------------------------------------------------------------:|:--------------------:|
257+
| agent.sample_rate | The percentage of trace when sampling. It's `[0, 1]`, Same with `WithSampler` parameter. | 0.1 |
241258

242259
# License
243260
Apache License 2.0. See [LICENSE](LICENSE) file for details.

plugins/http/test/docker-compose.yml

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ services:
3737
build:
3838
context: ../../../
3939
dockerfile: ./plugins/http/test/docker/Dockerfile.http_server
40+
environment:
41+
SW_AGENT_COLLECTOR_GET_AGENT_DYNAMIC_CONFIG_INTERVAL: -1
4042
networks:
4143
- e2e
4244
expose:
@@ -55,6 +57,8 @@ services:
5557
build:
5658
context: ../../../
5759
dockerfile: ./plugins/http/test/docker/Dockerfile.http_client
60+
environment:
61+
SW_AGENT_COLLECTOR_GET_AGENT_DYNAMIC_CONFIG_INTERVAL: -1
5862
expose:
5963
- 8080
6064
ports:

reporter/grpc.go

+22-70
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,35 @@ import (
3939
const (
4040
maxSendQueueSize int32 = 30000
4141
defaultCheckInterval = 20 * time.Second
42+
defaultCDSInterval = 20 * time.Second
4243
defaultLogPrefix = "go2sky-gRPC"
4344
authKey = "Authentication"
4445
)
4546

47+
func applyGRPCReporterOption(r *gRPCReporter, opts ...GRPCReporterOption) error {
48+
// read the options in the environment variable
49+
envOps, err := gRPCReporterOptionsFormEnv()
50+
if err != nil {
51+
return err
52+
}
53+
opts = append(opts, envOps...)
54+
for _, o := range opts {
55+
o(r)
56+
}
57+
return nil
58+
}
59+
4660
// NewGRPCReporter create a new reporter to send data to gRPC oap server. Only one backend address is allowed.
4761
func NewGRPCReporter(serverAddr string, opts ...GRPCReporterOption) (go2sky.Reporter, error) {
4862
r := &gRPCReporter{
4963
logger: logger.NewDefaultLogger(log.New(os.Stderr, defaultLogPrefix, log.LstdFlags)),
5064
sendCh: make(chan *agentv3.SegmentObject, maxSendQueueSize),
5165
checkInterval: defaultCheckInterval,
66+
cdsInterval: defaultCDSInterval, // cds default on
5267
}
53-
for _, o := range opts {
54-
o(r)
68+
69+
if err := applyGRPCReporterOption(r, opts...); err != nil {
70+
return nil, err
5571
}
5672

5773
var credsDialOption grpc.DialOption
@@ -62,11 +78,8 @@ func NewGRPCReporter(serverAddr string, opts ...GRPCReporterOption) (go2sky.Repo
6278
credsDialOption = grpc.WithInsecure()
6379
}
6480

65-
// cds default on
66-
if r.cdsInterval == 0 {
67-
r.cdsInterval = time.Second * 20
68-
}
69-
81+
// read the backend service address in the environment variable
82+
serverAddr = serverAddrFormEnv(serverAddr)
7083
conn, err := grpc.Dial(serverAddr, credsDialOption)
7184
if err != nil {
7285
return nil, err
@@ -81,67 +94,6 @@ func NewGRPCReporter(serverAddr string, opts ...GRPCReporterOption) (go2sky.Repo
8194
return r, nil
8295
}
8396

84-
// GRPCReporterOption allows for functional options to adjust behaviour
85-
// of a gRPC reporter to be created by NewGRPCReporter
86-
type GRPCReporterOption func(r *gRPCReporter)
87-
88-
// WithLogger setup logger for gRPC reporter
89-
// Deprecated: WithLog is recommended
90-
func WithLogger(log *log.Logger) GRPCReporterOption {
91-
return func(r *gRPCReporter) {
92-
r.logger = logger.NewDefaultLogger(log)
93-
}
94-
}
95-
96-
// WithLog setup log for gRPC reporter
97-
func WithLog(logger logger.Log) GRPCReporterOption {
98-
return func(r *gRPCReporter) {
99-
r.logger = logger
100-
}
101-
}
102-
103-
// WithCheckInterval setup service and endpoint registry check interval
104-
func WithCheckInterval(interval time.Duration) GRPCReporterOption {
105-
return func(r *gRPCReporter) {
106-
r.checkInterval = interval
107-
}
108-
}
109-
110-
// WithMaxSendQueueSize setup send span queue buffer length
111-
func WithMaxSendQueueSize(maxSendQueueSize int) GRPCReporterOption {
112-
return func(r *gRPCReporter) {
113-
r.sendCh = make(chan *agentv3.SegmentObject, maxSendQueueSize)
114-
}
115-
}
116-
117-
// WithInstanceProps setup service instance properties eg: org=SkyAPM
118-
func WithInstanceProps(props map[string]string) GRPCReporterOption {
119-
return func(r *gRPCReporter) {
120-
r.instanceProps = props
121-
}
122-
}
123-
124-
// WithTransportCredentials setup transport layer security
125-
func WithTransportCredentials(creds credentials.TransportCredentials) GRPCReporterOption {
126-
return func(r *gRPCReporter) {
127-
r.creds = creds
128-
}
129-
}
130-
131-
// WithAuthentication used Authentication for gRPC
132-
func WithAuthentication(auth string) GRPCReporterOption {
133-
return func(r *gRPCReporter) {
134-
r.md = metadata.New(map[string]string{authKey: auth})
135-
}
136-
}
137-
138-
// WithCDS setup Configuration Discovery Service to dynamic config
139-
func WithCDS(interval time.Duration) GRPCReporterOption {
140-
return func(r *gRPCReporter) {
141-
r.cdsInterval = interval
142-
}
143-
}
144-
14597
type gRPCReporter struct {
14698
service string
14799
serviceInstance string
@@ -308,7 +260,7 @@ func (r *gRPCReporter) initCDS(cdsWatchers []go2sky.AgentConfigChangeWatcher) {
308260

309261
if err != nil {
310262
r.logger.Errorf("fetch dynamic configuration error %v", err)
311-
time.Sleep(r.checkInterval)
263+
time.Sleep(r.cdsInterval)
312264
continue
313265
}
314266

@@ -317,7 +269,7 @@ func (r *gRPCReporter) initCDS(cdsWatchers []go2sky.AgentConfigChangeWatcher) {
317269
r.cdsService.HandleCommand(command)
318270
}
319271

320-
time.Sleep(r.checkInterval)
272+
time.Sleep(r.cdsInterval)
321273
}
322274
}()
323275
}

reporter/grpc_env.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//
2+
// Copyright 2022 SkyAPM org
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
package reporter
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"strconv"
23+
"time"
24+
25+
"github.com/pkg/errors"
26+
)
27+
28+
const (
29+
swAgentAuthentication = "SW_AGENT_AUTHENTICATION"
30+
swAgentCollectorHeartbeatPeriod = "SW_AGENT_COLLECTOR_HEARTBEAT_PERIOD"
31+
swAgentCollectorGetAgentDynamicConfigInterval = "SW_AGENT_COLLECTOR_GET_AGENT_DYNAMIC_CONFIG_INTERVAL"
32+
swAgentCollectorBackendServices = "SW_AGENT_COLLECTOR_BACKEND_SERVICES"
33+
swAgentCollectorMaxSendQueueSize = "SW_AGENT_COLLECTOR_MAX_SEND_QUEUE_SIZE"
34+
)
35+
36+
// serverAddrFormEnv read the backend service address in the environment variable
37+
func serverAddrFormEnv(serverAddr string) string {
38+
if value := os.Getenv(swAgentCollectorBackendServices); value != "" {
39+
return value
40+
}
41+
return serverAddr
42+
}
43+
44+
// gRPCReporterOptionsFormEnv read the options in the environment variable
45+
func gRPCReporterOptionsFormEnv() (opts []GRPCReporterOption, err error) {
46+
if auth := os.Getenv(swAgentAuthentication); auth != "" {
47+
opts = append(opts, WithAuthentication(auth))
48+
}
49+
50+
if value := os.Getenv(swAgentCollectorHeartbeatPeriod); value != "" {
51+
period, err1 := strconv.ParseInt(value, 0, 64)
52+
if err1 != nil {
53+
return nil, errors.Wrap(err1, fmt.Sprintf("%s=%s", swAgentCollectorHeartbeatPeriod, value))
54+
}
55+
opts = append(opts, WithCheckInterval(time.Duration(period)*time.Second))
56+
}
57+
58+
if value := os.Getenv(swAgentCollectorGetAgentDynamicConfigInterval); value != "" {
59+
interval, err1 := strconv.ParseInt(value, 0, 64)
60+
if err1 != nil {
61+
return nil, errors.Wrap(err1, fmt.Sprintf("%s=%s", swAgentCollectorGetAgentDynamicConfigInterval, value))
62+
}
63+
opts = append(opts, WithCDS(time.Duration(interval)*time.Second))
64+
}
65+
66+
if value := os.Getenv(swAgentCollectorMaxSendQueueSize); value != "" {
67+
size, err1 := strconv.ParseInt(value, 0, 64)
68+
if err1 != nil {
69+
return nil, err
70+
}
71+
opts = append(opts, WithMaxSendQueueSize(int(size)))
72+
}
73+
return
74+
}

0 commit comments

Comments
 (0)