forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
152 lines (126 loc) · 3.91 KB
/
logging.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 harness
import (
"context"
"fmt"
"os"
"runtime"
"time"
"github.com/apache/beam/sdks/go/pkg/beam/log"
pb "github.com/apache/beam/sdks/go/pkg/beam/model/fnexecution_v1"
"github.com/golang/protobuf/ptypes"
)
// TODO(herohde) 10/12/2017: make this file a separate package. Then
// populate InstructionReference and PrimitiveTransformReference properly.
// TODO(herohde) 10/13/2017: add top-level harness.Main panic handler that flushes logs.
// Also make logger flush on Fatal severity messages.
type contextKey string
const instKey contextKey = "beam:inst"
func setInstID(ctx context.Context, id string) context.Context {
return context.WithValue(ctx, instKey, id)
}
func tryGetInstID(ctx context.Context) (string, bool) {
id := ctx.Value(instKey)
if id == nil {
return "", false
}
return id.(string), true
}
type logger struct {
out chan<- *pb.LogEntry
}
func (l *logger) Log(ctx context.Context, sev log.Severity, calldepth int, msg string) {
now, _ := ptypes.TimestampProto(time.Now())
entry := &pb.LogEntry{
Timestamp: now,
Severity: convertSeverity(sev),
Message: msg,
}
if _, file, line, ok := runtime.Caller(calldepth); ok {
entry.LogLocation = fmt.Sprintf("%v:%v", file, line)
}
if id, ok := tryGetInstID(ctx); ok {
entry.InstructionReference = id
}
select {
case l.out <- entry:
// ok
default:
// buffer full: drop to stderr.
fmt.Fprintln(os.Stderr, msg)
}
}
func convertSeverity(sev log.Severity) pb.LogEntry_Severity_Enum {
switch sev {
case log.SevDebug:
return pb.LogEntry_Severity_DEBUG
case log.SevInfo:
return pb.LogEntry_Severity_INFO
case log.SevWarn:
return pb.LogEntry_Severity_WARN
case log.SevError:
return pb.LogEntry_Severity_ERROR
case log.SevFatal:
return pb.LogEntry_Severity_CRITICAL
default:
return pb.LogEntry_Severity_INFO
}
}
// setupRemoteLogging redirects local log messages to FnHarness. It will
// try to reconnect, if a connection goes bad. Falls back to stdout.
func setupRemoteLogging(ctx context.Context, endpoint string) {
buf := make(chan *pb.LogEntry, 2000)
log.SetLogger(&logger{out: buf})
w := &remoteWriter{buf, endpoint}
go w.Run(ctx)
}
type remoteWriter struct {
buffer chan *pb.LogEntry
endpoint string
}
func (w *remoteWriter) Run(ctx context.Context) error {
for {
err := w.connect(ctx)
fmt.Fprintf(os.Stderr, "Remote logging failed: %v. Retrying in 5 sec ...\n", err)
time.Sleep(5 * time.Second)
}
}
func (w *remoteWriter) connect(ctx context.Context) error {
conn, err := dial(ctx, w.endpoint, 30*time.Second)
if err != nil {
return err
}
defer conn.Close()
client, err := pb.NewBeamFnLoggingClient(conn).Logging(ctx)
if err != nil {
return err
}
defer client.CloseSend()
for msg := range w.buffer {
// fmt.Fprintf(os.Stderr, "REMOTE: %v\n", proto.MarshalTextString(msg))
// TODO: batch up log messages
list := &pb.LogEntry_List{
LogEntries: []*pb.LogEntry{msg},
}
recordLogEntries(list)
if err := client.Send(list); err != nil {
fmt.Fprintf(os.Stderr, "Failed to send message: %v\n%v", err, msg)
return err
}
// fmt.Fprintf(os.Stderr, "SENT: %v\n", msg)
}
return fmt.Errorf("internal: buffer closed?")
}