Skip to content

Commit 69b27ae

Browse files
committed
embedded otel collector
1 parent 19ac8d1 commit 69b27ae

File tree

14 files changed

+138
-34
lines changed

14 files changed

+138
-34
lines changed

internal/collector/nginxossreceiver/internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424
type Config struct {
2525
confighttp.ClientConfig `mapstructure:",squash"`
2626
APIDetails APIDetails `mapstructure:"api_details"`
27+
InstanceID string `mapstructure:"instance_id"`
2728
AccessLogs []AccessLog `mapstructure:"access_logs"`
2829
MetricsBuilderConfig metadata.MetricsBuilderConfig `mapstructure:",squash"`
2930
scraperhelper.ControllerConfig `mapstructure:",squash"`

internal/collector/nginxossreceiver/internal/scraper/accesslog/nginx_log_scraper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (nls *NginxLogScraper) Scrape(_ context.Context) (pmetric.Metrics, error) {
171171
nls.entries = make([]*entry.Entry, 0)
172172
timeNow := pcommon.NewTimestampFromTime(time.Now())
173173

174-
nls.rb.SetInstanceID(nls.settings.ID.Name())
174+
nls.rb.SetInstanceID(nls.cfg.InstanceID)
175175
nls.rb.SetInstanceType("nginx")
176176
nls.logger.Debug("NGINX OSS access log resource info", zap.Any("resource", nls.rb))
177177

internal/collector/nginxossreceiver/internal/scraper/stubstatus/stub_status_scraper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (s *NginxStubStatusScraper) Scrape(context.Context) (pmetric.Metrics, error
138138
return pmetric.Metrics{}, err
139139
}
140140

141-
s.rb.SetInstanceID(s.settings.ID.Name())
141+
s.rb.SetInstanceID(s.cfg.InstanceID)
142142
s.rb.SetInstanceType("nginx")
143143
s.settings.Logger.Debug("NGINX OSS stub status resource info", zap.Any("resource", s.rb))
144144

internal/collector/nginxplusreceiver/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const defaultCollectInterval = 10 * time.Second
2121
type Config struct {
2222
confighttp.ClientConfig `mapstructure:",squash"`
2323
APIDetails APIDetails `mapstructure:"api_details"`
24+
InstanceID string `mapstructure:"instance_id"`
2425
MetricsBuilderConfig metadata.MetricsBuilderConfig `mapstructure:",squash"`
2526
scraperhelper.ControllerConfig `mapstructure:",squash"`
2627
}

internal/collector/nginxplusreceiver/scraper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (nps *NginxPlusScraper) Scrape(ctx context.Context) (pmetric.Metrics, error
127127
return pmetric.Metrics{}, fmt.Errorf("failed to get stats from plus API: %w", err)
128128
}
129129

130-
nps.rb.SetInstanceID(nps.settings.ID.Name())
130+
nps.rb.SetInstanceID(nps.cfg.InstanceID)
131131
nps.rb.SetInstanceType("nginxplus")
132132
nps.logger.Debug("NGINX Plus resource info", zap.Any("resource", nps.rb))
133133

internal/collector/otel_collector_plugin.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import (
1616
"sync"
1717
"time"
1818

19+
"github.com/goccy/go-yaml"
1920
pkgConfig "github.com/nginx/agent/v3/pkg/config"
21+
"go.opentelemetry.io/collector/confmap"
2022

2123
"github.com/nginx/agent/v3/api/grpc/mpi/v1"
2224
"github.com/nginx/agent/v3/internal/backoff"
@@ -384,6 +386,31 @@ func (oc *Collector) updateHeadersSetterExtension(
384386
return headersSetterExtensionUpdated
385387
}
386388

389+
func (oc *Collector) writeRunningConfig(ctx context.Context, settings otelcol.CollectorSettings) error {
390+
slog.DebugContext(ctx, "Writing running OTel collector config", "path",
391+
"/var/lib/nginx-agent/opentelemetry-collector-agent-debug.yaml")
392+
resolver, err := confmap.NewResolver(settings.ConfigProviderSettings.ResolverSettings)
393+
if err != nil {
394+
return fmt.Errorf("unable to create resolver: %w", err)
395+
}
396+
397+
con, err := resolver.Resolve(ctx)
398+
if err != nil {
399+
return fmt.Errorf("error while resolving config: %w", err)
400+
}
401+
b, err := yaml.Marshal(con.ToStringMap())
402+
if err != nil {
403+
return fmt.Errorf("error while marshaling to YAML: %w", err)
404+
}
405+
406+
writeErr := os.WriteFile("/var/lib/nginx-agent/opentelemetry-collector-agent-debug.yaml", b, filePermission)
407+
if writeErr != nil {
408+
return fmt.Errorf("error while writing debug config: %w", err)
409+
}
410+
411+
return nil
412+
}
413+
387414
func (oc *Collector) restartCollector(ctx context.Context) {
388415
err := oc.Close(ctx)
389416
if err != nil {
@@ -392,6 +419,14 @@ func (oc *Collector) restartCollector(ctx context.Context) {
392419
}
393420

394421
settings := OTelCollectorSettings(oc.config)
422+
423+
if strings.ToLower(oc.config.Log.Level) == "debug" {
424+
writeErr := oc.writeRunningConfig(ctx, settings)
425+
if writeErr != nil {
426+
slog.ErrorContext(ctx, "Failed to write debug OTel Collector config", "error", writeErr)
427+
}
428+
}
429+
395430
oTelCollector, err := otelcol.NewCollector(settings)
396431
if err != nil {
397432
slog.ErrorContext(ctx, "Failed to create OTel Collector", "error", err)

internal/collector/otelcol.tmpl

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ receivers:
7979
{{- end }}
8080

8181
{{- range .Receivers.NginxReceivers }}
82+
{{- if gt (len $.Receivers.NginxReceivers) 1 }}
8283
nginx/{{- .InstanceID -}}:
84+
{{- else }}
85+
nginx:
86+
{{- end}}
87+
instance_id: "{{- .InstanceID -}}"
8388
api_details:
8489
url: "{{- .StubStatus.URL -}}"
8590
listen: "{{- .StubStatus.Listen -}}"
@@ -98,12 +103,17 @@ receivers:
98103
{{- end }}
99104

100105
{{- range .Receivers.NginxPlusReceivers }}
106+
{{- if gt (len $.Receivers.NginxPlusReceivers) 1 }}
101107
nginxplus/{{- .InstanceID -}}:
108+
{{- else }}
109+
nginxplus:
110+
{{- end}}
111+
instance_id: "{{- .InstanceID -}}"
102112
api_details:
103-
url: "{{- .PlusAPI.URL -}}"
104-
listen: "{{- .PlusAPI.Listen -}}"
105-
location: "{{- .PlusAPI.Location -}}"
106-
ca: "{{- .PlusAPI.Ca -}}"
113+
url: "{{- .PlusAPI.URL -}}"
114+
listen: "{{- .PlusAPI.Listen -}}"
115+
location: "{{- .PlusAPI.Location -}}"
116+
ca: "{{- .PlusAPI.Ca -}}"
107117
{{- if .CollectionInterval }}
108118
collection_interval: {{ .CollectionInterval }}
109119
{{- end }}
@@ -274,10 +284,18 @@ service:
274284
{{- end }}
275285
{{- else if eq $receiver "nginx_metrics" }}
276286
{{- range $.Receivers.NginxReceivers }}
287+
{{- if gt (len $.Receivers.NginxReceivers) 1 }}
277288
- nginx/{{- .InstanceID -}}
289+
{{- else }}
290+
- nginx
291+
{{- end }}
278292
{{- end }}
279293
{{- range $.Receivers.NginxPlusReceivers }}
294+
{{- if gt (len $.Receivers.NginxReceivers) 1 }}
280295
- nginxplus/{{- .InstanceID -}}
296+
{{- else }}
297+
- nginxplus
298+
{{- end }}
281299
{{- end }}
282300
{{- else }}
283301
- {{ $receiver }}

internal/collector/settings.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ func createConverterFactories() []confmap.ConverterFactory {
7171
}
7272

7373
func createURIs(cfg *config.Config) []string {
74-
return []string{cfg.Collector.ConfigPath}
74+
configFiles := []string{cfg.Collector.ConfigPath}
75+
configFiles = slices.Concat(configFiles, cfg.Collector.AdditionalPaths)
76+
slog.Info("Additional config files:", "", configFiles)
77+
78+
return configFiles
7579
}
7680

7781
func createFile(confPath string) error {

internal/collector/settings_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package collector
77
import (
88
"os"
99
"path/filepath"
10+
"slices"
1011
"testing"
1112
"time"
1213

@@ -107,6 +108,28 @@ func TestTemplateWrite(t *testing.T) {
107108
},
108109
},
109110
})
111+
112+
cfg.Collector.Receivers.NginxPlusReceivers = slices.Concat(cfg.Collector.Receivers.NginxPlusReceivers,
113+
[]config.NginxPlusReceiver{
114+
{
115+
InstanceID: "456",
116+
PlusAPI: config.APIDetails{
117+
URL: "http://localhost:80/api",
118+
Listen: "",
119+
Location: "",
120+
},
121+
CollectionInterval: 20 * time.Second,
122+
},
123+
{
124+
InstanceID: "789",
125+
PlusAPI: config.APIDetails{
126+
URL: "http://localhost:90/api",
127+
Listen: "",
128+
Location: "",
129+
},
130+
CollectionInterval: 20 * time.Second,
131+
},
132+
})
110133
// Clear default config and test collector with TLS enabled
111134
cfg.Collector.Receivers.OtlpReceivers["default"] = &config.OtlpReceiver{
112135
Server: &config.ServerConfig{
@@ -165,7 +188,10 @@ func TestTemplateWrite(t *testing.T) {
165188

166189
cfg.Collector.Pipelines.Metrics = make(map[string]*config.Pipeline)
167190
cfg.Collector.Pipelines.Metrics["default"] = &config.Pipeline{
168-
Receivers: []string{"hostmetrics", "containermetrics", "otlp/default", "nginx/123"},
191+
Receivers: []string{
192+
"hostmetrics", "containermetrics",
193+
"otlp/default", "nginx", "nginxplus/456", "nginxplus/789",
194+
},
169195
Processors: []string{"resource/default", "batch/default"},
170196
Exporters: []string{"otlp/default", "prometheus", "debug"},
171197
}

internal/config/config.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,14 +1061,14 @@ func resolveCollector(allowedDirs []string) (*Collector, error) {
10611061
}
10621062

10631063
col := &Collector{
1064-
ConfigPath: viperInstance.GetString(CollectorConfigPathKey),
1064+
ConfigPath: viperInstance.GetString(CollectorConfigPathKey),
10651065
AdditionalPaths: viperInstance.GetStringSlice(CollectorAdditionalConfigPathsKey),
1066-
Exporters: exporters,
1067-
Processors: resolveProcessors(),
1068-
Receivers: receivers,
1069-
Extensions: resolveExtensions(),
1070-
Log: resolveCollectorLog(),
1071-
Pipelines: resolvePipelines(),
1066+
Exporters: exporters,
1067+
Processors: resolveProcessors(),
1068+
Receivers: receivers,
1069+
Extensions: resolveExtensions(),
1070+
Log: resolveCollectorLog(),
1071+
Pipelines: resolvePipelines(),
10721072
}
10731073

10741074
// Check for self-signed certificate true in Agent conf

0 commit comments

Comments
 (0)