Skip to content

Commit

Permalink
Add support escape special characters in InfluxDB Line
Browse files Browse the repository at this point in the history
Signed-off-by: jadiunr <[email protected]>
  • Loading branch information
jadiunr authored and echlebek committed Jul 25, 2023
1 parent ef04da1 commit 04f1cf2
Show file tree
Hide file tree
Showing 3 changed files with 308 additions and 241 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG-6.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ Versioning](http://semver.org/spec/v2.0.0.html).
### Added
- Adding a flag at agent level to avoid collecting system.networks property in the agent entity state
- Added silences sorting by expiration to GraphQL service
- Added log-millisecond-timestamps backend configuration flag
- Added a session store, used to detect and prevent refresh token reuse
- Added support escape special characters in InfluxDB Line

### Changed
- Log handler error at error level instead of info level
- Users are now automatically logged out after a period of inactivity (12h)

## [6.9.2] - 2023-03-08

### Added
- Added GraphQL validator for query node depth

## [6.9.1] - 2022-12-01
Expand Down
56 changes: 36 additions & 20 deletions agent/transformers/influx_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type InfluxList []Influx

// Influx contains values of influx db line output metric format
type Influx struct {
Measurement string
TagSet []*v2.MetricTag
FieldSet []*Field
Timestamp int64
Measurement string
TagSet []*v2.MetricTag
FieldSet []*Field
Timestamp int64
}

// Transform transforms a metric in influx db line protocol to Sensu Metric
Expand All @@ -28,10 +28,10 @@ func (i InfluxList) Transform() []*v2.MetricPoint {
for _, influx := range i {
for _, fieldSet := range influx.FieldSet {
mp := &v2.MetricPoint{
Name: influx.Measurement + "." + fieldSet.Key,
Value: fieldSet.Value,
Timestamp: influx.Timestamp,
Tags: influx.TagSet,
Name: influx.Measurement + "." + fieldSet.Key,
Value: fieldSet.Value,
Timestamp: influx.Timestamp,
Tags: influx.TagSet,
}
points = append(points, mp)
}
Expand All @@ -43,8 +43,8 @@ func (i InfluxList) Transform() []*v2.MetricPoint {
func ParseInflux(event *v2.Event) InfluxList {
var influxList InfluxList
fields := logrus.Fields{
"namespace": event.Check.Namespace,
"check": event.Check.Name,
"namespace": event.Check.Namespace,
"check": event.Check.Name,
}

metric := strings.TrimSpace(event.Check.Output)
Expand All @@ -57,28 +57,28 @@ OUTER:
fields["line"] = l
l++
i := Influx{}
args := strings.Split(line, " ")
args := splitWithoutEscaped(line, " ")
if len(args) != 3 && len(args) != 2 {
logger.WithFields(fields).WithError(ErrMetricExtraction).Error("influxdb line format requires 2 arguments with a 3rd (optional) timestamp")
continue
}

measurementTag := strings.Split(args[0], ",")
i.Measurement = measurementTag[0]
measurementTag := splitWithoutEscaped(args[0], ",")
i.Measurement = unescape(measurementTag[0])
tagList := []*v2.MetricTag{}
if len(measurementTag) == 1 {
i.TagSet = tagList
} else {
for i, tagSet := range measurementTag {
if i != 0 {
ts := strings.Split(tagSet, "=")
ts := splitWithoutEscaped(tagSet, "=")
if len(ts) != 2 {
logger.WithFields(fields).WithError(ErrMetricExtraction).Error("metric tag set is invalid, must contain a key=value pair")
continue OUTER
}
tag := &v2.MetricTag{
Name: ts[0],
Value: ts[1],
Name: unescape(ts[0]),
Value: unescape(ts[1]),
}
tagList = append(tagList, tag)
}
Expand All @@ -90,10 +90,10 @@ OUTER:
i.TagSet = append(i.TagSet, event.Check.OutputMetricTags...)
}

fieldSets := strings.Split(args[1], ",")
fieldSets := splitWithoutEscaped(args[1], ",")
fieldList := []*Field{}
for _, fieldSet := range fieldSets {
fs := strings.Split(fieldSet, "=")
fs := splitWithoutEscaped(fieldSet, "=")
if len(fs) != 2 {
logger.WithFields(fields).WithError(ErrMetricExtraction).Error("metric field set is invalid, must contain a key=value pair")
continue OUTER
Expand All @@ -104,8 +104,8 @@ OUTER:
continue OUTER
}
field := &Field{
Key: fs[0],
Value: f,
Key: unescape(fs[0]),
Value: f,
}
fieldList = append(fieldList, field)
}
Expand Down Expand Up @@ -133,3 +133,19 @@ OUTER:

return influxList
}

func splitWithoutEscaped(s, sep string) []string {
s = strings.ReplaceAll(s, `\`+sep, "\x00")
segments := strings.Split(s, sep)
for i := range segments {
segments[i] = strings.ReplaceAll(segments[i], "\x00", sep)
}
return segments
}

func unescape(s string) string {
s = strings.ReplaceAll(s, `\\`, "\x00")
s = strings.ReplaceAll(s, `\`, "")
s = strings.ReplaceAll(s, "\x00", `\`)
return s
}
Loading

0 comments on commit 04f1cf2

Please sign in to comment.