Skip to content

Commit

Permalink
adapt open telemetry (traces, metrics) WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
aradwann committed Mar 25, 2024
1 parent e75ed12 commit 00a0e59
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Build stage
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY go.mod ./
COPY go.sum ./
RUN go mod download
COPY . .
RUN go build -o main main.go

Expand Down
9 changes: 4 additions & 5 deletions db/store/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package db
import (
"database/sql"
"fmt"
"log"
"os"
"path/filepath"
"strings"
Expand All @@ -29,8 +28,8 @@ func RunDBMigrations(db *sql.DB, migrationsURL string) {
// log.Fatal().Msg("cannot create new migrate instance")
slog.Error("cannot create new migrate instance %s", err)
}
migration.Up()
if err = migration.Up(); err != nil && err != migrate.ErrNoChange {
err = migration.Up()
if err != nil && err != migrate.ErrNoChange {
slog.Error("failed to run migrate up %s", err)

}
Expand Down Expand Up @@ -81,7 +80,7 @@ func runUnversionedMigrations(db *sql.DB, migrationDir string) error {

// Execute each SQL file
for _, file := range sqlFiles {
log.Printf("Executing SQL file: %s", file)
// log.Printf("Executing SQL file: %s", file)

contents, err := os.ReadFile(file)
if err != nil {
Expand All @@ -94,7 +93,7 @@ func runUnversionedMigrations(db *sql.DB, migrationDir string) error {
return fmt.Errorf("error executing SQL file %s: %w", file, err)
}

log.Printf("Finished executing SQL file: %s", file)
// log.Printf("Finished executing SQL file: %s", file)
}

return nil
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: "3.8" # Ensure you're using version 1.29 or above for health checks in depends_on

services:
postgres:
Expand Down
18 changes: 13 additions & 5 deletions gapi/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func TestHttpLogger(t *testing.T) {
// Mock HTTP handler function
mockHandler := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusOK)
res.Write([]byte("OK"))
_, err := res.Write([]byte("OK"))
require.NoError(t, err)
})

// Create a test HTTP request
Expand All @@ -82,7 +83,9 @@ func TestHttpLoggerErr(t *testing.T) {
// Mock HTTP handler function
mockHandler := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte("Error"))
_, err := res.Write([]byte("Error"))
require.NoError(t, err)

})

// Create a test HTTP request
Expand Down Expand Up @@ -116,7 +119,8 @@ func TestResponseRecorder(t *testing.T) {
}

// Write some data to ResponseRecorder
rec.Write([]byte("Test Body"))
_, err := rec.Write([]byte("Test Body"))
require.NoError(t, err)

// Verify the StatusCode
require.Equal(t, rec.StatusCode, http.StatusOK)
Expand All @@ -140,11 +144,15 @@ func captureLogs(fn func(), logCallback func(string)) {
var wg sync.WaitGroup
wg.Add(1)

go func() {
go func() error {
defer wg.Done()
var buf bytes.Buffer
io.Copy(&buf, r)
_, err := io.Copy(&buf, r)
if err != nil {
return err
}
logCallback(buf.String())
return nil
}()

fn()
Expand Down
15 changes: 14 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ require (
github.com/o1egl/paseto v1.0.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0
go.opentelemetry.io/otel v1.24.0
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.24.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.24.0
go.opentelemetry.io/otel/sdk v1.24.0
go.opentelemetry.io/otel/sdk/metric v1.24.0
go.uber.org/mock v0.4.0
golang.org/x/crypto v0.21.0
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237
Expand All @@ -23,6 +30,9 @@ require (
require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
Expand All @@ -31,12 +41,15 @@ require (
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgtype v1.14.2 // indirect
github.com/jackc/pgtype v1.14.3 // indirect
github.com/jackc/pgx/v4 v4.18.3 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/redis/go-redis/v9 v9.5.1 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/goleak v1.3.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/time v0.5.0 // indirect
Expand Down
Loading

0 comments on commit 00a0e59

Please sign in to comment.