Skip to content

Commit 6632a0b

Browse files
committed
docs + example
1 parent b10b4a3 commit 6632a0b

File tree

10 files changed

+226
-2
lines changed

10 files changed

+226
-2
lines changed

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,10 @@ vuln:
3636
which govulncheck
3737
govulncheck -version
3838
govulncheck ./...
39+
40+
start: run
41+
42+
run:
43+
go run example/example.go
44+
45+
include make/*.mk

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,59 @@ pkg
66

77
Various functions copy-pasted between different projects.
88

9+
See example: [example.go](example%2Fexample.go)
10+
911
cryptorand
1012
=======================================
1113
Generate cryptographically secure random strings with alphabet provided
1214

15+
Documentation: https://pkg.go.dev/github.com/vodolaz095/pkg/cryptorand
16+
1317
date
1418
=======================================
1519
Date and time helpers - see [moments_test.go](date%2Fmoments_test.go)
1620

21+
Documentation: https://pkg.go.dev/github.com/vodolaz095/pkg/date
22+
23+
24+
healthcheck
25+
=======================================
26+
Systemd compatible healthcheck.
27+
Documentation:
28+
https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html
29+
30+
Usage example:
31+
https://github.com/vodolaz095/stocks_broadcaster/blob/a03cf70efc1e333e959f58bd295aa2701cca37c8/main.go#L131-L160
32+
1733
math
1834
=======================================
1935
Various generic mathematical functions copy-pasted between different projects.
2036

37+
Documentation: https://pkg.go.dev/github.com/vodolaz095/pkg/math
38+
2139
stopper
2240
=======================================
2341
Make global application context which can be terminated by signals
2442

43+
Documentation: https://pkg.go.dev/github.com/vodolaz095/pkg/stopper
44+
2545
tracing
2646
=======================================
27-
Opinionated way to configure OpenTelemetry with `jaegertracing/all-in-one` started like with docker compose like this
47+
Opinionated way to configure OpenTelemetry with `jaegertracing/all-in-one` started with docker compose like this
2848

2949
```yaml
3050

3151
version: "3.11"
3252

53+
volumes:
54+
jaeger_temp:
55+
3356
services:
3457
jaeger:
3558
container_name: jaeger
36-
image: docker.io/jaegertracing/all-in-one:1.37
59+
image: docker.io/jaegertracing/all-in-one:1.39.0
60+
volumes:
61+
- jaeger_temp:/tmp
3762
ports:
3863
- "16686:16686/tcp" # webui is listening
3964
- "14268:14268/tcp" # accepting spans in compact jaeger thrift format over http
@@ -44,3 +69,6 @@ services:
4469
zerologger
4570
=======================================
4671
Opinionated way to configure zerolog with sane defaults
72+
73+
Documentation: https://pkg.go.dev/github.com/vodolaz095/pkg/zerologger
74+
Usage example: [zerologger_test.go](zerologger%2Fzerologger_test.go)

docker-compose.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
version: "3.11"
2+
3+
volumes:
4+
jaeger_temp:
5+
6+
services:
7+
jaeger:
8+
container_name: jaeger
9+
image: docker.io/jaegertracing/all-in-one:1.39.0
10+
volumes:
11+
- jaeger_temp:/tmp
12+
ports:
13+
- "16686:16686/tcp" # webui is listening
14+
- "14268:14268/tcp" # accepting spans in compact jaeger thrift format over http
15+
- "6831:6831/udp" # accepting spans in compact jaeger thrift format over udp

example/example.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package main
2+
3+
import (
4+
"time"
5+
6+
"github.com/rs/zerolog/log"
7+
"go.opentelemetry.io/otel"
8+
"go.opentelemetry.io/otel/attribute"
9+
semconv "go.opentelemetry.io/otel/semconv/v1.27.0"
10+
"go.opentelemetry.io/otel/trace"
11+
"golang.org/x/sync/errgroup"
12+
13+
"github.com/vodolaz095/pkg/stopper"
14+
"github.com/vodolaz095/pkg/tracing"
15+
"github.com/vodolaz095/pkg/zerologger"
16+
)
17+
18+
func main() {
19+
initialCtx, cancel := stopper.New()
20+
defer cancel()
21+
22+
// configure logging
23+
zerologger.Configure(zerologger.Log{Level: zerologger.TraceLevel})
24+
25+
// configure tracing
26+
err := tracing.ConfigureUDP(tracing.UDPConfig{Ratio: 1},
27+
semconv.ServiceName("pkg_example"),
28+
attribute.String("environment", "example"),
29+
)
30+
if err != nil {
31+
log.Error().Err(err).Msgf("error setting tracing")
32+
return
33+
}
34+
35+
eg, ctx := errgroup.WithContext(initialCtx)
36+
eg.Go(func() error {
37+
tt := time.NewTicker(time.Second)
38+
for {
39+
select {
40+
case <-ctx.Done():
41+
tt.Stop()
42+
log.Info().Msg("ticker 1 is stopping...")
43+
return nil
44+
case <-tt.C:
45+
log.Warn().Msg("Hello from ticker 1!")
46+
}
47+
}
48+
})
49+
50+
eg.Go(func() error {
51+
time.Sleep(500 * time.Millisecond)
52+
tt := time.NewTicker(time.Second)
53+
for {
54+
select {
55+
case <-ctx.Done():
56+
tt.Stop()
57+
log.Info().Msg("ticker 2 is stopping...")
58+
return nil
59+
case <-tt.C:
60+
log.Info().Msg("Hello from ticker 2!")
61+
}
62+
}
63+
})
64+
65+
eg.Go(func() error {
66+
tt := time.NewTicker(3 * time.Second)
67+
var n = 0
68+
for {
69+
select {
70+
case <-ctx.Done():
71+
tt.Stop()
72+
log.Info().Msg("ticker 3 is stopping...")
73+
return nil
74+
case <-tt.C:
75+
n += 1
76+
_, span := otel.Tracer("ticker").Start(ctx, "loop",
77+
trace.WithAttributes(attribute.Int("iteration", n)))
78+
span.AddEvent("Hello from ticker 3!", trace.WithAttributes(attribute.Int("iteration", n)))
79+
log.Info().
80+
Int("iteration", n).
81+
Str("trace_id", span.SpanContext().TraceID().String()).
82+
Msg("Hello from ticker 3!")
83+
span.End()
84+
}
85+
}
86+
})
87+
88+
eg.Go(func() error {
89+
return tracing.Wait(ctx)
90+
})
91+
92+
err = eg.Wait()
93+
if err != nil {
94+
log.Error().Err(err).Msgf("error starting application")
95+
return
96+
}
97+
log.Info().Msg("Application is stopped")
98+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
github.com/pmezard/go-difflib v1.0.0 // indirect
2323
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
2424
go.opentelemetry.io/otel/metric v1.33.0 // indirect
25+
golang.org/x/sync v0.10.0 // indirect
2526
golang.org/x/sys v0.28.0 // indirect
2627
gopkg.in/yaml.v3 v3.0.1 // indirect
2728
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ go.opentelemetry.io/otel/sdk v1.33.0 h1:iax7M131HuAm9QkZotNHEfstof92xM+N8sr3uHXc
4545
go.opentelemetry.io/otel/sdk v1.33.0/go.mod h1:A1Q5oi7/9XaMlIWzPSxLRWOI8nG3FnzHJNbiENQuihM=
4646
go.opentelemetry.io/otel/trace v1.33.0 h1:cCJuF7LRjUFso9LPnEAHJDB2pqzp+hbO8eu1qqW2d/s=
4747
go.opentelemetry.io/otel/trace v1.33.0/go.mod h1:uIcdVUZMpTAmz0tI1z04GoVSezK37CbGV4fr1f2nBck=
48+
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
49+
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
4850
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4951
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5052
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

healthcheck/watchdog.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,26 @@ func SetStatus(status string) (err error) {
2626
return
2727
}
2828

29+
// SetReloading notify systemd that service is reloading
30+
// https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html#RELOADING=1
31+
func SetReloading() (err error) {
32+
_, err = daemon.SdNotify(false, "RELOADING=1")
33+
return
34+
}
35+
36+
// SetStopping notify systemd that service is stopping
37+
// https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html#STOPPING=1
38+
func SetStopping() (err error) {
39+
_, err = daemon.SdNotify(false, "STOPPING=1")
40+
return
41+
}
42+
43+
// Notify sends free form notification to systemd about service state change
44+
func Notify(state string) (err error) {
45+
_, err = daemon.SdNotify(false, state)
46+
return
47+
}
48+
2949
// StartWatchDog starts background process that notifies systemd if application is running properly
3050
func StartWatchDog(mainCtx context.Context, pingers []Pinger) (err error) {
3151
var ok bool

make/docker.mk

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Installation of docker compose is described here
2+
# https://docs.docker.com/compose/install/linux/
3+
4+
docker/up:
5+
docker compose up -d
6+
docker ps
7+
8+
docker/down:
9+
docker compose down
10+
11+
docker/prune:
12+
docker system prune -a

make/podman.mk

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
podman/up:
2+
podman-compose up -d
3+
podman ps
4+
5+
podman/down:
6+
podman-compose down
7+
8+
podman/prune:
9+
podman system prune -a --volumes

zerologger/zerologger_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package zerologger
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/rs/zerolog/log"
8+
)
9+
10+
type testWriter struct {
11+
T *testing.T
12+
}
13+
14+
func (tw *testWriter) Write(p []byte) (n int, err error) {
15+
tw.T.Log(string(p))
16+
return len(p), nil
17+
}
18+
19+
func TestConfigure(t *testing.T) {
20+
Configure(Log{
21+
Level: TraceLevel,
22+
ToJournald: false,
23+
},
24+
&testWriter{T: t},
25+
os.Stdout,
26+
)
27+
log.Trace().Msg("trace")
28+
log.Debug().Msg("debug")
29+
log.Info().Msg("info")
30+
log.Warn().Msg("warn")
31+
log.Error().Msg("error")
32+
}

0 commit comments

Comments
 (0)