Skip to content

Record server.address #399

Closed
Closed

Description

@punya

Problem Statement

When observing an application that talks to a database, it's useful to know which particular database server was used. This would allow

  • an APM UI to show the database on a topology visualization
  • an operator to look at utilization metrics for the relevant host

Proposed Solution

The SQL DB semantic conventions recommend storing this information in the attribute server.address.

Alternatives

  • network.peer.address is another semconv attribute to consider for this purpose. Note that the DB semconv suggests in a footnote that

    When observed from the client side, and when communicating through an intermediary, server.address SHOULD represent the server address behind any intermediaries, for example proxies, if it’s available.

    If we're concerned that we can't meet this high standard, network.peer.address may be a way to convey our limited knowledge.

Prior Art

  • In Python, the SQLAlchemy instrumentation generates network.peer.address.
  • In Java, I believe (but have not confirmed) that the JDBC instrumentation generates server.address.

Activity

  1. added
    enhancement
    on Dec 15, 2024
  2. XSAM commented on Dec 19, 2024

    @XSAM
    Owner

    Hi @punya, thanks for raising this PR.

    I would consider adding server.address, as it reflects the server address behind any intermediaries.

    Adding network.peer.address may create noise in the topology visualization since the path from the client to the server may not be the same as the path from the receiver (otel collector receiver) to the server. Besides, the server itself may not be aware of the intermediaries, so the telemetry data populated from the server, if any, could only include the server.address.

    See, https://opentelemetry.io/docs/specs/semconv/general/attributes/#clientserver-example-with-reverse-proxy


    However, the otelsql can only make the best effort when populating server.address by parsing the DSN provided by users, which may be the proxy address instead of the server address.

    To mitigate this issue, I would like to make WithAttributes overwrite attributes whatever the library automatically resolves. So, users can use WithAttributes to populate server.address as a backup to make it correct when correlating with data from other sources.

    WDYT?

  3. punya commented on Dec 19, 2024

    @punya
    Author

    That sounds perfect @XSAM -- so otelsql would populate a default value by parsing the DSN, but allow the user to override it if necessary. Thanks!

    Let me know if you're planning to implement this yourself or if you're looking for a contribution.

  4. XSAM commented on Dec 20, 2024

    @XSAM
    Owner

    That is great! I am planning to implement this myself in recent days.

  5. self-assigned this
    on Jan 29, 2025
  6. XSAM commented on Jan 30, 2025

    @XSAM
    Owner

    Hi @punya, I checked the automated approach, but with the current design, only the otelsql.Register, otelsql.WrapDriver, and otelsql.Open get the chance to automatically populate the server.address attributes.

    otelsql.OpenDB and otelsql.RegisterDBStatsMetrics will not be able to populate the attribute automatically.

    So, for the consistency of adding the server.address attribute to traces and metrics, instead of implementing an automatic attribute injecting for part of the functionality, I am thinking about adding a helper function, like AttributesFromDSN.

    For instance, users can use this helper function to generate attributes from their DSN:

    // Connect to database
    db, err := otelsql.Open("mysql", mysqlDSN, otelsql.WithAttributes(
    	semconv.DBSystemMySQL,
    	otelsql.AttributesFromDSN(mysqlDSN)...,
    ))
    if err != nil {
    	log.Fatal(err)
    }
    
    // Register DB stats to meter
    err = otelsql.RegisterDBStatsMetrics(db, otelsql.WithAttributes(
    	semconv.DBSystemMySQL,
    	otelsql.AttributesFromDSN(mysqlDSN)...,
    ))
    if err != nil {
    	log.Fatal(err)
    }

    The automation approach (which only works for some functions) can be implemented after this helper function.

  7. punya commented on Jan 30, 2025

    @punya
    Author

    @XSAM your plan makes sense. Even before the automation approach, having the helper functions available and documented will be very useful.

  8. added a commit that references this issue on Feb 17, 2025
  9. XSAM commented on May 20, 2025

    @XSAM
    Owner

    I have been thinking about the automation approach for a while, but I feel the otelsql.AttributesFromDSN(mysqlDSN) itself can be the standard way to initialize otelsql. This also prevents inconsistency (some initialization methods can automatically resolve server.address while others cannot).

    So, I think the automation approach is not needed.

    Closing this issue as completed. @punya, feel free to re-open it if you have any concerns.

  10. punya commented on May 20, 2025

    @punya
    Author

    @XSAM thanks, your approach makes sense.

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

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions