Skip to content

Cannot read database label from the connection string (driver=sqlserver) #162

Closed

Description

@sadedil

I'm using sql_exporter with MSSQL DB (Azure SQL and Azure MI both) and my connection string format is like this

sqlserver://user1:[email protected]:1433?database=my_db_name&encrypt=true&TrustServerCertificate=true&connection+timeout=15

When I check the metrics (http://localhost:9237/metrics) I can see the correct metric I configured and the correct labels coming from the exporter itself except database label.

See below: I did not set driver, host, user labels, it reads from the connection string but database

sql_myproduct_active_connection_count{
  col="active_connection_count",
  database="",
  driver="sqlserver",
  host="something.database.windows.net:1433",
  sql_job="myproduct_sql_metric_export_job",
  user="user1"
} 1 1757520577243

The database label is always empty string ""


Expected: see database="my_db_name" instead of database=""

Activity

  1. dewey commented on Sep 10, 2025

    @dewey
    Member

    Happy to review a PR if you want to give it a try :)

  2. sadedil commented on Sep 10, 2025

    @sadedil
    Author

    I've tried, please check the merge request.

  3. sadedil commented on Sep 17, 2025

    @sadedil
    Author

    Hello @dewey, any plans to review/merge?

    Thank you

  4. dewey commented on Sep 17, 2025

    @dewey
    Member

    Thanks for the reminder, will check tomorrow.

  5. dewey commented on Sep 22, 2025

    @dewey
    Member

    Hey @sadedil, are you sure the current way we support doesn't work? I'm seeing a similar url being used in this project and I would've also expected there to be previous cases being flagged as something basic as the dbname parameter wouldn't work.

  6. sadedil commented on Sep 22, 2025

    @sadedil
    Author

    Hi @dewey,

    Actually, it felt a bit strange to me at first, but when I looked into the sql_exporter code, I saw that it expects the connection string for all databases in URL format (I’m more of a .NET person, so it wasn’t something I was very used to, but I got it).

    Since a URL has its own specification, it’s naturally made up of certain parts.

    • URL1 (standard web url): https://example.com:8080/products/electronics?category=phones&sort=price#reviews
    • URL2 (go mssql connection string url): sqlserver://user1:[email protected]:1433?database=db1&encrypt=true&TrustServerCertificate=true&connection+timeout=15&app name=sql_exporter_local

    Let's analyze the parts:

    Scheme Host Port Path Query Fragment
    https example.com 8080 /products/electronics ?category=phones&sort=price #reviews
    sqlserver host1.database.windows.net 1433 (empty) <-- the problem ?database=db1&encrypt=true&TrustServerCertificate=true&connection+timeout=15&app name=sql_exporter_local (empty)

    Strangely enough, the dburl library you pointed out gives an example like this:
    sqlserver://user:[email protected]/dbname

    However, in the microsoft/go-mssqldb library there’s no usage like /dbname; instead, it only supports something like /instance?database=dbname.

    Here’s an example taken directly from there:
    sqlserver://sa@localhost/SQLExpress?database=master&connection+timeout=30 // SQLExpress instance.


    Another point is that I also have a working example related to this.

  7. sadedil commented on Sep 22, 2025

    @sadedil
    Author

    I just checked again,

    I saw this note in the dburl:

    for Microsoft SQL Server, /dbname can be /instance/dbname, where /instance is optional. For Oracle Database, /dbname is of the form /service/dbname where /service is the service name or SID, and /dbname is optional. Please see below for examples.

    When I prepared the connection string like this, my problem was solved.

    Sorry for the trouble.

    sqlserver://user1:[email protected]:1433/db1?encrypt=true&TrustServerCertificate=true&connection+timeout=15&app name=sql_exporter_local

  8. sadedil commented on Sep 22, 2025

    @sadedil
    Author

    Sorry for spamming, but now I tried this on our project and this url query style @host1.database.windows.net:1433/db1?... has limited support for Azure SQL and Azure Managed Instance databases

    I've started to get these errors

    • mssql: login error: Database 'master' on server '' is not currently available. Please retry the connection later.
    • database must be specified when ApplicationIntent is ReadOnly

    The decision is yours.

  9. dewey commented on Sep 22, 2025

    @dewey
    Member

    Hey @sadedil, I've merged the changes. I don't have a msql server myself to test but as you verified that's working for you let's get it in.

    Thanks for the contribution!

  10. sadedil commented on Sep 22, 2025

    @sadedil
    Author

    Thank you, @dewey, for open-sourcing this project.
    When are you planning to release the new Docker image?

  11. dewey commented on Sep 22, 2025

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions