-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.go
123 lines (108 loc) · 4.45 KB
/
entrypoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package gen2
import (
"context"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
googlepropagator "github.com/GoogleCloudPlatform/opentelemetry-operations-go/propagator"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/ebi-yade/cloud-functions-samples/gen2/app/handlers"
"github.com/ebi-yade/cloud-functions-samples/gen2/app/pubsub"
"github.com/ebi-yade/cloud-functions-samples/gen2/app/web"
"github.com/ebi-yade/cloud-functions-samples/gen2/infra/topic"
"github.com/pkg/errors"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
)
func init() {
// https://cloud.google.com/blog/ja/products/application-development/graceful-shutdowns-cloud-run-deep-dive
const gracePeriod = 5 * time.Second // shorter than Cloud Run's grace period
ctx, stopInitial := signal.NotifyContext(context.Background(), syscall.SIGTERM)
// ==============================================================
// Setup observability solutions
// ==============================================================
logger := slog.New(NewLogHandler(os.Stderr, slog.LevelInfo))
slog.SetDefault(logger)
// maybe you want to get the project ID from the metadata server
projectID := mustEnv("GOOGLE_CLOUD_PROJECT")
slog.SetDefault(logger.With(slog.String("project_id", projectID)))
propagators := []propagation.TextMapPropagator{
googlepropagator.CloudTraceOneWayPropagator{},
propagation.TraceContext{},
propagation.Baggage{},
}
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagators...))
tp, err := NewTracerProvider(ctx, projectID, 0.1)
if err != nil {
fatal(ctx, errors.Wrap(err, "error NewTracerProvider"))
}
otel.SetTracerProvider(tp)
// ==============================================================
// Initialize infrastructure dependencies
// ==============================================================
topicID := mustEnv("PUBSUB_TOPIC_ID")
googleTopic, err := topic.NewGoogleTopic(ctx, projectID, topicID)
if err != nil {
fatal(ctx, errors.Wrap(err, "error topic.NewGoogleTopic"))
}
slog.InfoContext(ctx, "initialized pubsub topic", slog.String("topic", topicID))
// ==============================================================
// Register HTTP / Event-driven handlers
// ==============================================================
h := handlers.New(googleTopic)
webMids := web.Middlewares{
web.Recover,
}
functionsHTTP("functions-samples-start", web.BuildStdHttpFunc(webMids, h.Start))
functionsCloudEvent("functions-samples-hook", pubsub.BuildEventDrivenFunc(h.Hook))
// ==============================================================
// Start an asynchronous routine to handle shutdown signals
// ==============================================================
go func() {
defer stopInitial()
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), gracePeriod)
defer cancel()
slog.InfoContext(ctx, "shutting down...")
if err := googleTopic.Close(ctx); err != nil {
slog.ErrorContext(ctx, fmt.Sprintf("error pubsubClient.Close: %+v", err))
}
if err := tp.ForceFlush(ctx); err != nil {
slog.ErrorContext(ctx, fmt.Sprintf("error ForceFlush: %+v", err))
}
slog.InfoContext(ctx, "shutdown completed. bye!")
}()
}
func mustEnv(key string) string {
v := os.Getenv(key)
if v == "" {
panic(fmt.Sprintf("missing env: %s", key))
}
slog.Info("detected value from environment", slog.String("key", key), slog.String("value", v))
return v
}
func fatal(ctx context.Context, err error) {
slog.ErrorContext(ctx, fmt.Sprintf("exit with: %+v", err))
os.Exit(1)
}
// functionsHTTP は HTTP 関数を登録するための functions.HTTP をラップして otel に対応させたものです。
func functionsHTTP(entrypoint string, stdHandler http.HandlerFunc) {
otelHandler := otelhttp.NewHandler(stdHandler, entrypoint)
functions.HTTP(entrypoint, otelHandler.ServeHTTP)
}
// functionsCloudEvent はイベントドリブン関数を登録するための functions.CloudEvent をラップして otel に対応させたものです。
func functionsCloudEvent(name string, fn func(context.Context, event.Event) error) {
otelFn := func(ctx context.Context, e event.Event) error {
ctx, span := tracer.Start(ctx, name)
defer span.End()
return fn(ctx, e)
}
functions.CloudEvent(name, otelFn)
}
var tracer = otel.Tracer("github.com/ebi-yade/cloud-functions-samples/gen2")