Skip to content

Commit

Permalink
replace deprecated promlog with promslog
Browse files Browse the repository at this point in the history
  • Loading branch information
vinny-sabatini committed Jan 6, 2025
1 parent e870484 commit aeea8ce
Show file tree
Hide file tree
Showing 30 changed files with 260 additions and 2,528 deletions.
27 changes: 13 additions & 14 deletions bigquerydb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"math"
"os"
"sort"
"strings"
"time"

"cloud.google.com/go/bigquery"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/common/promslog"
"github.com/prometheus/prometheus/prompb"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)

// BigqueryClient allows sending batches of Prometheus samples to Bigquery.
type BigqueryClient struct {
logger log.Logger
logger *slog.Logger
client bigquery.Client
datasetID string
tableID string
Expand All @@ -50,13 +50,16 @@ type BigqueryClient struct {
}

// NewClient creates a new Client.
func NewClient(logger log.Logger, googleAPIjsonkeypath, googleProjectID, googleAPIdatasetID, googleAPItableID string, remoteTimeout time.Duration) *BigqueryClient {
func NewClient(logger *slog.Logger, googleAPIjsonkeypath, googleProjectID, googleAPIdatasetID, googleAPItableID string, remoteTimeout time.Duration) *BigqueryClient {
ctx := context.Background()
if logger == nil {
logger = promslog.NewNopLogger()
}
bigQueryClientOptions := []option.ClientOption{}
if googleAPIjsonkeypath != "" {
jsonFile, err := os.Open(googleAPIjsonkeypath)
if err != nil {
level.Error(logger).Log("err", err) //nolint:errcheck
logger.Error("failed to open google api json key", slog.Any("error", err))
os.Exit(1)
}

Expand All @@ -65,7 +68,7 @@ func NewClient(logger log.Logger, googleAPIjsonkeypath, googleProjectID, googleA
var result map[string]interface{}
err = json.Unmarshal([]byte(byteValue), &result)
if err != nil {
level.Error(logger).Log("err", err) //nolint:errcheck
logger.Error("failed to unmarshal google api json key", slog.Any("error", err))
os.Exit(1)
}

Expand All @@ -80,14 +83,10 @@ func NewClient(logger log.Logger, googleAPIjsonkeypath, googleProjectID, googleA
c, err := bigquery.NewClient(ctx, googleProjectID, bigQueryClientOptions...)

if err != nil {
level.Error(logger).Log("err", err) //nolint:errcheck
logger.Error("failed to create new bigquery client", slog.Any("error", err))
os.Exit(1)
}

if logger == nil {
logger = log.NewNopLogger()
}

return &BigqueryClient{
logger: logger,
client: *c,
Expand Down Expand Up @@ -180,7 +179,7 @@ func (c *BigqueryClient) Write(timeseries []*prompb.TimeSeries) error {
for _, s := range samples {
v := float64(s.Value)
if math.IsNaN(v) || math.IsInf(v, 0) {
//level.Debug(c.logger).Log("msg", "cannot send to BigQuery, skipping sample", "value", v, "sample", s)
c.logger.Debug("cannot send to bigquery, skipping sample", slog.Any("value", v), slog.Any("sample", s))
c.ignoredSamples.Inc()
continue
}
Expand Down Expand Up @@ -259,7 +258,7 @@ func (c *BigqueryClient) Read(req *prompb.ReadRequest) (*prompb.ReadResponse, er
}
duration := time.Since(begin).Seconds()
c.sqlQueryDuration.Observe(duration)
level.Debug(c.logger).Log("msg", "BigQuery SQL query", "rows", iter.TotalRows, "duration", duration) //nolint:errcheck
c.logger.Debug("bigquery sql query", slog.Any("rows", iter.TotalRows), slog.Any("duration", duration))
}

resp := prompb.ReadResponse{
Expand Down Expand Up @@ -312,7 +311,7 @@ func (c *BigqueryClient) buildCommand(q *prompb.Query) (string, error) {
matchers = append(matchers, fmt.Sprintf("timestamp <= TIMESTAMP_MILLIS(%v)", q.EndTimestampMs))

query := fmt.Sprintf("SELECT metricname, tags, UNIX_MILLIS(timestamp) as timestamp, value FROM %s.%s WHERE %v ORDER BY timestamp", c.datasetID, c.tableID, strings.Join(matchers, " AND "))
level.Debug(c.logger).Log("msg", "BigQuery read", "sql query", query) //nolint:errcheck
c.logger.Debug("bigquery read", slog.Any("sql query", query))

return query, nil
}
Expand Down
4 changes: 2 additions & 2 deletions bigquerydb/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ limitations under the License.
package bigquerydb

import (
"log/slog"
"math"
"os"
"testing"
"time"

"github.com/go-kit/log"
"github.com/prometheus/prometheus/prompb"
"github.com/stretchr/testify/assert"
)

var bigQueryClientTimeout = time.Second * 60
var logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))
var logger = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))

var googleAPIdatasetID = os.Getenv("BQ_DATASET_NAME")
var googleAPItableID = os.Getenv("BQ_TABLE_NAME")
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.23

require (
cloud.google.com/go/bigquery v1.65.0
github.com/go-kit/log v0.2.1
github.com/gogo/protobuf v1.3.2
github.com/golang/snappy v0.0.4
github.com/pkg/errors v0.9.1
Expand All @@ -29,7 +28,6 @@ require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/goccy/go-json v0.10.2 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
Expand Down
Loading

0 comments on commit aeea8ce

Please sign in to comment.