2021-11-09 04:10:43 -06:00
|
|
|
package pipeline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LokiDataOutput can output raw data to Loki (as string value).
|
|
|
|
type LokiDataOutput struct {
|
|
|
|
lokiWriter *lokiWriter
|
|
|
|
}
|
|
|
|
|
2021-11-09 10:12:10 -06:00
|
|
|
func NewLokiDataOutput(endpoint string, basicAuth *BasicAuth) *LokiDataOutput {
|
2021-11-09 04:10:43 -06:00
|
|
|
return &LokiDataOutput{
|
2021-11-09 10:12:10 -06:00
|
|
|
lokiWriter: newLokiWriter(endpoint, basicAuth),
|
2021-11-09 04:10:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const DataOutputTypeLoki = "loki"
|
|
|
|
|
|
|
|
func (out *LokiDataOutput) Type() string {
|
|
|
|
return DataOutputTypeLoki
|
|
|
|
}
|
|
|
|
|
|
|
|
func (out *LokiDataOutput) OutputData(_ context.Context, vars Vars, data []byte) ([]*ChannelData, error) {
|
|
|
|
if out.lokiWriter.endpoint == "" {
|
|
|
|
logger.Debug("Skip sending to Loki: no url")
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
err := out.lokiWriter.write(LokiStream{
|
|
|
|
Stream: map[string]string{"channel": vars.Channel},
|
|
|
|
Values: []interface{}{
|
|
|
|
[]interface{}{time.Now().UnixNano(), string(data)},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return nil, err
|
|
|
|
}
|