Skip to content

Commit

Permalink
fix generated formats
Browse files Browse the repository at this point in the history
  • Loading branch information
sharnoff committed Feb 13, 2025
1 parent 5956ccd commit 4a2bfd6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/agent/billing/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func createClients(ctx context.Context, logger *zap.Logger, cfg ClientsConfig) (

func jsonArrayBatch[B reporting.IOBuffer](buf func() B) func() reporting.BatchBuilder[*IncrementalEvent] {
return func() reporting.BatchBuilder[*IncrementalEvent] {
return reporting.NewJSONArrayBuilder[*IncrementalEvent](buf())
return reporting.NewJSONArrayBuilder[*IncrementalEvent](buf(), "events")
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/scalingevents/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func createClients(ctx context.Context, logger *zap.Logger, cfg ClientsConfig) (

func jsonLinesBatch[B reporting.IOBuffer](buf func() B) func() reporting.BatchBuilder[ScalingEvent] {
return func() reporting.BatchBuilder[ScalingEvent] {
return reporting.NewJSONArrayBuilder[ScalingEvent](buf())
return reporting.NewJSONLinesBuilder[ScalingEvent](buf())
}
}

Expand Down
35 changes: 30 additions & 5 deletions pkg/reporting/batch_jsonarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,42 @@ var _ BatchBuilder[int] = (*JSONArrayBuilder[int])(nil)
// JSONArrayBuilder is a BatchBuilder where all the events in a batch are serialized as a single
// large JSON array.
type JSONArrayBuilder[E any] struct {
buf IOBuffer
started bool
buf IOBuffer
started bool
nestingCount int
}

// NewJSONArrayBatch creates a new JSONArrayBuilder using the underlying IOBuffer to potentially
// process the JSON encoding -- either with ByteBuffer for plaintext or GZIPBuffer for gzip
// compression.
func NewJSONArrayBuilder[E any](buf IOBuffer) *JSONArrayBuilder[E] {
func NewJSONArrayBuilder[E any](buf IOBuffer, nestedFields ...string) *JSONArrayBuilder[E] {
for _, fieldName := range nestedFields {
// note: use a discrete json.Marhsal here instead of json.Encoder because encoder adds a
// newline at the end, and that'll make the formatting weird for us.
encodedField, err := json.Marshal(fieldName)
if err != nil {
panic(fmt.Sprintf("failed to JSON encode: %s", fieldName))
}

if _, err := buf.Write([]byte{'{'}); err != nil {
panic(fmt.Sprintf("failed to write: %s", err))
}
if _, err := buf.Write(encodedField); err != nil {
panic(fmt.Sprintf("failed to write: %s", err))
}
if _, err := buf.Write([]byte{':'}); err != nil {
panic(fmt.Sprintf("failed to write: %s", err))
}
}
// open the array:
if _, err := buf.Write([]byte{'['}); err != nil {
panic(fmt.Sprintf("failed to write: %s", err))
}

return &JSONArrayBuilder[E]{
buf: buf,
started: false,
buf: buf,
started: false,
nestingCount: len(nestedFields),
}
}

Expand Down Expand Up @@ -53,6 +73,11 @@ func (b *JSONArrayBuilder[E]) Finish() []byte {
if _, err := b.buf.Write([]byte("\n]")); err != nil {
panic(fmt.Sprintf("failed to write: %s", err))
}
for i := 0; i < b.nestingCount; i++ {
if _, err := b.buf.Write([]byte("}")); err != nil {
panic(fmt.Sprintf("failed to write: %s", err))
}
}

return b.buf.Collect()
}

0 comments on commit 4a2bfd6

Please sign in to comment.