-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdout_forwarder.go
44 lines (37 loc) · 1.1 KB
/
stdout_forwarder.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
// Copyright 2023, Shulhan <ms@kilabit.info>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package telemetry
import (
"fmt"
"os"
)
// StdoutForwarder write the metrics to os.Stdout.
// This type is used as example and to provide wrapper for os.Stdout, since
// user should not call Close on os.Stdout.
type StdoutForwarder struct {
formatter Formatter
}
// NewStdoutForwarder create new StdoutForwarder using f as Formatter.
func NewStdoutForwarder(f Formatter) *StdoutForwarder {
return &StdoutForwarder{
formatter: f,
}
}
// Close on StdoutForwarder sync the Stdout.
func (stdout *StdoutForwarder) Close() error {
os.Stdout.Sync()
return nil
}
// Formatter return the Formatter used by this StdoutForwarder.
func (stdout *StdoutForwarder) Formatter() Formatter {
return stdout.formatter
}
// Write the raw metrics to stdout.
func (stdout *StdoutForwarder) Write(wire []byte) (n int, err error) {
n, err = os.Stdout.Write(wire)
if err != nil {
return n, fmt.Errorf(`StdoutForwarder.Forward: %w`, err)
}
return n, nil
}