Skip to content

Commit

Permalink
[exporter/chronicleexporter]: Optimize getRawField when field is "bod…
Browse files Browse the repository at this point in the history
…y" (#1967)

* optimize getRawField when field is 'body'

* at switch for directly looking up known values

* check if string before returning
  • Loading branch information
jsirianni authored Nov 18, 2024
1 parent 7511da3 commit 8895796
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions exporter/chronicleexporter/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,44 @@ func (m *protoMarshaler) getIngestionLabels(logRecord plog.LogRecord) ([]*api.La
}

func (m *protoMarshaler) getRawField(ctx context.Context, field string, logRecord plog.LogRecord, scope plog.ScopeLogs, resource plog.ResourceLogs) (string, error) {
switch field {
case "body":
switch logRecord.Body().Type() {
case pcommon.ValueTypeStr:
return logRecord.Body().Str(), nil
case pcommon.ValueTypeMap:
bytes, err := json.Marshal(logRecord.Body().AsRaw())
if err != nil {
return "", fmt.Errorf("marshal log body: %w", err)
}
return string(bytes), nil
}
case logTypeField:
attributes := logRecord.Attributes().AsRaw()
if logType, ok := attributes["log_type"]; ok {
if v, ok := logType.(string); ok {
return v, nil
}
}
return "", nil
case chronicleLogTypeField:
attributes := logRecord.Attributes().AsRaw()
if logType, ok := attributes["chronicle_log_type"]; ok {
if v, ok := logType.(string); ok {
return v, nil
}
}
return "", nil
case chronicleNamespaceField:
attributes := logRecord.Attributes().AsRaw()
if namespace, ok := attributes["chronicle_namespace"]; ok {
if v, ok := namespace.(string); ok {
return v, nil
}
}
return "", nil
}

lrExpr, err := expr.NewOTTLLogRecordExpression(field, m.teleSettings)
if err != nil {
return "", fmt.Errorf("raw_log_field is invalid: %s", err)
Expand Down

0 comments on commit 8895796

Please sign in to comment.