Skip to content

Commit

Permalink
feat: Add improved handling for multi-line messages
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Sep 9, 2022
1 parent 6b0e522 commit b590932
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions agent/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"os"
"strings"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
Expand Down Expand Up @@ -66,18 +67,27 @@ 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)))
}
}

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))
defer span.End()

span.SetAttributes(attribute.String("message", string(p)))
span.SetAttributes(attribute.String("raw_message", msg))

props := map[string]string{}
if err := json.Unmarshal(p, &props); err != nil {
span.SetName(string(p))
if err := json.Unmarshal([]byte(msg), &props); err != nil {
span.SetName(msg)
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())

return os.Stdout.Write(p)
return err
}

properties := []attribute.KeyValue{}
Expand All @@ -88,5 +98,5 @@ func (s *TelemetryLogStream) Write(p []byte) (n int, err error) {
span.SetAttributes(properties...)
span.SetName(props["@message"])

return len(p), nil
return nil
}
2 changes: 1 addition & 1 deletion terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ variable "vault_version" {

variable "vault_agent_version" {
description = "The version of the Vault agent to use."
default = "1.3.3"
default = "1.3.4"
}

variable "opentelemetry" {
Expand Down

0 comments on commit b590932

Please sign in to comment.