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

database_observability: format DSN with parameters #2216

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (c *SchemaTable) extractSchema(ctx context.Context) error {
cacheKey := fmt.Sprintf("%s@%d", fullyQualifiedTable, table.updateTime.Unix())

if c.cache.Contains(cacheKey) {
level.Info(c.logger).Log("msg", "table definition already in cache", "schema", table.schema, "table", table.tableName)
level.Debug(c.logger).Log("msg", "table definition already in cache", "schema", table.schema, "table", table.tableName)
continue
}

Expand Down
19 changes: 17 additions & 2 deletions internal/component/database_observability/mysql/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"path"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -183,8 +184,7 @@ func (c *Component) Update(args component.Arguments) error {

c.args = args.(Arguments)

// TODO(cristian): verify before appending parameter
dbConnection, err := sql.Open("mysql", string(c.args.DataSourceName)+"?parseTime=true")
dbConnection, err := sql.Open("mysql", formatDSN(string(c.args.DataSourceName), "parseTime=true"))
if err != nil {
return err
}
Expand Down Expand Up @@ -268,3 +268,18 @@ func (c *Component) instanceKey() string {

return fmt.Sprintf("%s(%s)/%s", m.Net, m.Addr, m.DBName)
}

// formatDSN appends the given parameters to the DSN.
// parameters are expected to be in the form of "key=value".
func formatDSN(dsn string, params ...string) string {
if len(params) == 0 {
return dsn
}

if strings.Contains(dsn, "?") {
dsn = dsn + "&"
} else {
dsn = dsn + "?"
}
return dsn + strings.Join(params, "&")
}
Loading