forked from DataDog/datadog-agent
/
logs.go
117 lines (98 loc) · 2.93 KB
/
logs.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2019 Datadog, Inc.
package logs
import (
"errors"
"fmt"
"sync/atomic"
"github.com/ninnemana/datadog-agent/pkg/logs/sender"
"github.com/ninnemana/datadog-agent/pkg/util/log"
"github.com/ninnemana/datadog-agent/pkg/logs/config"
"github.com/ninnemana/datadog-agent/pkg/logs/scheduler"
"github.com/ninnemana/datadog-agent/pkg/logs/service"
"github.com/ninnemana/datadog-agent/pkg/logs/status"
)
const (
// key used to display a warning message on the agent status
invalidProcessingRules = "invalid_global_processing_rules"
invalidEndpoints = "invalid_endpoints"
)
var (
// isRunning indicates whether logs-agent is running or not
isRunning int32
// logs-agent
agent *Agent
// scheduler is plugged to autodiscovery to collect integration configs
// and schedule log collection for different kind of inputs
adScheduler *scheduler.Scheduler
)
// Start starts logs-agent
func Start() error {
if IsAgentRunning() {
return nil
}
// setup the sources and the services
sources := config.NewLogSources()
services := service.NewServices()
// setup the config scheduler
adScheduler = scheduler.NewScheduler(sources, services)
// setup the status
status.Init(&isRunning, sources)
// setup the server config
endpoints, err := sender.BuildEndpoints()
if err != nil {
message := fmt.Sprintf("Invalid endpoints: %v", err)
status.AddGlobalError(invalidEndpoints, message)
return errors.New(message)
}
// setup global processing rules
processingRules, err := config.GlobalProcessingRules()
if err != nil {
message := fmt.Sprintf("Invalid processing rules: %v", err)
status.AddGlobalError(invalidProcessingRules, message)
return errors.New(message)
}
// setup and start the agent
agent = NewAgent(sources, services, processingRules, endpoints)
log.Info("Starting logs-agent...")
agent.Start()
atomic.StoreInt32(&isRunning, 1)
log.Info("logs-agent started")
// add the default sources
for _, source := range config.DefaultSources() {
sources.AddSource(source)
}
return nil
}
// Stop stops properly the logs-agent to prevent data loss,
// it only returns when the whole pipeline is flushed.
func Stop() {
log.Info("Stopping logs-agent")
if IsAgentRunning() {
if agent != nil {
agent.Stop()
agent = nil
}
if adScheduler != nil {
adScheduler.Stop()
adScheduler = nil
}
status.Clear()
atomic.StoreInt32(&isRunning, 0)
}
log.Info("logs-agent stopped")
}
// IsAgentRunning returns true if the logs-agent is running.
func IsAgentRunning() bool {
return status.Get().IsRunning
}
// GetStatus returns logs-agent status
func GetStatus() status.Status {
return status.Get()
}
// GetScheduler returns the logs-config scheduler if set.
func GetScheduler() *scheduler.Scheduler {
return adScheduler
}