Skip to content

Commit 1560ee2

Browse files
committed
Add postgres receiver
1 parent 8768a8e commit 1560ee2

File tree

10 files changed

+233
-0
lines changed

10 files changed

+233
-0
lines changed

docs/components.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ The distribution offers support for the following components.
3232
| [mongodbatlas](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/mongodbatlasreceiver) | [beta] |
3333
| [oracledb](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/oracledbreceiver) | [alpha] |
3434
| [otlp](https://github.com/open-telemetry/opentelemetry-collector/tree/main/receiver/otlpreceiver) | [stable] |
35+
| [postgres](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/postgresqlreceiver) | [beta] |
3536
| [prometheusexecreceiver](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/prometheusexecreceiver) | [deprecated] |
3637
| [prometheusreceiver](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/prometheusreceiver) | [beta] |
3738
| [receiver_creator](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/receivercreator) | [beta] |

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ require (
6363
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kubeletstatsreceiver v0.70.0
6464
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasreceiver v0.70.0
6565
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/oracledbreceiver v0.70.0
66+
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver v0.70.0
6667
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusexecreceiver v0.70.0
6768
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver v0.70.0
6869
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/receivercreator v0.70.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,6 +1853,8 @@ github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasr
18531853
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasreceiver v0.70.0/go.mod h1:DmBckzGHUUi1qB2f7E7M9VQvtchzI1+DoXgKjIDJd0U=
18541854
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/oracledbreceiver v0.70.0 h1:17Ug4A2BoXjE9SJt67hte4PWY1anarQtxMXm+c2QuIg=
18551855
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/oracledbreceiver v0.70.0/go.mod h1:jINAWAwWVty1IxYZub4UB98Vsnoi2qfFK7r603zWkTg=
1856+
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver v0.70.0 h1:Ws4JdAGRDRVsF5+Gaod+m2VaMxNwF3cB77dKZFQ4BQs=
1857+
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver v0.70.0/go.mod h1:O8Xv/zy8aFzHdb7TmWmGgTRxrOw6fTgSNcUx0LT2J64=
18561858
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusexecreceiver v0.70.0 h1:O249Az9ujYbN8qDUpif9XGXGsXJah/AH4PAp8vEN9nU=
18571859
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusexecreceiver v0.70.0/go.mod h1:YxlNYCwYY3SQjPmJlH7t/SeJEcURlTPcYFw0z3kkQvk=
18581860
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver v0.70.0 h1:XNEvkCcP3B/EXATT3GN9SlgUgvso8Pge9iNR8L45hnw=

internal/components/components.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import (
6161
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kubeletstatsreceiver"
6262
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasreceiver"
6363
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/oracledbreceiver"
64+
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver"
6465
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusexecreceiver"
6566
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver"
6667
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/receivercreator"
@@ -141,6 +142,7 @@ func Get() (otelcol.Factories, error) {
141142
mongodbatlasreceiver.NewFactory(),
142143
oracledbreceiver.NewFactory(),
143144
otlpreceiver.NewFactory(),
145+
postgresqlreceiver.NewFactory(),
144146
prometheusexecreceiver.NewFactory(),
145147
prometheusreceiver.NewFactory(),
146148
receivercreator.NewFactory(),

internal/components/components_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func TestDefaultComponents(t *testing.T) {
6060
"mongodbatlas",
6161
"oracledb",
6262
"otlp",
63+
"postgresql",
6364
"prometheus",
6465
"prometheus_exec",
6566
"prometheus_simple",
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright Splunk, Inc.
2+
// Copyright The OpenTelemetry Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
//go:build integration
17+
18+
package tests
19+
20+
import (
21+
"path"
22+
"testing"
23+
"time"
24+
25+
"github.com/stretchr/testify/require"
26+
27+
"github.com/signalfx/splunk-otel-collector/tests/testutils"
28+
)
29+
30+
var postgresqldb = []testutils.Container{testutils.NewContainer().WithContext(
31+
path.Join(".", "testdata", "server"),
32+
).WithName("postgresqldb").WithExposedPorts("15432:5432").WillWaitForLogs("database system is ready to accept connections")}
33+
34+
// This test ensures the collector can connect to a PostgreSQL DB, and properly get metrics. It's not intended to
35+
// test the receiver itself.
36+
func TestPostgresqlDBIntegration(t *testing.T) {
37+
tc := testutils.NewTestcase(t)
38+
defer tc.PrintLogsOnFailure()
39+
defer tc.ShutdownOTLPReceiverSink()
40+
41+
_, stop := tc.Containers(postgresqldb...)
42+
defer stop()
43+
44+
_, shutdown := tc.SplunkOtelCollector(
45+
"all_metrics_config.yaml",
46+
func(collector testutils.Collector) testutils.Collector {
47+
return collector.WithEnv(map[string]string{
48+
"POSTGRESQLDB_ENDPOINT": "localhost:15432",
49+
"POSTGRESQLDB_USERNAME": "otelu",
50+
"POSTGRESQLDB_PASSWORD": "otelp",
51+
})
52+
},
53+
)
54+
defer shutdown()
55+
56+
expectedResourceMetrics := tc.ResourceMetrics("all.yaml")
57+
require.NoError(t, tc.OTLPReceiverSink.AssertAllMetricsReceived(t, *expectedResourceMetrics, 30*time.Second))
58+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
receivers:
2+
postgresql:
3+
endpoint: "${POSTGRESQLDB_ENDPOINT}"
4+
username: "${POSTGRESQLDB_USERNAME}"
5+
password: "${POSTGRESQLDB_PASSWORD}"
6+
databases:
7+
- otel
8+
- otel2
9+
tls:
10+
insecure: true
11+
exporters:
12+
otlp:
13+
endpoint: "${OTLP_ENDPOINT}"
14+
tls:
15+
insecure: true
16+
17+
18+
service:
19+
telemetry:
20+
logs:
21+
level: "debug"
22+
pipelines:
23+
metrics:
24+
receivers:
25+
- postgresql
26+
exporters: [otlp]
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
resource_metrics:
2+
- attributes:
3+
postgresql.database.name: <ANY>
4+
scope_metrics:
5+
- instrumentation_scope:
6+
name: otelcol/postgresqlreceiver
7+
version: <ANY>
8+
metrics:
9+
- name: postgresql.commits
10+
type: IntMonotonicCumulativeSum
11+
- name: postgresql.db_size
12+
type: IntNonmonotonicCumulativeSum
13+
- name: postgresql.rollbacks
14+
type: IntMonotonicCumulativeSum
15+
- name: postgresql.table.count
16+
type: IntNonmonotonicCumulativeSum
17+
- attributes:
18+
postgresql.database.name: <ANY>
19+
postgresql.index.name: <ANY>
20+
postgresql.table.name: <ANY>
21+
scope_metrics:
22+
- instrumentation_scope:
23+
name: otelcol/postgresqlreceiver
24+
version: <ANY>
25+
metrics:
26+
- name: postgresql.index.scans
27+
type: IntMonotonicCumulativeSum
28+
- name: postgresql.index.size
29+
type: IntGauge
30+
- attributes:
31+
postgresql.database.name: <ANY>
32+
postgresql.table.name: <ANY>
33+
scope_metrics:
34+
- instrumentation_scope:
35+
name: otelcol/postgresqlreceiver
36+
version: <ANY>
37+
metrics:
38+
- name: postgresql.blocks_read
39+
type: IntMonotonicCumulativeSum
40+
attributes:
41+
source: heap_read
42+
- name: postgresql.blocks_read
43+
type: IntMonotonicCumulativeSum
44+
attributes:
45+
source: heap_hit
46+
- name: postgresql.blocks_read
47+
type: IntMonotonicCumulativeSum
48+
attributes:
49+
source: idx_read
50+
- name: postgresql.blocks_read
51+
type: IntMonotonicCumulativeSum
52+
attributes:
53+
source: idx_hit
54+
- name: postgresql.blocks_read
55+
type: IntMonotonicCumulativeSum
56+
attributes:
57+
source: toast_hit
58+
- name: postgresql.blocks_read
59+
type: IntMonotonicCumulativeSum
60+
attributes:
61+
source: tidx_read
62+
- name: postgresql.blocks_read
63+
type: IntMonotonicCumulativeSum
64+
attributes:
65+
source: tidx_hit
66+
- name: postgresql.operations
67+
type: IntMonotonicCumulativeSum
68+
attributes:
69+
operation: del
70+
- name: postgresql.operations
71+
type: IntMonotonicCumulativeSum
72+
attributes:
73+
operation: upd
74+
- name: postgresql.operations
75+
type: IntMonotonicCumulativeSum
76+
attributes:
77+
operation: hot_upd
78+
- name: postgresql.rows
79+
type: IntNonmonotonicCumulativeSum
80+
attributes:
81+
state: dead
82+
- name: postgresql.rows
83+
type: IntNonmonotonicCumulativeSum
84+
attributes:
85+
state: live
86+
- name: postgresql.table.size
87+
type: IntNonmonotonicCumulativeSum
88+
- name: postgresql.table.vacuum.count
89+
type: IntMonotonicCumulativeSum
90+
- attributes: {}
91+
scope_metrics:
92+
- instrumentation_scope:
93+
name: otelcol/postgresqlreceiver
94+
version: <ANY>
95+
metrics:
96+
- name: postgresql.bgwriter.buffers.allocated
97+
type: IntMonotonicCumulativeSum
98+
- name: postgresql.bgwriter.buffers.writes
99+
type: IntMonotonicCumulativeSum
100+
- name: postgresql.bgwriter.checkpoint.count
101+
type: IntMonotonicCumulativeSum
102+
- name: postgresql.bgwriter.duration
103+
type: DoubleMonotonicCumulativeSum
104+
- name: postgresql.bgwriter.maxwritten
105+
type: IntMonotonicCumulativeSum
106+
- name: postgresql.connection.max
107+
type: IntGauge
108+
- name: postgresql.database.count
109+
type: IntNonmonotonicCumulativeSum
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM postgres:15-alpine
2+
3+
ENV POSTGRES_USER=root
4+
ENV POSTGRES_PASSWORD=otel
5+
ENV POSTGRES_DB=otel
6+
7+
COPY initPostgresqlDB.sql /docker-entrypoint-initdb.d/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
CREATE USER otelu WITH PASSWORD 'otelp';
2+
GRANT SELECT ON pg_stat_database TO otelu;
3+
4+
CREATE TABLE table1 (
5+
id serial PRIMARY KEY
6+
);
7+
CREATE TABLE table2 (
8+
id serial PRIMARY KEY
9+
);
10+
11+
CREATE DATABASE otel2 OWNER otelu;
12+
\c otel2
13+
CREATE TABLE test1 (
14+
id serial PRIMARY KEY
15+
);
16+
CREATE TABLE test2 (
17+
id serial PRIMARY KEY
18+
);
19+
20+
CREATE INDEX otelindex ON test1(id);
21+
CREATE INDEX otel2index ON test2(id);
22+
23+
-- Generating usage of index
24+
INSERT INTO test2 (id)
25+
VALUES(67);
26+
SELECT * FROM test2;

0 commit comments

Comments
 (0)