Skip to content

Line Protocol

influxdb.line_protocol

Define the line_protocol handler.

Functions

quote_ident(value)

Indent the quotes.

Parameters:

Name Type Description Default
value str

the value to quote

required

Returns:

Name Type Description
str

the quoted identifier

Source code in influxdb/line_protocol.py
71
72
73
74
75
76
77
78
79
80
81
def quote_ident(value):
    """Indent the quotes.

    Args:
        value (str): the value to quote

    Returns:
        str: the quoted identifier

    """
    return '"{}"'.format(value.replace("\\", "\\\\").replace('"', '\\"').replace("\n", "\\n"))

quote_literal(value)

Quote provided literal.

Parameters:

Name Type Description Default
value str

the value to quote

required

Returns:

Name Type Description
str

the quoted literal

Source code in influxdb/line_protocol.py
84
85
86
87
88
89
90
91
92
93
94
def quote_literal(value):
    """Quote provided literal.

    Args:
        value (str): the value to quote

    Returns:
        str: the quoted literal

    """
    return "'{}'".format(value.replace("\\", "\\\\").replace("'", "\\'"))

make_line(measurement, tags=None, fields=None, time=None, precision=None)

Extract the actual point from a given measurement line.

Parameters:

Name Type Description Default
measurement str

the measurement name

required
tags dict

dictionary of tag key-value pairs

None
fields dict

dictionary of field key-value pairs

None
time

the timestamp for the point

None
precision str

time precision (n, u, ms, s, m, h)

None

Returns:

Name Type Description
str

the line protocol representation of the point

Source code in influxdb/line_protocol.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
def make_line(measurement, tags=None, fields=None, time=None, precision=None):
    """Extract the actual point from a given measurement line.

    Args:
        measurement (str): the measurement name
        tags (dict): dictionary of tag key-value pairs
        fields (dict): dictionary of field key-value pairs
        time: the timestamp for the point
        precision (str): time precision (n, u, ms, s, m, h)

    Returns:
        str: the line protocol representation of the point

    """
    tags = tags or {}
    fields = fields or {}

    line = _escape_tag(_get_unicode(measurement))

    # tags should be sorted client-side to take load off server
    tag_list = []
    for tag_key in sorted(tags.keys()):
        key = _escape_tag(tag_key)
        value = _escape_tag(tags[tag_key])

        if key != "" and value != "":
            tag_list.append("{key}={value}".format(key=key, value=value))

    if tag_list:
        line += "," + ",".join(tag_list)

    field_list = []
    for field_key in sorted(fields.keys()):
        key = _escape_tag(field_key)
        value = _escape_value(fields[field_key])

        if key != "" and value != "":
            field_list.append("{key}={value}".format(key=key, value=value))

    if field_list:
        line += " " + ",".join(field_list)

    if time is not None:
        timestamp = _get_unicode(str(int(_convert_timestamp(time, precision))))
        line += " " + timestamp

    return line

make_lines(data, precision=None)

Extract points from given dict.

Extracts the points from the given dict and returns a Unicode string matching the line protocol introduced in InfluxDB 0.9.0.

Parameters:

Name Type Description Default
data dict

dictionary containing 'points' key with list of point objects

required
precision str

time precision (n, u, ms, s, m, h)

None

Returns:

Name Type Description
str

line protocol formatted string

Source code in influxdb/line_protocol.py
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
def make_lines(data, precision=None):
    """Extract points from given dict.

    Extracts the points from the given dict and returns a Unicode string
    matching the line protocol introduced in InfluxDB 0.9.0.

    Args:
        data (dict): dictionary containing 'points' key with list of point objects
        precision (str): time precision (n, u, ms, s, m, h)

    Returns:
        str: line protocol formatted string

    """
    lines = []
    static_tags = data.get("tags")
    for point in data["points"]:
        if static_tags:
            tags = dict(static_tags)  # make a copy, since we'll modify
            tags.update(point.get("tags") or {})
        else:
            tags = point.get("tags") or {}

        line = make_line(
            point.get("measurement", data.get("measurement")),
            tags=tags,
            fields=point.get("fields"),
            precision=precision,
            time=point.get("time"),
        )
        lines.append(line)

    return "\n".join(lines) + "\n"