Skip to content

Commit

Permalink
Merge pull request #28 from nicksinger/escape
Browse files Browse the repository at this point in the history
Add proper escaping to influx output
  • Loading branch information
perlpunk authored Jan 19, 2024
2 parents 56a273d + e985482 commit 4e21244
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions backlogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,28 +195,43 @@ def render_influxdb(data):
if status == "Resolved":
measure = "leadTime"
extra = ",leadTime={leadTime},cycleTime={cycleTime},leadTimeSum={leadTimeSum},cycleTimeSum={cycleTimeSum}".format(
leadTime=mean(times["leadTime"]) / 3600,
cycleTime=mean(times["cycleTime"]) / 3600,
leadTimeSum=sum(times["leadTime"]) / 3600,
cycleTimeSum=sum(times["cycleTime"]) / 3600,
leadTime=escape_telegraf_str(mean(times["leadTime"]) / 3600, "field value"),
cycleTime=escape_telegraf_str(mean(times["cycleTime"]) / 3600, "field value"),
leadTimeSum=escape_telegraf_str(sum(times["leadTime"]) / 3600, "field value"),
cycleTimeSum=escape_telegraf_str(sum(times["cycleTime"]) / 3600, "field value"),
)
else:
measure = "slo"
extra = ""
output.append(
'{measure},team="{team}",status="{status}",title="{title}" count={count}{extra}'.format(
measure=measure,
team="\\ ".join(data["team"].split(" ")),
status="\\ ".join(status.split(" ")),
title="\\ ".join(conf["title"].split(" ")),
count=count,
measure=escape_telegraf_str(measure, "measurement"),
team=escape_telegraf_str(data["team"], "tag value"),
status=escape_telegraf_str(status, "tag value"),
title=escape_telegraf_str(conf["title"], "tag value"),
count=escape_telegraf_str(count, "field value"),
extra=extra,
)
)
if status == "Resolved":
output[-1] += " " + str(_today_nanoseconds())
return output

def escape_telegraf_str(value_to_escape, element):
# See https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#special-characters for escaping rules and where they apply
escaped_str = str(value_to_escape) #especially for field values it can happen that we get an int
if (element == "field value"): #field values are the only thing where unique rules apply
escaped_str = escaped_str.replace("\\", "\\\\")
escaped_str = escaped_str.replace("\"", "\\\"")
return escaped_str

# common rules applicable to everything else
escaped_str = escaped_str.replace(",", "\\,")
escaped_str = escaped_str.replace(" ", "\\ ")
if (element != "measurement"):
escaped_str = escaped_str.replace("=", "\\=")
return escaped_str


if __name__ == "__main__":
parser = argparse.ArgumentParser()
Expand Down

0 comments on commit 4e21244

Please sign in to comment.