-
Notifications
You must be signed in to change notification settings - Fork 74
/
timestamp.go
70 lines (60 loc) · 2.3 KB
/
timestamp.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
// Copyright 2015 trivago GmbH
//
// 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 format
import (
"github.com/trivago/gollum/core"
"github.com/trivago/gollum/shared"
)
// Timestamp is a formatter that allows prefixing a message with a timestamp
// (time of arrival at gollum) as well as postfixing it with a delimiter string.
// Configuration example
//
// - "<producer|stream>":
// Formatter: "format.Timestamp"
// TimestampFormatter: "format.Envelope"
// Timestamp: "2006-01-02T15:04:05.000 MST | "
//
// Timestamp defines a Go time format string that is used to format the actual
// timestamp that prefixes the message.
// By default this is set to "2006-01-02 15:04:05 MST | "
//
// TimestampDataFormatter defines the formatter for the data transferred as
// message. By default this is set to "format.Forward"
type Timestamp struct {
base core.Formatter
timestampFormat string
}
func init() {
shared.RuntimeType.Register(Timestamp{})
}
// Configure initializes this formatter with values from a plugin config.
func (format *Timestamp) Configure(conf core.PluginConfig) error {
plugin, err := core.NewPluginWithType(conf.GetString("TimestampFormatter", "format.Forward"), conf)
if err != nil {
return err
}
format.base = plugin.(core.Formatter)
format.timestampFormat = conf.GetString("Timestamp", "2006-01-02 15:04:05 MST | ")
return nil
}
// Format prepends the timestamp of the message to the message.
func (format *Timestamp) Format(msg core.Message) ([]byte, core.MessageStreamID) {
basePayload, stream := format.base.Format(msg)
baseLength := len(basePayload)
timestampStr := msg.Timestamp.Format(format.timestampFormat)
payload := make([]byte, len(timestampStr)+baseLength)
len := copy(payload, []byte(timestampStr))
copy(payload[len:], basePayload)
return payload, stream
}