@@ -16,6 +16,9 @@ import (
16
16
"slices"
17
17
"strconv"
18
18
"strings"
19
+ "time"
20
+
21
+ "github.com/nginx/agent/v3/internal/datasource/file"
19
22
20
23
uuidLibrary "github.com/nginx/agent/v3/pkg/id"
21
24
selfsignedcerts "github.com/nginx/agent/v3/pkg/tls"
@@ -115,6 +118,8 @@ func ResolveConfig() (*Config, error) {
115
118
Labels : resolveLabels (),
116
119
}
117
120
121
+ checkCollectorConfiguration (collector , config )
122
+
118
123
slog .Debug ("Agent config" , "config" , config )
119
124
slog .Info ("Enabled features" , "features" , config .Features )
120
125
slog .Info ("Excluded files from being watched for file changes" , "exclude_files" ,
@@ -123,6 +128,60 @@ func ResolveConfig() (*Config, error) {
123
128
return config , nil
124
129
}
125
130
131
+ func checkCollectorConfiguration (collector * Collector , config * Config ) {
132
+ if isOTelExporterConfigured (collector ) && config .IsGrpcClientConfigured () && config .IsAuthConfigured () &&
133
+ config .IsTLSConfigured () {
134
+ slog .Info ("No collector configuration found in NGINX Agent config, command server configuration found." +
135
+ "Using default collector configuration" )
136
+ defaultCollector (collector , config )
137
+ }
138
+ }
139
+
140
+ func defaultCollector (collector * Collector , config * Config ) {
141
+ token := config .Command .Auth .Token
142
+ if config .Command .Auth .TokenPath != "" {
143
+ slog .Debug ("Reading token from file" , "path" , config .Command .Auth .TokenPath )
144
+ pathToken , err := file .ReadFromFile (config .Command .Auth .TokenPath )
145
+ if err != nil {
146
+ slog .Error ("Error adding token to default collector, " +
147
+ "default collector configuration not started" , "error" , err )
148
+
149
+ return
150
+ }
151
+ token = pathToken
152
+ }
153
+
154
+ collector .Receivers .HostMetrics = & HostMetrics {
155
+ Scrapers : & HostMetricsScrapers {
156
+ CPU : & CPUScraper {},
157
+ Disk : & DiskScraper {},
158
+ Filesystem : & FilesystemScraper {},
159
+ Memory : & MemoryScraper {},
160
+ Network : nil ,
161
+ },
162
+ CollectionInterval : 1 * time .Minute ,
163
+ InitialDelay : 1 * time .Second ,
164
+ }
165
+
166
+ collector .Exporters .OtlpExporters = append (collector .Exporters .OtlpExporters , OtlpExporter {
167
+ Server : config .Command .Server ,
168
+ TLS : config .Command .TLS ,
169
+ Compression : "" ,
170
+ Authenticator : "headers_setter" ,
171
+ })
172
+
173
+ header := []Header {
174
+ {
175
+ Action : "insert" ,
176
+ Key : "authorization" ,
177
+ Value : token ,
178
+ },
179
+ }
180
+ collector .Extensions .HeadersSetter = & HeadersSetter {
181
+ Headers : header ,
182
+ }
183
+ }
184
+
126
185
func setVersion (version , commit string ) {
127
186
RootCommand .Version = version + "-" + commit
128
187
viperInstance .SetDefault (VersionKey , version )
@@ -911,3 +970,8 @@ func resolveMapStructure(key string, object any) error {
911
970
912
971
return nil
913
972
}
973
+
974
+ func isOTelExporterConfigured (collector * Collector ) bool {
975
+ return len (collector .Exporters .OtlpExporters ) == 0 && collector .Exporters .PrometheusExporter == nil &&
976
+ collector .Exporters .Debug == nil
977
+ }
0 commit comments