-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING: loki label 'source' => 'logpush_source'
- Loading branch information
Showing
9 changed files
with
525 additions
and
469 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
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,103 @@ | ||
package loki | ||
|
||
import ( | ||
"maps" | ||
"strings" | ||
|
||
"github.com/google/uuid" | ||
"github.com/maddsua/logpush/service/dbops" | ||
"github.com/maddsua/logpush/service/ingester/streams" | ||
"github.com/maddsua/logpush/service/logdata" | ||
) | ||
|
||
func webStreamToLabeled(stream *streams.WebStream, streamSource *dbops.Stream, txID uuid.UUID) []LokiStream { | ||
|
||
baseLabels := map[string]string{ | ||
"logpush_source": "web", | ||
"service_name": streamSource.Name, | ||
"logpush_tx": txID.String(), | ||
} | ||
|
||
logdata.MergeStreamLabels(streamSource, baseLabels) | ||
logdata.CopyMetaFields(baseLabels, stream.Meta) | ||
|
||
var result []LokiStream | ||
|
||
for idx, entry := range stream.Entries { | ||
|
||
if entry.Message = strings.TrimSpace(entry.Message); entry.Message == "" { | ||
continue | ||
} | ||
|
||
labels := map[string]string{} | ||
maps.Copy(labels, baseLabels) | ||
logdata.CopyMetaFields(labels, entry.Meta) | ||
labels["detected_level"] = entry.Level.String() | ||
|
||
result = append(result, LokiStream{ | ||
Stream: labels, | ||
Values: [][]any{ | ||
{ | ||
entry.Date.String(idx), | ||
entry.Message, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func webStreamToStructured(stream *streams.WebStream, streamSource *dbops.Stream, txID uuid.UUID) LokiStream { | ||
|
||
labels := map[string]string{ | ||
"logpush_source": "web", | ||
"service_name": streamSource.Name, | ||
"logpush_tx": txID.String(), | ||
} | ||
|
||
logdata.MergeStreamLabels(streamSource, labels) | ||
|
||
metaFields := map[string]string{} | ||
|
||
for key, val := range stream.Meta { | ||
|
||
if _, has := labels[key]; has { | ||
continue | ||
} | ||
|
||
switch key { | ||
case "env", "environment": | ||
labels["env"] = val | ||
default: | ||
metaFields[key] = val | ||
} | ||
} | ||
|
||
var streamValues [][]any | ||
for idx, entry := range stream.Entries { | ||
|
||
if entry.Message = strings.TrimSpace(entry.Message); entry.Message == "" { | ||
continue | ||
} | ||
|
||
meta := map[string]string{} | ||
maps.Copy(meta, metaFields) | ||
meta["detected_level"] = entry.Level.String() | ||
|
||
if entry.Meta != nil { | ||
maps.Copy(meta, entry.Meta) | ||
} | ||
|
||
streamValues = append(streamValues, []any{ | ||
entry.Date.String(idx), | ||
entry.Message, | ||
meta, | ||
}) | ||
} | ||
|
||
return LokiStream{ | ||
Stream: labels, | ||
Values: streamValues, | ||
} | ||
} |
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 timescale | ||
|
||
import ( | ||
"database/sql" | ||
"encoding/json" | ||
"maps" | ||
"strings" | ||
|
||
"github.com/google/uuid" | ||
"github.com/maddsua/logpush/service/dbops" | ||
"github.com/maddsua/logpush/service/ingester/streams" | ||
"github.com/maddsua/logpush/service/logdata" | ||
) | ||
|
||
func webStreamToRows(batch *streams.WebStream, streamID uuid.UUID, txID uuid.UUID) []dbops.InsertStreamEntryParams { | ||
|
||
var result []dbops.InsertStreamEntryParams | ||
|
||
var mergeMeta = func(entry streams.WebLogEntry) map[string]string { | ||
|
||
metadata := map[string]string{} | ||
|
||
maps.Copy(metadata, batch.Meta) | ||
logdata.CopyMetaFields(metadata, entry.Meta) | ||
|
||
if len(metadata) == 0 { | ||
return nil | ||
} | ||
|
||
return metadata | ||
} | ||
|
||
for idx, entry := range batch.Entries { | ||
|
||
if entry.Message = strings.TrimSpace(entry.Message); entry.Message == "" { | ||
continue | ||
} | ||
|
||
var metadata sql.Null[[]byte] | ||
if meta := mergeMeta(entry); meta != nil { | ||
metadata.V, _ = json.Marshal(meta) | ||
metadata.Valid = true | ||
} | ||
|
||
result = append(result, dbops.InsertStreamEntryParams{ | ||
CreatedAt: entry.Date.Time(idx), | ||
StreamID: streamID, | ||
TxID: uuid.NullUUID{UUID: txID, Valid: true}, | ||
Level: entry.Level.String(), | ||
Message: entry.Message, | ||
Metadata: metadata, | ||
}) | ||
} | ||
|
||
return result | ||
} |
Oops, something went wrong.