Skip to content

[receiver/postgresql] add basic query sample collection for postgresqlreceiver #39573

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

cuichenli
Copy link
Contributor

Description

we introduced the top query collection to postgresql receiver. this will collect the queries from pg_stat_statements and report the related metrics. this will help the end user to identify which queries are most executed, and resources most used.

Example output:

resourceLogs:
  - resource:
      attributes: []
    scopeLogs:
      - logRecords:
          - attributes:
              - key: db.system.name
                value:
                  stringValue: postgresql
              - key: db.namespace
                value:
                  stringValue: postgres
              - key: db.query.text
                value:
                  stringValue: "select * from pg_stat_activity where id = ?"
              - key: postgresql.calls 
                value: 
                  intValue: 3
              - key: postgresql.rows 
                value:
                  intValue: 10
              - key: postgresql.shared_blks_dirtied
                value:
                  intValue: 1 
              - key: postgresql.shared_blks_hit
                value:
                  intValue: 2
              - key: postgresql.shared_blks_read
                value:
                  intValue: 3
              - key: postgresql.shared_blks_written
                value:
                  intValue: 4
              - key: postgresql.temp_blks_read
                value:
                  intValue: 5
              - key: postgresql.temp_blks_written
                value:
                  intValue: 6
              - key: postgresql.queryid 
                value: 
                  stringValue: "114514"
              - key: postgresql.rolname 
                value: 
                  stringValue: master
              - key: postgresql.total_exec_time 
                value: 
                  doubleValue: 1
              - key: postgresql.total_plan_time 
                value: 
                  doubleValue: 1
            body:
              stringValue: top query
        scope:
          name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver
          version: 0.0.1

@cuichenli cuichenli requested a review from a team as a code owner April 23, 2025 02:22
@cuichenli cuichenli requested a review from mwear April 23, 2025 02:22

scopedLog := resourceLog.ScopeLogs().AppendEmpty()
scopedLog.Scope().SetName(metadata.ScopeName)
scopedLog.Scope().SetVersion("0.0.1")
Copy link
Contributor

Choose a reason for hiding this comment

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

is that version correct?

Comment on lines +7 to +18
DbAttributePrefix = "postgresql."
QueryidColumnName = "queryid"
TotalExecTimeColumnName = "total_exec_time"
TotalPlanTimeColumnName = "total_plan_time"
CallsColumnName = "calls"
RowsColumnName = "rows"
SharedBlksDirtiedColumnName = "shared_blks_dirtied"
SharedBlksHitColumnName = "shared_blks_hit"
SharedBlksReadColumnName = "shared_blks_read"
SharedBlksWrittenColumnName = "shared_blks_written"
TempBlksReadColumnName = "temp_blks_read"
TempBlksWrittenColumnName = "temp_blks_written"
Copy link
Contributor

Choose a reason for hiding this comment

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

do they need to be exported?


p.collectTopQuery(ctx, dbClient, &logRecords, maxRowsPerQuery, topNQuery, &errs, p.logger)

defer dbClient.Close()
Copy link
Contributor

Choose a reason for hiding this comment

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

should the defer call be before we collect?

mux.addPartial(err)
logger.Error("failed to read attributes from row", zap.Error(err))
}
record.Attributes().PutStr("db.system.name", "postgresql")
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't this be a resource attribute?

Copy link
Contributor

@atoulme atoulme left a comment

Choose a reason for hiding this comment

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

LGTM. Some small things to address.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants