Skip to content

Postgresql discovery config - add OTel native receiver #3957

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

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
84f2d77
remove smart agent postgresql configs and use native receiver for config
greatestusername Nov 20, 2023
0fa2afb
write bundled test for otel postgresql receiver
greatestusername Nov 20, 2023
edba6de
ordering
greatestusername Nov 20, 2023
f082aca
Merge branch 'signalfx:main' into postgresql-discovery-config
greatestusername-splunk Nov 20, 2023
702ad46
Merge branch 'signalfx:main' into postgresql-discovery-config
greatestusername-splunk Nov 21, 2023
16db21e
adjust windows bundles
greatestusername Nov 21, 2023
a53318f
remove old bunded test and add k8s bits to otel native bundled test
greatestusername Nov 21, 2023
c0ec51d
Merge branch 'signalfx:main' into postgresql-discovery-config
greatestusername-splunk Nov 22, 2023
38293b7
re-add smartagent-postgresql discovery with enabled set to false
greatestusername Nov 27, 2023
133061d
Merge branch 'signalfx:main' into postgresql-discovery-config
greatestusername-splunk Nov 27, 2023
e63cd1e
Merge branch 'postgresql-discovery-config' of https://github.com/grea…
greatestusername Nov 27, 2023
dfb1f13
Merge branch 'signalfx:main' into postgresql-discovery-config
greatestusername-splunk Dec 1, 2023
3167875
add back smartagent postgresql bundled_test
greatestusername Dec 1, 2023
23370bc
gofmt
greatestusername Dec 1, 2023
10369d1
Merge branch 'signalfx:main' into postgresql-discovery-config
greatestusername-splunk Dec 11, 2023
65ef8dc
Merge branch 'signalfx:main' into postgresql-discovery-config
greatestusername-splunk Dec 12, 2023
4a15e95
Update internal/confmapprovider/discovery/bundle/bundle.d/receivers/p…
greatestusername-splunk Dec 13, 2023
0e22df7
reference existing testdata files and remove copies.
greatestusername Dec 13, 2023
09ea3a6
update properties.discovery.yaml.example
greatestusername Dec 14, 2023
c12d747
Merge remote-tracking branch 'upstream/main' into postgresql-discover…
greatestusername Dec 14, 2023
299fe78
fix for bundledfs
greatestusername Dec 14, 2023
33e0bd6
fix alpha ordering
greatestusername Dec 14, 2023
f71ee95
Merge branch 'signalfx:main' into postgresql-discovery-config
greatestusername-splunk Dec 14, 2023
307e68a
Merge branch 'signalfx:main' into postgresql-discovery-config
greatestusername-splunk Dec 18, 2023
0389e2d
add breaking change to postgresql discovery in changelog and update d…
greatestusername Dec 18, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,22 @@
# Please note that any changes made to this file will need to be reconciled during #
# upgrades of the Collector. #
#####################################################################################
# smartagent/postgresql:
# postgresql:
# enabled: true
# rule:
# docker_observer: type == "container" and any([name, image, command], {# matches "(?i)postgres"}) and not (command matches "splunk.discovery")
# host_observer: type == "hostport" and command matches "(?i)postgres" and not (command matches "splunk.discovery")
# k8s_observer: type == "port" and pod.name matches "(?i)postgres"
# config:
# default:
# type: postgresql
# connectionString: 'sslmode=disable user={{.username}} password={{.password}}'
# params:
# username: splunk.discovery.default
# password: splunk.discovery.default
# masterDBName: splunk.discovery.default
# default: {}
# status:
# metrics:
# successful:
# - strict: postgres_query_count
# - strict: postgresql.commits
# first_only: true
# log_record:
# severity_text: info
# body: PostgreSQL receiver is working!
# partial:
# - strict: postgres_rows_inserted
# first_only: true
# log_record:
# severity_text: info
# body: >-
# Make sure that
# `CREATE EXTENSION IF NOT EXISTS pg_stat_statements;`
# has been run for each database you would like to monitor.
# For example:
# `psql --dbname "<db-name>" -c "CREATE EXTENSION IF NOT EXISTS pg_stat_statements;"`
# statements:
# failed:
# - regexp: 'connect: network is unreachable'
Expand All @@ -61,19 +44,28 @@
# append_pattern: true
# body: >-
# Please ensure your user credentials are correctly specified with
# `--set splunk.discovery.receivers.smartagent/postgresql.config.params::username="<username>"` and
# `--set splunk.discovery.receivers.smartagent/postgresql.config.params::password="<password>"` or
# `SPLUNK_DISCOVERY_RECEIVERS_smartagent_x2f_postgresql_CONFIG_params_x3a__x3a_username="<username>"` and
# `SPLUNK_DISCOVERY_RECEIVERS_smartagent_x2f_postgresql_CONFIG_params_x3a__x3a_password="<password>"` environment variables.
# `--set splunk.discovery.receivers.postgresql.config.username="<username>"` and
# `--set splunk.discovery.receivers.postgresql.config.password="<password>"` or
# `SPLUNK_DISCOVERY_RECEIVERS_postgresql_CONFIG_username="<username>"` and
# `SPLUNK_DISCOVERY_RECEIVERS_postgresql_CONFIG_password="<password>"` environment variables.
# - regexp: 'pq: database .* does not exist'
# first_only: true
# log_record:
# severity_text: info
# append_pattern: true
# body: >-
# Make sure the target database is correctly specified using the
# `--set splunk.discovery.receivers.smartagent/postgresql.config.masterDBName="<db>"` command or the
# `SPLUNK_DISCOVERY_RECEIVERS_smartagent_x2f_postgresql_CONFIG_masterDBName="<db>"` environment variable.
# `--set splunk.discovery.receivers.postgresql.config.databases="[<db>]"` command or the
# `SPLUNK_DISCOVERY_RECEIVERS_postgresql_CONFIG_databases="[<db>]"` environment variable.
# - regexp: 'pq: SSL is not enabled on the server'
# first_only: true
# log_record:
# severity_text: info
# append_pattern: true
# body: >-
# Make sure the target database has SSL enabled or set insecure using the
# `--set splunk.discovery.receivers.postgresql.config.tls::insecure="<boolean>"` command or the
# `SPLUNK_DISCOVERY_RECEIVERS_postgresql_CONFIG_tls_x3a__x3a_insecure="<boolean>"` environment variable.
# - regexp: 'pq: pg_stat_statements must be loaded via shared_preload_libraries'
# first_only: true
# log_record:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,22 @@
# Do not edit manually! #
# All changes must be made to associated .tmpl file before running 'make bundle.d'. #
#####################################################################################
smartagent/postgresql:
postgresql:
enabled: true
rule:
docker_observer: type == "container" and any([name, image, command], {# matches "(?i)postgres"}) and not (command matches "splunk.discovery")
host_observer: type == "hostport" and command matches "(?i)postgres" and not (command matches "splunk.discovery")
k8s_observer: type == "port" and pod.name matches "(?i)postgres"
config:
default:
type: postgresql
connectionString: 'sslmode=disable user={{.username}} password={{.password}}'
params:
username: splunk.discovery.default
password: splunk.discovery.default
masterDBName: splunk.discovery.default
default: {}
status:
metrics:
successful:
- strict: postgres_query_count
- strict: postgresql.commits
first_only: true
log_record:
severity_text: info
body: PostgreSQL receiver is working!
partial:
- strict: postgres_rows_inserted
first_only: true
log_record:
severity_text: info
body: >-
Make sure that
`CREATE EXTENSION IF NOT EXISTS pg_stat_statements;`
has been run for each database you would like to monitor.
For example:
`psql --dbname "<db-name>" -c "CREATE EXTENSION IF NOT EXISTS pg_stat_statements;"`
statements:
failed:
- regexp: 'connect: network is unreachable'
Expand All @@ -57,19 +40,28 @@ smartagent/postgresql:
append_pattern: true
body: >-
Please ensure your user credentials are correctly specified with
`--set splunk.discovery.receivers.smartagent/postgresql.config.params::username="<username>"` and
`--set splunk.discovery.receivers.smartagent/postgresql.config.params::password="<password>"` or
`SPLUNK_DISCOVERY_RECEIVERS_smartagent_x2f_postgresql_CONFIG_params_x3a__x3a_username="<username>"` and
`SPLUNK_DISCOVERY_RECEIVERS_smartagent_x2f_postgresql_CONFIG_params_x3a__x3a_password="<password>"` environment variables.
`--set splunk.discovery.receivers.postgresql.config.username="<username>"` and
`--set splunk.discovery.receivers.postgresql.config.password="<password>"` or
`SPLUNK_DISCOVERY_RECEIVERS_postgresql_CONFIG_username="<username>"` and
`SPLUNK_DISCOVERY_RECEIVERS_postgresql_CONFIG_password="<password>"` environment variables.
- regexp: 'pq: database .* does not exist'
first_only: true
log_record:
severity_text: info
append_pattern: true
body: >-
Make sure the target database is correctly specified using the
`--set splunk.discovery.receivers.smartagent/postgresql.config.masterDBName="<db>"` command or the
`SPLUNK_DISCOVERY_RECEIVERS_smartagent_x2f_postgresql_CONFIG_masterDBName="<db>"` environment variable.
`--set splunk.discovery.receivers.postgresql.config.databases="[<db>]"` command or the
`SPLUNK_DISCOVERY_RECEIVERS_postgresql_CONFIG_databases="[<db>]"` environment variable.
- regexp: 'pq: SSL is not enabled on the server'
first_only: true
log_record:
severity_text: info
append_pattern: true
body: >-
Make sure the target database has SSL enabled or set insecure using the
`--set splunk.discovery.receivers.postgresql.config.tls::insecure="<boolean>"` command or the
`SPLUNK_DISCOVERY_RECEIVERS_postgresql_CONFIG_tls_x3a__x3a_insecure="<boolean>"` environment variable.
- regexp: 'pq: pg_stat_statements must be loaded via shared_preload_libraries'
first_only: true
log_record:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
{{ receiver "smartagent/postgresql" }}:
{{ receiver "postgresql" }}:
enabled: true
rule:
docker_observer: type == "container" and any([name, image, command], {# matches "(?i)postgres"}) and not (command matches "splunk.discovery")
host_observer: type == "hostport" and command matches "(?i)postgres" and not (command matches "splunk.discovery")
k8s_observer: type == "port" and pod.name matches "(?i)postgres"
config:
default:
type: postgresql
connectionString: 'sslmode=disable user={{ "{{.username}}" }} password={{ "{{.password}}" }}'
params:
username: {{ defaultValue }}
password: {{ defaultValue }}
masterDBName: {{ defaultValue }}
default: {}
status:
metrics:
successful:
- strict: postgres_query_count
- strict: postgresql.commits
first_only: true
log_record:
severity_text: info
body: PostgreSQL receiver is working!
partial:
- strict: postgres_rows_inserted
first_only: true
log_record:
severity_text: info
body: >-
Make sure that
`CREATE EXTENSION IF NOT EXISTS pg_stat_statements;`
has been run for each database you would like to monitor.
For example:
`psql --dbname "<db-name>" -c "CREATE EXTENSION IF NOT EXISTS pg_stat_statements;"`
statements:
failed:
- regexp: 'connect: network is unreachable'
Expand All @@ -53,19 +36,28 @@
append_pattern: true
body: >-
Please ensure your user credentials are correctly specified with
`--set {{ configProperty "params" "username" "<username>" }}` and
`--set {{ configProperty "params" "password" "<password>" }}` or
`{{ configPropertyEnvVar "params" "username" "<username>" }}` and
`{{ configPropertyEnvVar "params" "password" "<password>" }}` environment variables.
`--set {{ configProperty "username" "<username>" }}` and
`--set {{ configProperty "password" "<password>" }}` or
`{{ configPropertyEnvVar "username" "<username>" }}` and
`{{ configPropertyEnvVar "password" "<password>" }}` environment variables.
- regexp: 'pq: database .* does not exist'
first_only: true
log_record:
severity_text: info
append_pattern: true
body: >-
Make sure the target database is correctly specified using the
`--set {{ configProperty "masterDBName" "<db>" }}` command or the
`{{ configPropertyEnvVar "masterDBName" "<db>" }}` environment variable.
`--set {{ configProperty "databases" "[<db>]" }}` command or the
`{{ configPropertyEnvVar "databases" "[<db>]" }}` environment variable.
- regexp: 'pq: SSL is not enabled on the server'
first_only: true
log_record:
severity_text: info
append_pattern: true
body: >-
Make sure the target database has SSL enabled or set insecure using the
`--set {{ configProperty "tls::insecure" "<boolean>" }}` command or the
`{{ configPropertyEnvVar "tls::insecure" "<boolean>" }}` environment variable.
- regexp: 'pq: pg_stat_statements must be loaded via shared_preload_libraries'
first_only: true
log_record:
Expand Down
4 changes: 2 additions & 2 deletions internal/confmapprovider/discovery/bundle/bundle_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
// TODO: add a pre-bundle.d step that generates these generate directives for easier maintenance/management across platforms
//go:generate go install github.com/signalfx/splunk-otel-collector/internal/confmapprovider/discovery/bundle/cmd/discoverybundler

//go:generate discoverybundler -r -t bundle.d/receivers/smartagent-postgresql.discovery.yaml.tmpl
//go:generate discoverybundler -r -c -d ../../../../cmd/otelcol/config/collector/config.d.linux/receivers -t bundle.d/receivers/smartagent-postgresql.discovery.yaml.tmpl
//go:generate discoverybundler -r -t bundle.d/receivers/smartagent-collectd-mysql.discovery.yaml.tmpl
//go:generate discoverybundler -r -c -d ../../../../cmd/otelcol/config/collector/config.d.linux/receivers -t bundle.d/receivers/smartagent-collectd-mysql.discovery.yaml.tmpl
//go:generate discoverybundler -r -t bundle.d/receivers/smartagent-collectd-nginx.discovery.yaml.tmpl
//go:generate discoverybundler -r -c -d ../../../../cmd/otelcol/config/collector/config.d.linux/receivers -t bundle.d/receivers/smartagent-collectd-nginx.discovery.yaml.tmpl
//go:generate discoverybundler -r -t bundle.d/receivers/postgresql.discovery.yaml.tmpl
//go:generate discoverybundler -r -c -d ../../../../cmd/otelcol/config/collector/config.d.linux/receivers -t bundle.d/receivers/postgresql.discovery.yaml.tmpl
//go:generate discoverybundler -r -t bundle.d/receivers/oracledb.discovery.yaml.tmpl
//go:generate discoverybundler -r -c -d ../../../../cmd/otelcol/config/collector/config.d.linux/receivers -t bundle.d/receivers/oracledb.discovery.yaml.tmpl
//go:generate discoverybundler -r -t bundle.d/receivers/redis.discovery.yaml.tmpl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ func TestBundleDir(t *testing.T) {
require.NoError(t, err)
require.Equal(t, []string{
"bundle.d/receivers/oracledb.discovery.yaml",
"bundle.d/receivers/postgresql.discovery.yaml",
"bundle.d/receivers/redis.discovery.yaml",
"bundle.d/receivers/smartagent-collectd-mysql.discovery.yaml",
"bundle.d/receivers/smartagent-collectd-nginx.discovery.yaml",
"bundle.d/receivers/smartagent-postgresql.discovery.yaml",
}, receivers)

extensions, err := fs.Glob(BundledFS, "bundle.d/extensions/*.discovery.yaml")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ import (
//
//go:embed bundle.d/extensions/*.discovery.yaml
//go:embed bundle.d/receivers/oracledb.discovery.yaml
//go:embed bundle.d/receivers/postgresql.discovery.yaml
//go:embed bundle.d/receivers/redis.discovery.yaml
//go:embed bundle.d/receivers/smartagent-postgresql.discovery.yaml
var BundledFS embed.FS
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func TestBundleDir(t *testing.T) {
require.NoError(t, err)
require.Equal(t, []string{
"bundle.d/receivers/oracledb.discovery.yaml",
"bundle.d/receivers/postgresql.discovery.yaml",
"bundle.d/receivers/redis.discovery.yaml",
"bundle.d/receivers/smartagent-postgresql.discovery.yaml",
}, receivers)

extensions, err := fs.Glob(BundledFS, "bundle.d/extensions/*.discovery.yaml")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,41 @@ import (
"github.com/signalfx/splunk-otel-collector/tests/testutils/kubeutils/manifests"
)

func TestPostgresqlDockerObserver(t *testing.T) {
testutils.SkipIfNotContainerTest(t)
if runtime.GOOS == "darwin" {
t.Skip("unable to share sockets between mac and d4m vm: https://github.com/docker/for-mac/issues/483#issuecomment-758836836")
}

testutils.AssertAllMetricsReceived(t, "bundled.yaml", "otlp_exporter.yaml",
postgresqldb, []testutils.CollectorBuilder{
func(c testutils.Collector) testutils.Collector {
cc := c.(*testutils.CollectorContainer)
cc.Container = cc.Container.WithBinds("/var/run/docker.sock:/var/run/docker.sock:ro")
cc.Container = cc.Container.WillWaitForLogs("Discovering for next")
cc.Container = cc.Container.WithUser(fmt.Sprintf("999:%d", testutils.GetDockerGID(t)))
return cc
},
func(collector testutils.Collector) testutils.Collector {
return collector.WithEnv(map[string]string{
"SPLUNK_DISCOVERY_DURATION": "10s",
// confirm that debug logging doesn't affect runtime
"SPLUNK_DISCOVERY_LOG_LEVEL": "debug",
}).WithArgs(
"--discovery",
"--set", "splunk.discovery.receivers.postgresql.config.username=otelu",
"--set", "splunk.discovery.receivers.postgresql.config.password=otelp",
"--set", "splunk.discovery.receivers.postgresql.config.tls::insecure=true",
//Disabling postgresql.backends metric that doesn't dependably show up in metrics during testing
"--set", "splunk.discovery.receivers.postgresql.config.metrics::postgresql.backends::enabled=false",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was strange. During testing I'd have a 50/50 shot of this postgresql.backends metric showing up or not when pulling metrics for comparison.
Tried longer startup times and different methods of generating connections to try to get this metric to consistently show up but couldn't find any real rhyme or reason.

Open to ideas if it is imperative we have this enabled for testing.

"--set", `splunk.discovery.extensions.k8s_observer.enabled=false`,
"--set", `splunk.discovery.extensions.host_observer.enabled=false`,
)
},
},
)
}

func TestK8sObserver(t *testing.T) {
testutils.SkipIfNotContainerTest(t)
tc := testutils.NewTestcase(t, testutils.OTLPReceiverSinkAllInterfaces)
Expand Down Expand Up @@ -76,30 +111,30 @@ func TestK8sObserver(t *testing.T) {
sout, serr, err = cluster.Apply(manifests.RenderAll(t, configMap, ds))
require.NoError(t, err, "stdout: %s, stderr: %s", sout, serr)

pods := cluster.WaitForPods(ds.Name, namespace.Name, 5*time.Minute)
pods := cluster.WaitForPods(ds.Name, namespace.Name, 10*time.Minute)
require.Len(t, pods, 1)
collectorName := pods[0].Name

expectedMetrics := tc.ResourceMetrics("all.yaml")
expectedMetrics := tc.ResourceMetrics("all_k8s.yaml")
require.NoError(t, tc.OTLPReceiverSink.AssertAllMetricsReceived(t, *expectedMetrics, 30*time.Second))

stdOut, stdErr, err := cluster.Kubectl("logs", "-n", namespace.Name, collectorName)
require.NoError(t, err)
require.Contains(
t, stdOut.String(),
fmt.Sprintf(`Successfully discovered "smartagent/postgresql" using "k8s_observer" endpoint "k8s_observer/%s/(5432)`, postgresUID),
fmt.Sprintf(`Successfully discovered "postgresql" using "k8s_observer" endpoint "k8s_observer/%s/(5432)`, postgresUID),
stdErr.String(),
)
}

type testCluster struct{ *kubeutils.KindCluster }

func (cluster testCluster) createPostgres(name, namespace, serviceAccount string) string {
dbsql, err := os.ReadFile(filepath.Join(".", "testdata", "server", "initdb.d", "db.sql"))
dbsql, err := os.ReadFile(filepath.Join(".", "testdata", "server_k8s", "initdb.d", "db.sql"))
require.NoError(cluster.Testcase, err)
cmContent := map[string]any{"db.sql": string(dbsql)}

initsh, err := os.ReadFile(filepath.Join(".", "testdata", "server", "initdb.d", "init.sh"))
initsh, err := os.ReadFile(filepath.Join(".", "testdata", "server_k8s", "initdb.d", "init.sh"))
require.NoError(cluster.Testcase, err)
cmContent["init.sh"] = string(initsh)

Expand Down Expand Up @@ -231,7 +266,7 @@ func (cluster testCluster) createPostgres(name, namespace, serviceAccount string
)
require.NoError(cluster.Testcase, err)

cluster.WaitForPods(postgres.Name, namespace, 5*time.Minute)
cluster.WaitForPods(postgres.Name, namespace, 10*time.Minute)
return string(postgres.UID)
}

Expand Down Expand Up @@ -332,10 +367,11 @@ func (cluster testCluster) daemonSet(namespace, serviceAccount, configMap string
Image: testutils.GetCollectorImageOrSkipTest(cluster.Testcase),
Command: []string{
"/otelcol", "--config=/config/config.yaml", "--discovery",
"--set", "splunk.discovery.receivers.smartagent/postgresql.config.params::username='${env:PG_USERNAME}'",
"--set", "splunk.discovery.receivers.smartagent/postgresql.config.params::password='${env:PG_PASSWORD}'",
"--set", "splunk.discovery.receivers.smartagent/postgresql.config.masterDBName=test_db",
"--set", `splunk.discovery.receivers.smartagent/postgresql.config.extraMetrics=["*"]`,
"--set", "splunk.discovery.receivers.postgresql.config.username='${env:PG_USERNAME}'",
"--set", "splunk.discovery.receivers.postgresql.config.password='${env:PG_PASSWORD}'",
"--set", "splunk.discovery.receivers.postgresql.config.tls::insecure=true",
//Disabling postgresql.backends metric that doesn't dependably show up in metrics during testing
"--set", "splunk.discovery.receivers.postgresql.config.metrics::postgresql.backends::enabled=false",
},
Env: []corev1.EnvVar{
{Name: "PG_USERNAME", Value: "test_user"},
Expand Down
6 changes: 6 additions & 0 deletions tests/receivers/postgresql/testdata/client/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ARG POSTGRES_VERSION=13-alpine
FROM postgres:${POSTGRES_VERSION}

COPY requests.sh /usr/local/bin/requests.sh

CMD ["requests.sh"]
Loading