Skip to content

Commit

Permalink
tweak: Switch to reporting logs as individual spans to ensure realtim…
Browse files Browse the repository at this point in the history
…e access
  • Loading branch information
notheotherben committed Sep 9, 2022
1 parent 3fa6c6b commit 327c0f6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
3 changes: 3 additions & 0 deletions agent/executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func SetExecutablePattern(ctx context.Context, pattern string) {

matches, err := filepath.Glob(pattern)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return
}
Expand All @@ -35,6 +36,7 @@ func ensureExecutable(ctx context.Context, app string) {

stat, err := os.Stat(app)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return
}
Expand All @@ -52,6 +54,7 @@ func ensureExecutable(ctx context.Context, app string) {
err := os.Chmod(app, stat.Mode()|0111)
if err != nil {
span.SetAttributes(attribute.String("mode.final", stat.Mode().String()))
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
} else {
span.SetAttributes(attribute.String("mode.final", (stat.Mode() | 0111).String()))
Expand Down
4 changes: 3 additions & 1 deletion agent/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func RunApp(ctx context.Context, app string, args []string) error {
ctx, span := otel.Tracer("vault").Start(ctx, "launcher.RunApp")
ctx, span := otel.Tracer("vault").Start(ctx, "launcher.RunApp", trace.WithSpanKind(trace.SpanKindServer))
defer span.End()

ensureExecutable(ctx, app)
Expand All @@ -39,6 +39,7 @@ func RunApp(ctx context.Context, app string, args []string) error {
select {
case s := <-c:
if cmd.Process != nil {
span.AddEvent("Propagating signal to child process.", trace.WithAttributes(attribute.String("signal", s.String())))
cmd.Process.Signal(s)
}
case <-exit:
Expand All @@ -52,6 +53,7 @@ func RunApp(ctx context.Context, app string, args []string) error {
err = cmd.Run()

if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}

Expand Down
17 changes: 9 additions & 8 deletions agent/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"context"
"encoding/json"
"os"
"time"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
Expand Down Expand Up @@ -65,8 +66,13 @@ func NewTelemetryLogStream(ctx context.Context, span trace.Span) *TelemetryLogSt
}

func (s *TelemetryLogStream) Write(p []byte) (n int, err error) {
_, span := otel.Tracer("vault").Start(s.ctx, "launcher.TelemetryLogStream.Write", trace.WithSpanKind(trace.SpanKindConsumer))
defer span.End()

props := map[string]string{}
if err := json.Unmarshal(p, &props); err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
s.span.AddEvent(
"Failed to parse log message",
trace.WithAttributes(attribute.String("message", string(p)), attribute.String("error", err.Error())),
Expand All @@ -75,18 +81,13 @@ func (s *TelemetryLogStream) Write(p []byte) (n int, err error) {
return os.Stdout.Write(p)
}

options := []trace.EventOption{}
if ts, err := time.Parse(time.RFC3339, props["@timestamp"]); err == nil {
options = append(options, trace.WithTimestamp(ts))
}

properties := []attribute.KeyValue{}
for k, v := range props {
properties = append(properties, attribute.String(k, v))
}
options = append(options, trace.WithAttributes(properties...))

s.span.AddEvent(props["@message"], options...)
span.SetAttributes(properties...)
span.SetName(props["@message"])

return len(p), 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.1"
default = "1.3.2"
}

variable "opentelemetry" {
Expand Down

0 comments on commit 327c0f6

Please sign in to comment.