Skip to content

Commit 00a0e59

Browse files
committed
adapt open telemetry (traces, metrics) WIP
1 parent e75ed12 commit 00a0e59

11 files changed

+226
-17
lines changed

Dockerfile

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Build stage
22
FROM golang:1.21-alpine AS builder
33
WORKDIR /app
4+
COPY go.mod ./
5+
COPY go.sum ./
6+
RUN go mod download
47
COPY . .
58
RUN go build -o main main.go
69

db/store/migrate.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package db
33
import (
44
"database/sql"
55
"fmt"
6-
"log"
76
"os"
87
"path/filepath"
98
"strings"
@@ -29,8 +28,8 @@ func RunDBMigrations(db *sql.DB, migrationsURL string) {
2928
// log.Fatal().Msg("cannot create new migrate instance")
3029
slog.Error("cannot create new migrate instance %s", err)
3130
}
32-
migration.Up()
33-
if err = migration.Up(); err != nil && err != migrate.ErrNoChange {
31+
err = migration.Up()
32+
if err != nil && err != migrate.ErrNoChange {
3433
slog.Error("failed to run migrate up %s", err)
3534

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

8281
// Execute each SQL file
8382
for _, file := range sqlFiles {
84-
log.Printf("Executing SQL file: %s", file)
83+
// log.Printf("Executing SQL file: %s", file)
8584

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

97-
log.Printf("Finished executing SQL file: %s", file)
96+
// log.Printf("Finished executing SQL file: %s", file)
9897
}
9998

10099
return nil

docker-compose.yml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
version: "3.8" # Ensure you're using version 1.29 or above for health checks in depends_on
21

32
services:
43
postgres:

gapi/logger_test.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func TestHttpLogger(t *testing.T) {
5656
// Mock HTTP handler function
5757
mockHandler := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
5858
res.WriteHeader(http.StatusOK)
59-
res.Write([]byte("OK"))
59+
_, err := res.Write([]byte("OK"))
60+
require.NoError(t, err)
6061
})
6162

6263
// Create a test HTTP request
@@ -82,7 +83,9 @@ func TestHttpLoggerErr(t *testing.T) {
8283
// Mock HTTP handler function
8384
mockHandler := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
8485
res.WriteHeader(http.StatusInternalServerError)
85-
res.Write([]byte("Error"))
86+
_, err := res.Write([]byte("Error"))
87+
require.NoError(t, err)
88+
8689
})
8790

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

118121
// Write some data to ResponseRecorder
119-
rec.Write([]byte("Test Body"))
122+
_, err := rec.Write([]byte("Test Body"))
123+
require.NoError(t, err)
120124

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

143-
go func() {
147+
go func() error {
144148
defer wg.Done()
145149
var buf bytes.Buffer
146-
io.Copy(&buf, r)
150+
_, err := io.Copy(&buf, r)
151+
if err != nil {
152+
return err
153+
}
147154
logCallback(buf.String())
155+
return nil
148156
}()
149157

150158
fn()

go.mod

+14-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ require (
1212
github.com/o1egl/paseto v1.0.0
1313
github.com/spf13/viper v1.18.2
1414
github.com/stretchr/testify v1.9.0
15+
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0
16+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0
17+
go.opentelemetry.io/otel v1.24.0
18+
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.24.0
19+
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.24.0
20+
go.opentelemetry.io/otel/sdk v1.24.0
21+
go.opentelemetry.io/otel/sdk/metric v1.24.0
1522
go.uber.org/mock v0.4.0
1623
golang.org/x/crypto v0.21.0
1724
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237
@@ -23,6 +30,9 @@ require (
2330
require (
2431
github.com/cespare/xxhash/v2 v2.2.0 // indirect
2532
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
33+
github.com/felixge/httpsnoop v1.0.4 // indirect
34+
github.com/go-logr/logr v1.4.1 // indirect
35+
github.com/go-logr/stdr v1.2.2 // indirect
2636
github.com/golang/protobuf v1.5.4 // indirect
2737
github.com/hashicorp/errwrap v1.1.0 // indirect
2838
github.com/hashicorp/go-multierror v1.1.1 // indirect
@@ -31,12 +41,15 @@ require (
3141
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438 // indirect
3242
github.com/jackc/pgio v1.0.0 // indirect
3343
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
34-
github.com/jackc/pgtype v1.14.2 // indirect
44+
github.com/jackc/pgtype v1.14.3 // indirect
3545
github.com/jackc/pgx/v4 v4.18.3 // indirect
3646
github.com/jackc/puddle/v2 v2.2.1 // indirect
3747
github.com/redis/go-redis/v9 v9.5.1 // indirect
3848
github.com/robfig/cron/v3 v3.0.1 // indirect
49+
go.opentelemetry.io/otel/metric v1.24.0 // indirect
50+
go.opentelemetry.io/otel/trace v1.24.0 // indirect
3951
go.uber.org/atomic v1.11.0 // indirect
52+
go.uber.org/goleak v1.3.0 // indirect
4053
golang.org/x/net v0.22.0 // indirect
4154
golang.org/x/sync v0.6.0 // indirect
4255
golang.org/x/time v0.5.0 // indirect

0 commit comments

Comments
 (0)