-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chdump): add OTLP logs ingester
- Loading branch information
Showing
2 changed files
with
133 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package chdump | ||
|
||
import ( | ||
"context" | ||
|
||
"go.opentelemetry.io/collector/pdata/plog" | ||
"go.opentelemetry.io/collector/pdata/plog/plogotlp" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
// IngestLogs loads logs from dump and sends them to the collector. | ||
type IngestLogs struct { | ||
// Workers is the number of workers to use. | ||
// | ||
// Defaults to 1. | ||
Workers int | ||
} | ||
|
||
// Run ingests logs. | ||
func (lg IngestLogs) Run(ctx context.Context, client plogotlp.GRPCClient, tr TableReader) error { | ||
var ( | ||
workers = max(lg.Workers, 1) | ||
batcnCh = make(chan plog.Logs, workers) | ||
) | ||
grp, grpCtx := errgroup.WithContext(ctx) | ||
grp.Go(func() error { | ||
defer close(batcnCh) | ||
return Consume(tr, ConsumeOptions{ | ||
OnLogs: func(t *Logs) error { | ||
ctx := grpCtx | ||
|
||
batch := plog.NewLogs() | ||
t.ToOTLP(batch) | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case batcnCh <- batch: | ||
return nil | ||
} | ||
}, | ||
}) | ||
}) | ||
for range workers { | ||
grp.Go(func() error { | ||
ctx := grpCtx | ||
for batch := range batcnCh { | ||
if _, err := client.Export(ctx, plogotlp.NewExportRequestFromLogs(batch)); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
}) | ||
} | ||
return grp.Wait() | ||
} |
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