Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds OTEL Syslog receiver to Agent v3 #932

Draft
wants to merge 5 commits into
base: v3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/collector/factories.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/tailsamplingprocessor"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/syslogreceiver"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/exporter"
Expand Down Expand Up @@ -110,6 +111,7 @@ func createReceiverFactories() (map[component.Type]receiver.Factory, error) {
hostmetricsreceiver.NewFactory(),
nginxreceiver.NewFactory(),
nginxplusreceiver.NewFactory(),
syslogreceiver.NewFactory(),
}

return receiver.MakeFactoryMap(receiverList...)
Expand Down
2 changes: 1 addition & 1 deletion internal/collector/factories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestOTelComponentFactories(t *testing.T) {
require.NoError(t, err, "OTelComponentFactories should not return an error")
assert.NotNil(t, factories, "factories should not be nil")

assert.Len(t, factories.Receivers, 4)
assert.Len(t, factories.Receivers, 5)
assert.Len(t, factories.Processors, 20)
assert.Len(t, factories.Exporters, 4)
assert.Len(t, factories.Extensions, 3)
Expand Down
11 changes: 11 additions & 0 deletions internal/collector/otel_collector_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@
}
}

func (oc *Collector) checkForNewNginxReceivers(nginxConfigContext *model.NginxConfigContext) bool {

Check failure on line 357 in internal/collector/otel_collector_plugin.go

View workflow job for this annotation

GitHub Actions / Lint

cognitive-complexity: function (*Collector).checkForNewNginxReceivers has cognitive complexity 11 (> max enabled 10) (revive)
nginxReceiverFound, reloadCollector := oc.updateExistingNginxPlusReceiver(nginxConfigContext)

if !nginxReceiverFound && nginxConfigContext.PlusAPI != "" {
Expand All @@ -365,6 +365,16 @@
PlusAPI: nginxConfigContext.PlusAPI,
},
)
if nginxConfigContext.Syslog != nil && nginxConfigContext.Syslog.SyslogServer != "" {
oc.config.Collector.Receivers.SyslogReceivers = append(
oc.config.Collector.Receivers.SyslogReceivers,
config.SyslogReceiver{
InstanceID: nginxConfigContext.InstanceID,
Server: nginxConfigContext.Syslog.SyslogServer,
Protocol: "rfc3164", // default value, need to get from the agent conf
},
)
}

reloadCollector = true
} else if nginxConfigContext.PlusAPI == "" {
Expand All @@ -387,6 +397,7 @@
return reloadCollector
}

