-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/reciever/jobreceiver] Implement logentries output handler
Signed-off-by: Christian Kruse <[email protected]>
- Loading branch information
Showing
6 changed files
with
228 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package logentries | ||
|
||
import ( | ||
"context" | ||
"io" | ||
|
||
"github.com/SumoLogic/sumologic-otel-collector/pkg/receiver/jobreceiver/output/consumer" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const ( | ||
streamNameStdout = "stdout" | ||
streamNameStderr = "stderr" | ||
commandNameLabel = "command.name" | ||
commandStreamNameLabel = "command.stream.name" | ||
) | ||
|
||
type handler struct { | ||
logger *zap.SugaredLogger | ||
writer consumer.WriterOp | ||
|
||
config LogEntriesConfig | ||
scanFactory scannerFactory | ||
} | ||
|
||
func (h *handler) Consume(ctx context.Context, stdout, stderr io.Reader) consumer.CloseFunc { | ||
go h.consume(ctx, stdout, streamNameStdout) | ||
go h.consume(ctx, stderr, streamNameStderr) | ||
return nopCloser | ||
} | ||
func (h *handler) consume(ctx context.Context, in io.Reader, stream string) { | ||
|
||
scanner := h.scanFactory.Build(in) | ||
for scanner.Scan() { | ||
ent, err := h.writer.NewEntry(scanner.Text()) | ||
if err != nil { | ||
h.logger.Errorf("log entry handler could not create a new log entry: %s", err) | ||
} | ||
if ent.Attributes == nil { | ||
ent.Attributes = map[string]interface{}{} | ||
} | ||
if h.config.IncludeCommandName { | ||
ent.Attributes[commandNameLabel] = ctx.Value(consumer.ContextKeyCommandName) | ||
} | ||
if h.config.IncludeStreamName { | ||
ent.Attributes[commandStreamNameLabel] = stream | ||
} | ||
h.writer.Write(ctx, ent) | ||
} | ||
if err := scanner.Err(); err != nil { | ||
h.logger.Errorf("error reading input stream %s: %w", stream, err) | ||
} | ||
} | ||
|
||
func nopCloser(_ consumer.ExecutionSummary) {} |
91 changes: 91 additions & 0 deletions
91
pkg/receiver/jobreceiver/output/logentries/handler_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package logentries | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestConsumeWithMaxLogSize(t *testing.T) { | ||
var w stubWriter | ||
cfg := LogEntriesConfig{ | ||
IncludeCommandName: true, | ||
IncludeStreamName: true, | ||
MaxLogSize: 128, | ||
} | ||
h, err := cfg.Build(zap.NewNop().Sugar(), &w) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
stdoutR, stdoutW := io.Pipe() | ||
stderrR, stderrW := io.Pipe() | ||
|
||
writeStdout := func(w io.WriteCloser) { | ||
fmt.Fprint(w, strings.Repeat("a", 64)) | ||
fmt.Fprint(w, strings.Repeat("a", 64)) | ||
for i := 0; i < 64; i = i + 8 { | ||
fmt.Fprint(w, strings.Repeat("b", 8)) | ||
} | ||
fmt.Fprint(w, "\n") | ||
w.Close() | ||
} | ||
|
||
writeStderr := func(w io.WriteCloser) { | ||
fmt.Fprint(w, "hello world") | ||
w.Close() | ||
} | ||
h.Consume(context.Background(), stdoutR, stderrR) | ||
|
||
go writeStdout(stdoutW) | ||
go writeStderr(stderrW) | ||
|
||
done := make(chan struct{}) | ||
go func() { | ||
close(done) | ||
}() | ||
|
||
require.Eventually(t, | ||
func() bool { | ||
w.MU.Lock() | ||
defer w.MU.Unlock() | ||
return len(w.Out) == 3 | ||
}, | ||
time.Second, | ||
time.Millisecond*100, | ||
"expected three log entries out", | ||
) | ||
for _, ent := range w.Out { | ||
body, ok := ent.Body.(string) | ||
require.True(t, ok) | ||
assert.LessOrEqual(t, len(body), 128) | ||
} | ||
} | ||
|
||
type stubWriter struct { | ||
MU sync.Mutex | ||
Out []*entry.Entry | ||
} | ||
|
||
func (s *stubWriter) NewEntry(value interface{}) (*entry.Entry, error) { | ||
e := entry.New() | ||
e.Attributes = make(map[string]interface{}) | ||
e.Resource = make(map[string]interface{}) | ||
e.Body = value | ||
return e, nil | ||
} | ||
|
||
func (s *stubWriter) Write(ctx context.Context, e *entry.Entry) { | ||
s.MU.Lock() | ||
s.Out = append(s.Out, e) | ||
s.MU.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters