This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
/
main.go
92 lines (74 loc) · 2.83 KB
/
main.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
/*
Copyright 2019 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"flag"
"log"
"cloud.google.com/go/compute/metadata"
"github.com/kelseyhightower/envconfig"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"knative.dev/eventing/pkg/tracing"
"github.com/google/knative-gcp/pkg/pubsub/publisher"
tracingconfig "github.com/google/knative-gcp/pkg/tracing"
)
type envConfig struct {
// Environment variable containing project id.
Project string `envconfig:"PROJECT_ID"`
// Topic is the environment variable containing the PubSub Topic being
// subscribed to's name. In the form that is unique within the project.
// E.g. 'laconia', not 'projects/my-gcp-project/topics/laconia'.
Topic string `envconfig:"PUBSUB_TOPIC_ID" required:"true"`
// TracingConfigJson is a JSON string of tracing.Config. This is used to configure tracing. The
// original config is stored in a ConfigMap inside the controller's namespace. Its value is
// copied here as a JSON string.
TracingConfigJson string `envconfig:"K_TRACING_CONFIG" required:"true"`
}
func main() {
flag.Parse()
ctx := context.Background()
logCfg := zap.NewProductionConfig() // TODO: to replace with a dynamically updating logger.
logCfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
logger, err := logCfg.Build()
if err != nil {
log.Fatalf("Unable to create logger: %v", err)
}
var env envConfig
if err := envconfig.Process("", &env); err != nil {
logger.Fatal("Failed to process env var", zap.Error(err))
}
if env.Project == "" {
project, err := metadata.ProjectID()
if err != nil {
logger.Fatal("failed to find project id. ", zap.Error(err))
}
env.Project = project
}
logger.Info("Using project.", zap.String("project", env.Project))
tracingConfig, err := tracingconfig.JSONToConfig(env.TracingConfigJson)
if err != nil {
logger.Error("Failed to process tracing options", zap.Error(err))
}
if err := tracing.SetupStaticPublishing(logger.Sugar(), "", tracingConfig); err != nil {
logger.Error("Failed to setup tracing", zap.Error(err), zap.Any("tracingConfig", tracingConfig))
}
startable := &publisher.Publisher{
ProjectID: env.Project,
TopicID: env.Topic,
}
logger.Info("Starting Pub/Sub Publisher.", zap.Any("publisher", startable))
if err := startable.Start(ctx); err != nil {
logger.Fatal("failed to start publisher: ", zap.Error(err))
}
}