// Todo: consider update scenario for new syslogreceivers
func (oc *Collector) updateExistingNginxPlusReceiver(
nginxConfigContext *model.NginxConfigContext,
) (nginxReceiverFound, reloadCollector bool) {
Expand Down
12 changes: 12 additions & 0 deletions internal/collector/otel_collector_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ func TestCollector_ProcessNginxConfigUpdateTopic(t *testing.T) {
Network: &config.NetworkScraper{},
},
},
SyslogReceivers: []config.SyslogReceiver{
{
Server: "127.0.0.1:1515",
InstanceID: "1",
Protocol: "rfc3164"},
},
OtlpReceivers: types.OtlpReceivers(),
NginxPlusReceivers: []config.NginxPlusReceiver{
{
Expand Down Expand Up @@ -157,6 +163,12 @@ func TestCollector_ProcessNginxConfigUpdateTopic(t *testing.T) {
},
},
OtlpReceivers: types.OtlpReceivers(),
SyslogReceivers: []config.SyslogReceiver{
{
Server: "127.0.0.1:1515",
InstanceID: "1",
Protocol: "rfc3164"},
},
NginxReceivers: []config.NginxReceiver{
{
InstanceID: "123",
Expand Down
22 changes: 22 additions & 0 deletions internal/collector/otelcol.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
receivers:
{{- range .Receivers.SyslogReceivers }}
syslog/{{- .InstanceID -}}:
tcp:
listen_address: "{{- .Server -}}"
protocol: rfc3164
{{- end }}
{{- if ne .Receivers.HostMetrics nil }}
hostmetrics:
collection_interval: {{ .Receivers.HostMetrics.CollectionInterval }}
Expand Down Expand Up @@ -233,3 +239,19 @@ service:
{{- if ne .Exporters.Debug nil }}
- debug
{{- end }}
logs:
receivers:
{{- range .Receivers.SyslogReceivers }}
- syslog/{{- .InstanceID -}}
{{- end }}
processors:
{{- if ne .Processors.Batch nil }}
- batch
{{- end }}
exporters:
{{- range $index, $otlpExporter := .Exporters.OtlpExporters }}
- otlp/{{$index}}
{{- end }}
{{- if ne .Exporters.Debug nil }}
- debug
{{- end }}
1 change: 1 addition & 0 deletions internal/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

const (
//Todo: Add default Protocol for nap
DefGracefulShutdownPeriod = 5 * time.Second
DefNginxReloadMonitoringPeriod = 10 * time.Second
DefTreatErrorsAsWarnings = false
Expand Down
7 changes: 7 additions & 0 deletions internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ type (
OtlpReceivers []OtlpReceiver `yaml:"-" mapstructure:"otlp_receivers"`
NginxReceivers []NginxReceiver `yaml:"-" mapstructure:"nginx_receivers"`
NginxPlusReceivers []NginxPlusReceiver `yaml:"-" mapstructure:"nginx_plus_receivers"`
SyslogReceivers []SyslogReceiver `yaml:"-" mapstructure:"syslog_receiver"`
}

OtlpReceiver struct {
Expand All @@ -171,6 +172,12 @@ type (
OtlpTLSConfig *OtlpTLSConfig `yaml:"-" mapstructure:"tls"`
}

SyslogReceiver struct {
InstanceID string `yaml:"-" mapstructure:"instance_id"`
Server string `yaml:"-" mapstructure:"host"`
Protocol string `yaml:"-" mapstructure:"protocol"`
}

NginxReceiver struct {
InstanceID string `yaml:"-" mapstructure:"instance_id"`
StubStatus string `yaml:"-" mapstructure:"stub_status"`
Expand Down
7 changes: 7 additions & 0 deletions internal/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
"github.com/nginx/agent/v3/api/grpc/mpi/v1"
)

type NginxConfigContext struct {

Check failure on line 14 in internal/model/config.go

View workflow job for this annotation

GitHub Actions / Lint

fieldalignment: struct with 128 pointer bytes could be 112 (govet)
StubStatus string
PlusAPI string
InstanceID string
Files []*v1.File
AccessLogs []*AccessLog
ErrorLogs []*ErrorLog
Syslog *NAP
}

func (ncc *NginxConfigContext) Equal(otherNginxConfigContext *NginxConfigContext) bool {
Expand Down Expand Up @@ -85,6 +86,12 @@
Readable bool
}

type NAP struct {

Check failure on line 89 in internal/model/config.go

View workflow job for this annotation

GitHub Actions / Lint

fieldalignment: struct with 16 pointer bytes could be 8 (govet)
Enable bool
Syslog bool
SyslogServer string
}

type (
WriteStatus int
)
Expand Down
16 changes: 16 additions & 0 deletions internal/watcher/instance/nginx_config_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"
Expand All @@ -36,6 +37,7 @@ const (
stubStatusAPIDirective = "stub_status"
apiFormat = "http://%s%s"
locationDirective = "location"
napDirective = "app_protect_security_log"
)

type (
Expand Down Expand Up @@ -104,6 +106,7 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
case "log_format":
formatMap = ncp.formatMap(directive)
case "access_log":
fmt.Println("am here")
if !ncp.ignoreLog(directive.Args[0]) {
accessLog := ncp.accessLog(directive.Args[0], ncp.accessLogDirectiveFormat(directive),
formatMap)
Expand All @@ -120,6 +123,19 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
case "ssl_certificate", "proxy_ssl_certificate", "ssl_client_certificate", "ssl_trusted_certificate":
sslCertFile := ncp.sslCert(ctx, directive.Args[0], rootDir)
nginxConfigContext.Files = append(nginxConfigContext.Files, sslCertFile)
case "app_protect_security_log":
if len(directive.Args) > 1 {
syslogArg := directive.Args[1]
re := regexp.MustCompile(`syslog:server=([\S]+)`)
matches := re.FindStringSubmatch(syslogArg)
if len(matches) > 1 {
syslogServer := matches[1]
nginxConfigContext.Syslog.SyslogServer = syslogServer
slog.InfoContext(ctx, "Captured syslog server", "syslog_server", syslogServer)
fmt.Println("syslogServer")
fmt.Println(syslogServer)
}
}
}

return nil
Expand Down
12 changes: 12 additions & 0 deletions test/config/collector/test-opentelemetry-collector-agent.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
receivers:
syslog/1:
tcp:
listen_address: "127.0.0.1:1515"
protocol: rfc3164
hostmetrics:
collection_interval: 1m0s
initial_delay: 1s
Expand Down Expand Up @@ -84,3 +88,11 @@ service:
- otlp/0
- prometheus
- debug
logs:
receivers:
- syslog/1
processors:
- batch
exporters:
- otlp/0
- debug
49 changes: 22 additions & 27 deletions test/docker/nginx-plus/deb/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,47 @@ COPY $ENTRY_POINT /agent/entrypoint.sh
RUN --mount=type=secret,id=nginx-crt,dst=nginx-repo.crt \
--mount=type=secret,id=nginx-key,dst=nginx-repo.key \
set -x \
# Create nginx user/group first, to be consistent throughout Docker variants
# Create nginx user/group first, to be consistent throughout Docker variants
&& groupadd --system --gid 101 nginx \
&& useradd --system --gid nginx --no-create-home --home-dir /nonexistent --uid 101 nginx \
&& apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y \
ca-certificates \
gnupg1 \
gnupg2 \
lsb-release \
git \
wget \
make \
&& \
NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \
found=''; \
for server in \
hkp://keyserver.ubuntu.com:80 \
pgp.mit.edu \
; do \
echo "Fetching GPG key $NGINX_GPGKEY from $server"; \
apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; \
done; \
test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \
apt-get remove --purge --auto-remove -y gnupg1 && rm -rf /var/lib/apt/lists/* \
# Install the latest release of NGINX Plus and/or NGINX Plus modules
# Uncomment individual modules if necessary
# Use versioned packages over defaults to specify a release
&& nginxPackages=" \
nginx-plus \
" \
&& mkdir -p /etc/ssl/nginx \
&& cat nginx-repo.crt > /etc/ssl/nginx/nginx-repo.crt \
&& cat nginx-repo.key > /etc/ssl/nginx/nginx-repo.key \
&& echo "Acquire::https::pkgs.nginx.com::Verify-Peer \"true\";" > /etc/apt/apt.conf.d/90nginx \
&& echo "Acquire::https::pkgs.nginx.com::Verify-Host \"true\";" >> /etc/apt/apt.conf.d/90nginx \
&& echo "Acquire::https::pkgs.nginx.com::SslCert \"/etc/ssl/nginx/nginx-repo.crt\";" >> /etc/apt/apt.conf.d/90nginx \
&& echo "Acquire::https::pkgs.nginx.com::SslKey \"/etc/ssl/nginx/nginx-repo.key\";" >> /etc/apt/apt.conf.d/90nginx \
&& printf "deb https://pkgs.nginx.com/plus/ubuntu `lsb_release -cs` nginx-plus\n" > /etc/apt/sources.list.d/nginx-plus.list \
&& mkdir -p /etc/ssl/nginx \
&& cat nginx-repo.crt > /etc/ssl/nginx/nginx-repo.crt \
&& cat nginx-repo.key > /etc/ssl/nginx/nginx-repo.key \
# Add GPG keys for nginx-plus and app-protect
&& wget -qO - https://cs.nginx.com/static/keys/nginx_signing.key | gpg --dearmor | tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null \
&& wget -qO - https://cs.nginx.com/static/keys/app-protect-security-updates.key | gpg --dearmor | tee /usr/share/keyrings/app-protect-security-updates.gpg >/dev/null \
#&& ls -l /usr/share/keyrings/nginx-archive-keyring.gpg /usr/share/keyrings/app-protect-security-updates.gpg \


# Install the latest release of NGINX Plus and/or NGINX Plus modules
# Set up repository sources for NGINX Plus and App Protect
&& printf "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] https://pkgs.nginx.com/plus/ubuntu `lsb_release -cs` nginx-plus\n" > /etc/apt/sources.list.d/nginx-plus.list \
&& printf "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] https://pkgs.nginx.com/app-protect/ubuntu `lsb_release -cs` nginx-plus\n" | tee /etc/apt/sources.list.d/nginx-app-protect.list \
&& printf "deb [signed-by=/usr/share/keyrings/app-protect-security-updates.gpg] https://pkgs.nginx.com/app-protect-security-updates/ubuntu `lsb_release -cs` nginx-plus\n" | tee -a /etc/apt/sources.list.d/nginx-app-protect.list \
# && wget -P /etc/apt/apt.conf.d https://cs.nginx.com/static/files/90pkgs-nginx \
# && printf "deb [signed-by=/usr/share/keyrings/app-protect-security-updates.gpg] https://pkgs.nginx.com/app-protect/ubuntu `lsb_release -cs` nginx-plus\n" > /etc/apt/sources.list.d/nginx-app-protect.list \
&& apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y \
$nginxPackages \
nginx-plus \
app-protect \
curl \
gettext-base \
jq \
gnupg2 \
&& apt-get remove --purge -y lsb-release \
&& apt-get remove --purge --auto-remove -y && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx-plus.list \
&& apt-get remove --purge --auto-remove -y \
&& rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx-plus.list /etc/apt/sources.list.d/nginx-app-protect.list \
&& rm -rf /etc/apt/apt.conf.d/90nginx /etc/ssl/nginx

EXPOSE 80
Expand Down
7 changes: 7 additions & 0 deletions test/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ func AgentConfig() *config.Config {
},
Receivers: config.Receivers{
OtlpReceivers: OtlpReceivers(),
SyslogReceivers: []config.SyslogReceiver{
{
Server: "127.0.0.1:1515",
InstanceID: "1",
Protocol: "rfc3164",
},
},
HostMetrics: &config.HostMetrics{
CollectionInterval: time.Minute,
InitialDelay: time.Second,
Expand Down
Loading