Skip to content

Commit 5ad9cb6

Browse files
Various fixes for issue #8 (#9)
- added caution for Windows usage - APM usage without initialization now panics instead of auto-init - fixed issue with OTEL init without collector URL for traces - some default IDE configs
1 parent 791abfa commit 5ad9cb6

7 files changed

Lines changed: 34 additions & 26 deletions

File tree

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,11 @@ $RECYCLE.BIN/
105105
*.lnk
106106

107107
# End of https://www.toptal.com/developers/gitignore/api/go,visualstudiocode,osx,linux,windows
108+
109+
### Additional IDEs/Editors. ###
110+
/.idea/
111+
/*.iml
112+
113+
/.vimrc
114+
*.swp
115+
*.tmp

inits.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ var now = time.Now()
2727

2828
func startAPM(ctx context.Context, cfg *configs.Configs) *apm.APM {
2929
ap, err := apm.New(ctx, &apm.Options{
30-
Debug: cfg.Environment == configs.EnvLocal,
31-
Environment: cfg.Environment.String(),
32-
ServiceName: cfg.AppName,
33-
ServiceVersion: cfg.AppVersion,
34-
TracesSampleRate: 50.00,
35-
UseStdOut: cfg.Environment == configs.EnvLocal,
30+
Debug: cfg.Environment == configs.EnvLocal,
31+
Environment: cfg.Environment.String(),
32+
ServiceName: cfg.AppName,
33+
ServiceVersion: cfg.AppVersion,
34+
PrometheusScrapePort: 9090,
35+
TracesSampleRate: 50.00,
36+
UseStdOut: cfg.Environment == configs.EnvLocal,
3637
})
3738
if err != nil {
3839
panic(errors.Wrap(err, "failed to start APM"))

internal/pkg/apm/apm.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/naughtygopher/errors"
10+
"github.com/naughtygopher/goapp/internal/pkg/logger"
1011
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
1112
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
1213
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
@@ -70,7 +71,7 @@ func New(ctx context.Context, opts *Options) (*APM, error) {
7071
s.meterProvider = mProvider
7172
SetGlobal(s)
7273

73-
return s, nil
74+
return Global(), nil
7475
}
7576

7677
// Shutdown gracefully switch off apm, flushing any data it have
@@ -140,9 +141,7 @@ func SetGlobal(apm *APM) {
140141
// Global gets global apm instance
141142
func Global() *APM {
142143
if global == nil {
143-
apm, _ := New(context.Background(), &Options{UseStdOut: false})
144-
global = apm
145-
return apm
144+
logger.Error(context.Background(), "APM access attempt before initialisation is a bug")
146145
}
147146
return global
148147
}
@@ -187,6 +186,9 @@ func newTracer(ctx context.Context, opts *Options) (trace.TracerProvider, *Trace
187186

188187
if opts.UseStdOut {
189188
exporter, err = stdouttrace.New()
189+
} else if opts.CollectorURL == "" {
190+
// Using no-op tracer as CollectorURL was not set.
191+
return nil, nil, nil
190192
} else if httpCollector {
191193
exporter, err = otlptracehttp.New(
192194
ctx,

internal/pkg/apm/prometheus.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package apm
22

33
import (
4+
"context"
45
"fmt"
56
"net/http"
67
"time"
78

89
"github.com/naughtygopher/errors"
10+
"github.com/naughtygopher/goapp/internal/pkg/logger"
911
"github.com/prometheus/client_golang/prometheus/promhttp"
1012
"go.opentelemetry.io/otel/exporters/prometheus"
1113
)
@@ -24,24 +26,13 @@ func prometheusScraper(opts *Options) {
2426
mux.Handle("/-/metrics", promhttp.Handler())
2527
server := &http.Server{
2628
Handler: mux,
27-
Addr: fmt.Sprintf("%d", opts.PrometheusScrapePort),
29+
Addr: fmt.Sprintf(":%d", opts.PrometheusScrapePort),
2830
ReadHeaderTimeout: 5 * time.Second,
2931
}
3032

31-
// logger.Info(
32-
// "[otel/http] starting prometheus scrape endpoint",
33-
// zap.String(
34-
// "addr",
35-
// fmt.Sprintf("localhost:%d/-/metrics", opts.PrometheusScrapePort),
36-
// ),
37-
// )
33+
logger.Info(context.Background(), fmt.Sprintf("[otel/http] starting prometheus metrics on :%d/-/metrics", opts.PrometheusScrapePort))
3834
err := server.ListenAndServe()
3935
if err != nil {
40-
// logger.Error(
41-
// "[otel/http] failed to serve metrics at:",
42-
// zap.Error(err),
43-
// zap.Uint16("port", opts.PrometheusScrapePort),
44-
// )
45-
return
36+
logger.Error(context.Background(), fmt.Sprintf("[otel/http] failed to start prometheus metrics on :%d/-/metrics ; %+v", opts.PrometheusScrapePort, err))
4637
}
4738
}

internal/pkg/sysignals/sysignals.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func NotifyErrorOnQuit(errs chan<- error, otherSignals ...syscall.Signal) {
2020

2121
for signalType := range interrupt {
2222
switch signalType {
23+
// CAUTION: syscall.SIGTSTP is not supported on Windows and go build will fail.
2324
case syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGTSTP:
2425
errs <- errors.Wrapf(ErrSigQuit, "%v", signalType)
2526
return

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ func main() {
7474
}),
7575
)
7676

77+
// This needs to remain after log initialisation and before server initialisation.
78+
ap := startAPM(ctx, cfgs)
79+
7780
healthResponder, err := startHealthResponder(ctx, probestatus, fatalErr)
7881
if err != nil {
7982
panic(err)
@@ -93,7 +96,7 @@ func main() {
9396
healthResponder,
9497
hserver,
9598
gserver,
96-
startAPM(ctx, cfgs),
99+
ap,
97100
)
98101
exitErr = <-fatalErr
99102
}

shutdown.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ func shutdown(
3838
probes as much context as possible. Esepcially during the graceful shutdown period.
3939
Hence it is recommended to setup an independent server for health checks alone.
4040
*/
41-
defer healthResp.Shutdown(ctx)
41+
defer func() {
42+
_ = healthResp.Shutdown(ctx)
43+
}()
4244

4345
/*
4446
When a server begins its shutdown process, it first signals Kubernetes (or any other prober)

0 commit comments

Comments
 (0)