Skip to content

Commit

Permalink
feat: add opentelemetry package (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdh authored Feb 6, 2024
1 parent ae1caa8 commit b9123e1
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 54 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ jobs:
steps:
- uses: actions/setup-go@v2
with:
go-version: '1.19'
go-version: '1.21'
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: run unit tests
run: make test
- name: golangci-lint
uses: golangci/golangci-lint-action@v3.2.0
uses: golangci/golangci-lint-action@v3.7.0
with:
skip-go-installation: true
version: v1.46.2
version: v1.55.2
16 changes: 15 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"net/url"
"time"

"github.com/jmoiron/sqlx"
Expand All @@ -14,10 +15,18 @@ type Client struct {
*sqlx.DB
queryTimeOut time.Duration
cfg Config
host string
}

// NewClient creates a new sqlx database client
func New(cfg Config) (*Client, error) {
dbURL, err := url.Parse(cfg.URL)
if err != nil {
return nil, err
}

host := dbURL.Host

db, err := sqlx.Connect(cfg.Driver, cfg.URL)
if err != nil {
return nil, err
Expand All @@ -27,7 +36,7 @@ func New(cfg Config) (*Client, error) {
db.SetMaxOpenConns(cfg.MaxOpenConns)
db.SetConnMaxLifetime(cfg.ConnMaxLifeTime)

return &Client{DB: db, queryTimeOut: cfg.MaxQueryTimeout, cfg: cfg}, err
return &Client{DB: db, queryTimeOut: cfg.MaxQueryTimeout, cfg: cfg, host: host}, err
}

func (c Client) WithTimeout(ctx context.Context, op func(ctx context.Context) error) (err error) {
Expand Down Expand Up @@ -73,6 +82,11 @@ func (c *Client) ConnectionURL() string {
return c.cfg.URL
}

// Host fetch the database host information
func (c *Client) Host() string {
return c.host
}

// Close closes the database connection
func (c *Client) Close() error {
return c.DB.Close()
Expand Down
27 changes: 17 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ require (
github.com/charmbracelet/glamour v0.3.0
github.com/cli/safeexec v1.0.0
github.com/golang-migrate/migrate/v4 v4.16.0
github.com/google/go-cmp v0.5.9
github.com/google/go-cmp v0.6.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.3.0
github.com/google/uuid v1.3.1
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0
github.com/hashicorp/go-version v1.3.0
github.com/jeremywohl/flatten v1.0.1
github.com/jmoiron/sqlx v1.3.5
Expand All @@ -39,14 +39,21 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.8.1
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/contrib/instrumentation/host v0.47.0
go.opentelemetry.io/contrib/instrumentation/runtime v0.47.0
go.opentelemetry.io/contrib/samplers/probability/consistent v0.16.0
go.opentelemetry.io/otel v1.22.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.45.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.22.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.22.0
go.opentelemetry.io/otel/sdk v1.22.0
go.opentelemetry.io/otel/sdk/metric v1.22.0
go.uber.org/zap v1.19.0
golang.org/x/net v0.15.0 // indirect
golang.org/x/oauth2 v0.10.0
golang.org/x/oauth2 v0.13.0
golang.org/x/text v0.13.0
google.golang.org/api v0.126.0
google.golang.org/genproto/googleapis/api v0.0.0-20230726155614-23370e0ffb3e
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.58.0
google.golang.org/protobuf v1.31.0
google.golang.org/api v0.128.0
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97
google.golang.org/grpc v1.60.1
google.golang.org/protobuf v1.32.0
gopkg.in/yaml.v3 v3.0.1
)
Loading

0 comments on commit b9123e1

Please sign in to comment.