Skip to content

Commit

Permalink
feat: Improve handling of log lines which don't contain valid JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Sep 9, 2022
1 parent b590932 commit f9fcc26
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions agent/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,25 @@ func NewTelemetryLogStream(ctx context.Context, span trace.Span) *TelemetryLogSt
}

func (s *TelemetryLogStream) Write(p []byte) (n int, err error) {
for _, line := range strings.Split(string(p), "\n") {
if err := s.WriteMessage(line); err != nil {
s.span.RecordError(err, trace.WithAttributes(attribute.String("raw_message", line)))
if !strings.HasPrefix(string(p), `{"`) {
_, span := otel.Tracer("vault").Start(s.ctx, "log", trace.WithSpanKind(trace.SpanKindConsumer))
defer span.End()

span.SetName(string(p))
span.SetAttributes(attribute.String("raw_message", string(p)))
} else {
for _, line := range strings.Split(string(p), "\n") {
if err := s.WriteMessage(line); err != nil {
s.span.RecordError(err, trace.WithAttributes(attribute.String("raw_message", line)))
}
}
}

return os.Stdout.Write(p)
}

func (s *TelemetryLogStream) WriteMessage(msg string) error {
_, span := otel.Tracer("vault").Start(s.ctx, "launcher.TelemetryLogStream.Write", trace.WithSpanKind(trace.SpanKindConsumer))
_, span := otel.Tracer("vault").Start(s.ctx, "log", trace.WithSpanKind(trace.SpanKindConsumer))
defer span.End()

span.SetAttributes(attribute.String("raw_message", msg))
Expand Down

0 comments on commit f9fcc26

Please sign in to comment.