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

Make replication stats return whole number #28824

Merged
Show file tree
Hide file tree
Changes from 3 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
27 changes: 27 additions & 0 deletions .chloggen/replication-stats-conversion-failure.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: deprecation

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: postgresqlreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Deprecation of replication lag metrics in favor of more precise 'write_ms','replay_ms' and 'flush_ms'"
djaglowski marked this conversation as resolved.
Show resolved Hide resolved

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [26714]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
84 changes: 75 additions & 9 deletions receiver/postgresqlreceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ import (
"github.com/lib/pq"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/featuregate"
"go.uber.org/multierr"
)

const lagMetricsInSecondsFeatureGateID = "postgresqlreceiver.lagmetricsinseconds"

var lagInSecondsFG = featuregate.GlobalRegistry().MustRegister(
lagMetricsInSecondsFeatureGateID,
featuregate.StageDeprecated,
featuregate.WithRegisterDescription("Metrics 'flush', 'replay' and 'write' are replaced by 'flush_ms','replay_ms' and 'write_ms'."),
)

// databaseName is a name that refers to a database so that it can be uniquely referred to later
// i.e. database1
type databaseName string
Expand Down Expand Up @@ -64,6 +73,10 @@ type postgreSQLConfig struct {
tls configtls.TLSClientSetting
}

func init() {
featuregate.GlobalRegistry().Set(lagInSecondsFG.ID(), true)
}

func sslConnectionString(tls configtls.TLSClientSetting) string {
if tls.Insecure {
return "sslmode='disable'"
Expand Down Expand Up @@ -484,18 +497,24 @@ func (c *postgreSQLClient) getMaxConnections(ctx context.Context) (int64, error)
type replicationStats struct {
clientAddr string
pendingBytes int64
flushLag int64
replayLag int64
writeLag int64
flushLag int64 // Deprecated
replayLag int64 // Deprecated
writeLag int64 // Deprecated
flushLagMs int64
replayLagMs int64
writeLagMs int64
}

func (c *postgreSQLClient) getReplicationStats(ctx context.Context) ([]replicationStats, error) {
func (c *postgreSQLClient) getDeprecatedReplicationStats(ctx context.Context) ([]replicationStats, error) {
query := `SELECT
client_addr,
coalesce(pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn), -1) AS replication_bytes_pending,
extract('epoch' from coalesce(write_lag, '-1 seconds')),
extract('epoch' from coalesce(flush_lag, '-1 seconds')),
extract('epoch' from coalesce(replay_lag, '-1 seconds'))
extract('epoch' from coalesce(write_lag, '-1 seconds'))::integer,
extract('epoch' from coalesce(flush_lag, '-1 seconds'))::integer,
extract('epoch' from coalesce(replay_lag, '-1 seconds'))::integer,
(extract('epoch' from coalesce(write_lag, '-0.001 seconds')) * 1000)::integer AS write_lag_ms,
(extract('epoch' from coalesce(flush_lag, '-0.001 seconds')) * 1000)::integer AS flush_lag_ms,
(extract('epoch' from coalesce(replay_lag, '-0.001 seconds')) * 1000)::integer AS replay_lag_ms
FROM pg_stat_replication;
`
rows, err := c.client.QueryContext(ctx, query)
Expand All @@ -507,8 +526,12 @@ func (c *postgreSQLClient) getReplicationStats(ctx context.Context) ([]replicati
var errors error
for rows.Next() {
var client string
var replicationBytes, writeLag, flushLag, replayLag int64
err = rows.Scan(&client, &replicationBytes, &writeLag, &flushLag, &replayLag)
var replicationBytes int64
var writeLag, flushLag, replayLag int64
var writeLagMs, flushLagMs, replayLagMs int64
err = rows.Scan(&client, &replicationBytes,
&writeLag, &flushLag, &replayLag,
&writeLagMs, &flushLagMs, &replayLagMs)
if err != nil {
errors = multierr.Append(errors, err)
continue
Expand All @@ -519,6 +542,49 @@ func (c *postgreSQLClient) getReplicationStats(ctx context.Context) ([]replicati
replayLag: replayLag,
writeLag: writeLag,
flushLag: flushLag,
replayLagMs: replayLagMs,
writeLagMs: writeLagMs,
flushLagMs: flushLagMs,
})
}

return rs, errors
}

func (c *postgreSQLClient) getReplicationStats(ctx context.Context) ([]replicationStats, error) {
if lagInSecondsFG.IsEnabled() {
return c.getDeprecatedReplicationStats(ctx)
}

query := `SELECT
client_addr,
coalesce(pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn), -1) AS replication_bytes_pending,
(extract('epoch' from coalesce(write_lag, '-0.001 seconds')) * 1000)::integer AS write_lag_ms,
(extract('epoch' from coalesce(flush_lag, '-0.001 seconds')) * 1000)::integer AS flush_lag_ms,
(extract('epoch' from coalesce(replay_lag, '-0.001 seconds')) * 1000)::integer AS replay_lag_ms
FROM pg_stat_replication;
`
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("unable to query pg_stat_replication: %w", err)
}
defer rows.Close()
var rs []replicationStats
var errors error
for rows.Next() {
var client string
var replicationBytes, writeLagMs, flushLagMs, replayLagMs int64
err = rows.Scan(&client, &replicationBytes, &writeLagMs, &flushLagMs, &replayLagMs)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
rs = append(rs, replicationStats{
clientAddr: client,
pendingBytes: replicationBytes,
replayLagMs: replayLagMs,
writeLagMs: writeLagMs,
flushLagMs: flushLagMs,
})
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions receiver/postgresqlreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,26 @@ func (p *postgreSQLScraper) collectReplicationStats(
if rs.pendingBytes >= 0 {
p.mb.RecordPostgresqlReplicationDataDelayDataPoint(now, rs.pendingBytes, rs.clientAddr)
}
if rs.writeLag >= 0 {
p.mb.RecordPostgresqlWalLagDataPoint(now, rs.writeLag, metadata.AttributeWalOperationLagWrite, rs.clientAddr)
if lagInSecondsFG.IsEnabled() {
if rs.writeLag >= 0 {
p.mb.RecordPostgresqlWalLagDataPoint(now, rs.writeLag, metadata.AttributeWalOperationLagWrite, rs.clientAddr)
}
if rs.replayLag >= 0 {
p.mb.RecordPostgresqlWalLagDataPoint(now, rs.replayLag, metadata.AttributeWalOperationLagReplay, rs.clientAddr)
}
if rs.flushLag >= 0 {
p.mb.RecordPostgresqlWalLagDataPoint(now, rs.flushLag, metadata.AttributeWalOperationLagFlush, rs.clientAddr)
}
}
if rs.replayLag >= 0 {
p.mb.RecordPostgresqlWalLagDataPoint(now, rs.replayLag, metadata.AttributeWalOperationLagReplay, rs.clientAddr)

if rs.writeLagMs >= 0 {
p.mb.RecordPostgresqlWalLagDataPoint(now, rs.writeLagMs, metadata.AttributeWalOperationLagWriteMs, rs.clientAddr)
}
if rs.replayLagMs >= 0 {
p.mb.RecordPostgresqlWalLagDataPoint(now, rs.replayLagMs, metadata.AttributeWalOperationLagReplayMs, rs.clientAddr)
}
if rs.flushLag >= 0 {
p.mb.RecordPostgresqlWalLagDataPoint(now, rs.flushLag, metadata.AttributeWalOperationLagFlush, rs.clientAddr)
if rs.flushLagMs >= 0 {
p.mb.RecordPostgresqlWalLagDataPoint(now, rs.flushLagMs, metadata.AttributeWalOperationLagFlushMs, rs.clientAddr)
}
}
}
Expand Down