Skip to content

Commit

Permalink
feat(otel): Add tracing to databases (#2465)
Browse files Browse the repository at this point in the history
  • Loading branch information
silvestre authored Jan 18, 2024
1 parent 8980bab commit d574016
Show file tree
Hide file tree
Showing 38 changed files with 194 additions and 61 deletions.
15 changes: 15 additions & 0 deletions packages/eventgenerator/spec
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ files:
- autoscaler/vendor/github.com/cenkalti/backoff/v4/* # gosub
- autoscaler/vendor/github.com/cespare/xxhash/v2/* # gosub
- autoscaler/vendor/github.com/facebookgo/clock/* # gosub
- autoscaler/vendor/github.com/go-logr/logr/* # gosub
- autoscaler/vendor/github.com/go-logr/logr/funcr/* # gosub
- autoscaler/vendor/github.com/go-logr/stdr/* # gosub
- autoscaler/vendor/github.com/go-sql-driver/mysql/* # gosub
- autoscaler/vendor/github.com/golang/protobuf/jsonpb/* # gosub
- autoscaler/vendor/github.com/golang/protobuf/proto/* # gosub
Expand Down Expand Up @@ -93,10 +96,22 @@ files:
- autoscaler/vendor/github.com/tedsuo/ifrit/grouper/* # gosub
- autoscaler/vendor/github.com/tedsuo/ifrit/http_server/* # gosub
- autoscaler/vendor/github.com/tedsuo/ifrit/sigmon/* # gosub
- autoscaler/vendor/github.com/uptrace/opentelemetry-go-extra/otelsql/* # gosub
- autoscaler/vendor/github.com/uptrace/opentelemetry-go-extra/otelsqlx/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/attribute/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/baggage/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/codes/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/attribute/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/baggage/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/global/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/metric/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/metric/embedded/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/propagation/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/internal/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.10.0/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.21.0/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/trace/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/trace/embedded/* # gosub
- autoscaler/vendor/golang.org/x/crypto/bcrypt/* # gosub
Expand Down
4 changes: 4 additions & 0 deletions packages/golangapiserver/spec
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ files:
- autoscaler/vendor/github.com/tedsuo/ifrit/grouper/* # gosub
- autoscaler/vendor/github.com/tedsuo/ifrit/http_server/* # gosub
- autoscaler/vendor/github.com/tedsuo/ifrit/sigmon/* # gosub
- autoscaler/vendor/github.com/uptrace/opentelemetry-go-extra/otelsql/* # gosub
- autoscaler/vendor/github.com/uptrace/opentelemetry-go-extra/otelsqlx/* # gosub
- autoscaler/vendor/github.com/xeipuuv/gojsonpointer/* # gosub
- autoscaler/vendor/github.com/xeipuuv/gojsonreference/* # gosub
- autoscaler/vendor/github.com/xeipuuv/gojsonschema/* # gosub
Expand All @@ -152,6 +154,8 @@ files:
- autoscaler/vendor/go.opentelemetry.io/otel/sdk/internal/env/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/sdk/resource/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/sdk/trace/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/internal/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.10.0/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.17.0/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.19.0/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.21.0/* # gosub
Expand Down
15 changes: 15 additions & 0 deletions packages/metricsforwarder/spec
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ files:
- autoscaler/vendor/code.cloudfoundry.org/tlsconfig/* # gosub
- autoscaler/vendor/github.com/beorn7/perks/quantile/* # gosub
- autoscaler/vendor/github.com/cespare/xxhash/v2/* # gosub
- autoscaler/vendor/github.com/go-logr/logr/* # gosub
- autoscaler/vendor/github.com/go-logr/logr/funcr/* # gosub
- autoscaler/vendor/github.com/go-logr/stdr/* # gosub
- autoscaler/vendor/github.com/go-sql-driver/mysql/* # gosub
- autoscaler/vendor/github.com/golang/protobuf/jsonpb/* # gosub
- autoscaler/vendor/github.com/golang/protobuf/proto/* # gosub
Expand Down Expand Up @@ -84,10 +87,22 @@ files:
- autoscaler/vendor/github.com/tedsuo/ifrit/grouper/* # gosub
- autoscaler/vendor/github.com/tedsuo/ifrit/http_server/* # gosub
- autoscaler/vendor/github.com/tedsuo/ifrit/sigmon/* # gosub
- autoscaler/vendor/github.com/uptrace/opentelemetry-go-extra/otelsql/* # gosub
- autoscaler/vendor/github.com/uptrace/opentelemetry-go-extra/otelsqlx/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/attribute/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/baggage/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/codes/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/attribute/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/baggage/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/global/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/metric/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/metric/embedded/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/propagation/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/internal/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.10.0/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.21.0/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/trace/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/trace/embedded/* # gosub
- autoscaler/vendor/golang.org/x/crypto/bcrypt/* # gosub
Expand Down
15 changes: 15 additions & 0 deletions packages/metricsgateway/spec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ files:
- autoscaler/vendor/code.cloudfoundry.org/tlsconfig/* # gosub
- autoscaler/vendor/github.com/beorn7/perks/quantile/* # gosub
- autoscaler/vendor/github.com/cespare/xxhash/v2/* # gosub
- autoscaler/vendor/github.com/go-logr/logr/* # gosub
- autoscaler/vendor/github.com/go-logr/logr/funcr/* # gosub
- autoscaler/vendor/github.com/go-logr/stdr/* # gosub
- autoscaler/vendor/github.com/go-sql-driver/mysql/* # gosub
- autoscaler/vendor/github.com/golang/protobuf/jsonpb/* # gosub
- autoscaler/vendor/github.com/golang/protobuf/proto/* # gosub
Expand Down Expand Up @@ -78,10 +81,22 @@ files:
- autoscaler/vendor/github.com/tedsuo/ifrit/grouper/* # gosub
- autoscaler/vendor/github.com/tedsuo/ifrit/http_server/* # gosub
- autoscaler/vendor/github.com/tedsuo/ifrit/sigmon/* # gosub
- autoscaler/vendor/github.com/uptrace/opentelemetry-go-extra/otelsql/* # gosub
- autoscaler/vendor/github.com/uptrace/opentelemetry-go-extra/otelsqlx/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/attribute/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/baggage/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/codes/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/attribute/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/baggage/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/global/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/metric/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/metric/embedded/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/propagation/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/internal/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.10.0/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.21.0/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/trace/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/trace/embedded/* # gosub
- autoscaler/vendor/golang.org/x/crypto/bcrypt/* # gosub
Expand Down
16 changes: 16 additions & 0 deletions packages/metricsserver/spec
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ files:
- autoscaler/vendor/dario.cat/mergo/* # gosub
- autoscaler/vendor/github.com/beorn7/perks/quantile/* # gosub
- autoscaler/vendor/github.com/cespare/xxhash/v2/* # gosub
- autoscaler/vendor/github.com/go-logr/logr/* # gosub
- autoscaler/vendor/github.com/go-logr/logr/funcr/* # gosub
- autoscaler/vendor/github.com/go-logr/stdr/* # gosub
- autoscaler/vendor/github.com/go-sql-driver/mysql/* # gosub
- autoscaler/vendor/github.com/golang/protobuf/jsonpb/* # gosub
- autoscaler/vendor/github.com/golang/protobuf/proto/* # gosub
Expand Down Expand Up @@ -80,16 +83,29 @@ files:
- autoscaler/vendor/github.com/tedsuo/ifrit/grouper/* # gosub
- autoscaler/vendor/github.com/tedsuo/ifrit/http_server/* # gosub
- autoscaler/vendor/github.com/tedsuo/ifrit/sigmon/* # gosub
- autoscaler/vendor/github.com/uptrace/opentelemetry-go-extra/otelsql/* # gosub
- autoscaler/vendor/github.com/uptrace/opentelemetry-go-extra/otelsqlx/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/attribute/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/baggage/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/codes/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/attribute/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/baggage/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/global/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/metric/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/metric/embedded/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/propagation/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/internal/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.10.0/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.21.0/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/trace/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/trace/embedded/* # gosub
- autoscaler/vendor/golang.org/x/crypto/bcrypt/* # gosub
- autoscaler/vendor/golang.org/x/crypto/blowfish/* # gosub
- autoscaler/vendor/golang.org/x/crypto/pbkdf2/* # gosub
- autoscaler/vendor/golang.org/x/exp/maps/* # gosub
- autoscaler/vendor/golang.org/x/net/context/* # gosub
- autoscaler/vendor/golang.org/x/net/http/httpguts/* # gosub
- autoscaler/vendor/golang.org/x/net/http2/* # gosub
- autoscaler/vendor/golang.org/x/net/http2/hpack/* # gosub
Expand Down
15 changes: 15 additions & 0 deletions packages/operator/spec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ files:
- autoscaler/vendor/code.cloudfoundry.org/tlsconfig/* # gosub
- autoscaler/vendor/github.com/beorn7/perks/quantile/* # gosub
- autoscaler/vendor/github.com/cespare/xxhash/v2/* # gosub
- autoscaler/vendor/github.com/go-logr/logr/* # gosub
- autoscaler/vendor/github.com/go-logr/logr/funcr/* # gosub
- autoscaler/vendor/github.com/go-logr/stdr/* # gosub
- autoscaler/vendor/github.com/go-sql-driver/mysql/* # gosub
- autoscaler/vendor/github.com/gorilla/mux/* # gosub
- autoscaler/vendor/github.com/hashicorp/go-cleanhttp/* # gosub
Expand Down Expand Up @@ -69,10 +72,22 @@ files:
- autoscaler/vendor/github.com/tedsuo/ifrit/grouper/* # gosub
- autoscaler/vendor/github.com/tedsuo/ifrit/http_server/* # gosub
- autoscaler/vendor/github.com/tedsuo/ifrit/sigmon/* # gosub
- autoscaler/vendor/github.com/uptrace/opentelemetry-go-extra/otelsql/* # gosub
- autoscaler/vendor/github.com/uptrace/opentelemetry-go-extra/otelsqlx/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/attribute/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/baggage/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/codes/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/attribute/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/baggage/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/internal/global/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/metric/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/metric/embedded/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/propagation/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/internal/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.10.0/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.21.0/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/trace/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/trace/embedded/* # gosub
- autoscaler/vendor/golang.org/x/crypto/bcrypt/* # gosub
Expand Down
4 changes: 4 additions & 0 deletions packages/scalingengine/spec
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ files:
- autoscaler/vendor/github.com/tedsuo/ifrit/grouper/* # gosub
- autoscaler/vendor/github.com/tedsuo/ifrit/http_server/* # gosub
- autoscaler/vendor/github.com/tedsuo/ifrit/sigmon/* # gosub
- autoscaler/vendor/github.com/uptrace/opentelemetry-go-extra/otelsql/* # gosub
- autoscaler/vendor/github.com/uptrace/opentelemetry-go-extra/otelsqlx/* # gosub
- autoscaler/vendor/go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux/* # gosub
- autoscaler/vendor/go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux/internal/semconvutil/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/* # gosub
Expand All @@ -130,6 +132,8 @@ files:
- autoscaler/vendor/go.opentelemetry.io/otel/sdk/internal/env/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/sdk/resource/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/sdk/trace/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/internal/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.10.0/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.17.0/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.19.0/* # gosub
- autoscaler/vendor/go.opentelemetry.io/otel/semconv/v1.21.0/* # gosub
Expand Down
8 changes: 4 additions & 4 deletions src/autoscaler/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ type InstanceMetricsDB interface {
RetrieveInstanceMetrics(appid string, instanceIndex int, name string, start int64, end int64, orderType OrderType) ([]*models.AppInstanceMetric, error)
SaveMetric(metric *models.AppInstanceMetric) error
SaveMetricsInBulk(metrics []*models.AppInstanceMetric) error
PruneInstanceMetrics(before int64) error
PruneInstanceMetrics(ctx context.Context, before int64) error
io.Closer
}

type PolicyDB interface {
healthendpoint.DatabaseStatus
healthendpoint.Pinger
GetAppIds() (map[string]bool, error)
GetAppIds(ctx context.Context) (map[string]bool, error)
GetAppPolicy(ctx context.Context, appId string) (*models.ScalingPolicy, error)
SaveAppPolicy(ctx context.Context, appId string, policy *models.ScalingPolicy, policyGuid string) error
SetOrUpdateDefaultAppPolicy(ctx context.Context, appIds []string, oldPolicyGuid string, newPolicy *models.ScalingPolicy, newPolicyGuid string) ([]string, error)
Expand Down Expand Up @@ -92,7 +92,7 @@ type AppMetricDB interface {
SaveAppMetric(appMetric *models.AppMetric) error
SaveAppMetricsInBulk(metrics []*models.AppMetric) error
RetrieveAppMetrics(appId string, metricType string, start int64, end int64, orderType OrderType) ([]*models.AppMetric, error)
PruneAppMetrics(before int64) error
PruneAppMetrics(ctx context.Context, before int64) error
io.Closer
}

Expand All @@ -102,7 +102,7 @@ type ScalingEngineDB interface {

CountScalingHistories(ctx context.Context, appId string, start int64, end int64, includeAll bool) (int, error)
RetrieveScalingHistories(ctx context.Context, appId string, start int64, end int64, orderType OrderType, includeAll bool, page int, resultsPerPAge int) ([]*models.AppScalingHistory, error)
PruneScalingHistories(before int64) error
PruneScalingHistories(ctx context.Context, before int64) error
UpdateScalingCooldownExpireTime(appId string, expireAt int64) error
CanScaleApp(appId string) (bool, int64, error)
GetActiveSchedule(appId string) (*models.ActiveSchedule, error)
Expand Down
9 changes: 7 additions & 2 deletions src/autoscaler/db/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import (
"strings"

"github.com/go-sql-driver/mysql"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
)

type Database struct {
DriverName string
DSN string
DriverName string
DSN string
OTELAttribute attribute.KeyValue
}

type MySQLConfig struct {
Expand Down Expand Up @@ -54,8 +57,10 @@ func GetConnection(dbUrl string) (*Database, error) {
return nil, err
}
database.DSN = cfg.config.FormatDSN()
database.OTELAttribute = semconv.DBSystemMySQL
case PostgresDriverName:
database.DSN = dbUrl
database.OTELAttribute = semconv.DBSystemPostgreSQL
}
return database, nil
}
Expand Down
21 changes: 13 additions & 8 deletions src/autoscaler/db/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db_test

import (
. "code.cloudfoundry.org/app-autoscaler/src/autoscaler/db"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -27,8 +28,9 @@ var _ = Describe("Helper", func() {
It("returns mysql database object", func() {
Expect(err).NotTo(HaveOccurred())
Expect(database).To(Equal(&Database{
DriverName: "mysql",
DSN: "root@tcp(localhost:3306)/autoscaler?parseTime=true&tls=preferred",
DriverName: "mysql",
DSN: "root@tcp(localhost:3306)/autoscaler?parseTime=true&tls=preferred",
OTELAttribute: semconv.DBSystemMySQL,
}))
})
})
Expand All @@ -40,8 +42,9 @@ var _ = Describe("Helper", func() {
It("returns mysql database object", func() {
Expect(err).NotTo(HaveOccurred())
Expect(database).To(Equal(&Database{
DriverName: "mysql",
DSN: "root@tcp(localhost:3306)/autoscaler?parseTime=true",
DriverName: "mysql",
DSN: "root@tcp(localhost:3306)/autoscaler?parseTime=true",
OTELAttribute: semconv.DBSystemMySQL,
}))
})

Expand All @@ -55,8 +58,9 @@ var _ = Describe("Helper", func() {
It("returns mysql database connection", func() {
Expect(err).NotTo(HaveOccurred())
Expect(database).To(Equal(&Database{
DriverName: "mysql",
DSN: "root@tcp(localhost:3306)/autoscaler?parseTime=true&tls=verify-ca",
DriverName: "mysql",
DSN: "root@tcp(localhost:3306)/autoscaler?parseTime=true&tls=verify-ca",
OTELAttribute: semconv.DBSystemMySQL,
}))
})
})
Expand All @@ -78,8 +82,9 @@ var _ = Describe("Helper", func() {
It("returns postgres database object", func() {
Expect(err).NotTo(HaveOccurred())
Expect(database).To(Equal(&Database{
DriverName: "pgx",
DSN: "postgres://postgres:password@localhost:5432/autoscaler?sslmode=disable",
DriverName: "pgx",
DSN: "postgres://postgres:password@localhost:5432/autoscaler?sslmode=disable",
OTELAttribute: semconv.DBSystemPostgreSQL,
}))
})
})
Expand Down
8 changes: 5 additions & 3 deletions src/autoscaler/db/sqldb/appmetric_sqldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"code.cloudfoundry.org/app-autoscaler/src/autoscaler/db"
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/models"
"github.com/uptrace/opentelemetry-go-extra/otelsql"
"github.com/uptrace/opentelemetry-go-extra/otelsqlx"

"database/sql"
"time"
Expand All @@ -26,7 +28,7 @@ func NewAppMetricSQLDB(dbConfig db.DatabaseConfig, logger lager.Logger) (*AppMet
return nil, err
}

sqldb, err := sqlx.Open(database.DriverName, database.DSN)
sqldb, err := otelsqlx.Open(database.DriverName, database.DSN, otelsql.WithAttributes(database.OTELAttribute))
if err != nil {
logger.Error("open-AppMetric-db", err, lager.Data{"dbConfig": dbConfig})
return nil, err
Expand Down Expand Up @@ -138,9 +140,9 @@ func (adb *AppMetricSQLDB) RetrieveAppMetrics(appIdP string, metricTypeP string,
return appMetricList, rows.Err()
}

func (adb *AppMetricSQLDB) PruneAppMetrics(before int64) error {
func (adb *AppMetricSQLDB) PruneAppMetrics(ctx context.Context, before int64) error {
query := adb.sqldb.Rebind("DELETE FROM app_metric WHERE timestamp <= ?")
_, err := adb.sqldb.Exec(query, before)
_, err := adb.sqldb.ExecContext(ctx, query, before)
if err != nil {
adb.logger.Error("prune-metrics-from-app_metric-table", err, lager.Data{"query": query, "before": before})
}
Expand Down
3 changes: 2 additions & 1 deletion src/autoscaler/db/sqldb/appmetric_sqldb_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sqldb_test

import (
"context"
"os"
"strings"
"sync"
Expand Down Expand Up @@ -404,7 +405,7 @@ var _ = Describe("AppMetricSQLDB", func() {
})

JustBeforeEach(func() {
err = adb.PruneAppMetrics(before)
err = adb.PruneAppMetrics(context.TODO(), before)
})

Context("when pruning app metrics before all the timestamps of metrics", func() {
Expand Down
Loading

0 comments on commit d574016

Please sign in to comment.