Skip to content

Instantly share code, notes, and snippets.

@nourspace
Forked from robskillington/prometheus.proto
Last active November 12, 2024 15:22
Show Gist options
  • Save nourspace/04264118a5c4c0bffe5a8724b486ab4d to your computer and use it in GitHub Desktop.
Save nourspace/04264118a5c4c0bffe5a8724b486ab4d to your computer and use it in GitHub Desktop.
Example Python Prometheus remote write client

Python Prometheus remote-write Client

Requirements

python -m pip install -r requirements.txt

Add metrics in write

Thanks to @robskillington for his Gist .

// Copyright 2016 Prometheus Team
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";
package prometheus;
option go_package = "prompb";
message WriteRequest {
repeated prometheus.TimeSeries timeseries = 1;
}
message ReadRequest {
repeated Query queries = 1;
}
message ReadResponse {
// In same order as the request's queries.
repeated QueryResult results = 1;
}
message Query {
int64 start_timestamp_ms = 1;
int64 end_timestamp_ms = 2;
repeated prometheus.LabelMatcher matchers = 3;
prometheus.ReadHints hints = 4;
}
message QueryResult {
// Samples within a time series must be ordered by time.
repeated prometheus.TimeSeries timeseries = 1;
}
message Sample {
double value = 1;
int64 timestamp = 2;
}
message TimeSeries {
repeated Label labels = 1;
repeated Sample samples = 2;
}
message Label {
string name = 1;
string value = 2;
}
message Labels {
repeated Label labels = 1;
}
// Matcher specifies a rule, which can match or set of labels or not.
message LabelMatcher {
enum Type {
EQ = 0;
NEQ = 1;
RE = 2;
NRE = 3;
}
Type type = 1;
string name = 2;
string value = 3;
}
message ReadHints {
int64 step_ms = 1; // Query step size in milliseconds.
string func = 2; // String representation of surrounding function or aggregation.
int64 start_ms = 3; // Start time in milliseconds.
int64 end_ms = 4; // End time in milliseconds.
}
# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: prometheus.proto
import sys
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
DESCRIPTOR = _descriptor.FileDescriptor(
name='prometheus.proto',
package='prometheus',
syntax='proto3',
serialized_options=_b('Z\006prompb'),
serialized_pb=_b('\n\x10prometheus.proto\x12\nprometheus\":\n\x0cWriteRequest\x12*\n\ntimeseries\x18\x01 \x03(\x0b\x32\x16.prometheus.TimeSeries\"1\n\x0bReadRequest\x12\"\n\x07queries\x18\x01 \x03(\x0b\x32\x11.prometheus.Query\"8\n\x0cReadResponse\x12(\n\x07results\x18\x01 \x03(\x0b\x32\x17.prometheus.QueryResult\"\x8f\x01\n\x05Query\x12\x1a\n\x12start_timestamp_ms\x18\x01 \x01(\x03\x12\x18\n\x10\x65nd_timestamp_ms\x18\x02 \x01(\x03\x12*\n\x08matchers\x18\x03 \x03(\x0b\x32\x18.prometheus.LabelMatcher\x12$\n\x05hints\x18\x04 \x01(\x0b\x32\x15.prometheus.ReadHints\"9\n\x0bQueryResult\x12*\n\ntimeseries\x18\x01 \x03(\x0b\x32\x16.prometheus.TimeSeries\"*\n\x06Sample\x12\r\n\x05value\x18\x01 \x01(\x01\x12\x11\n\ttimestamp\x18\x02 \x01(\x03\"T\n\nTimeSeries\x12!\n\x06labels\x18\x01 \x03(\x0b\x32\x11.prometheus.Label\x12#\n\x07samples\x18\x02 \x03(\x0b\x32\x12.prometheus.Sample\"$\n\x05Label\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t\"+\n\x06Labels\x12!\n\x06labels\x18\x01 \x03(\x0b\x32\x11.prometheus.Label\"\x82\x01\n\x0cLabelMatcher\x12+\n\x04type\x18\x01 \x01(\x0e\x32\x1d.prometheus.LabelMatcher.Type\x12\x0c\n\x04name\x18\x02 \x01(\t\x12\r\n\x05value\x18\x03 \x01(\t\"(\n\x04Type\x12\x06\n\x02\x45Q\x10\x00\x12\x07\n\x03NEQ\x10\x01\x12\x06\n\x02RE\x10\x02\x12\x07\n\x03NRE\x10\x03\"L\n\tReadHints\x12\x0f\n\x07step_ms\x18\x01 \x01(\x03\x12\x0c\n\x04\x66unc\x18\x02 \x01(\t\x12\x10\n\x08start_ms\x18\x03 \x01(\x03\x12\x0e\n\x06\x65nd_ms\x18\x04 \x01(\x03\x42\x08Z\x06prompbb\x06proto3')
)
_LABELMATCHER_TYPE = _descriptor.EnumDescriptor(
name='Type',
full_name='prometheus.LabelMatcher.Type',
filename=None,
file=DESCRIPTOR,
values=[
_descriptor.EnumValueDescriptor(
name='EQ', index=0, number=0,
serialized_options=None,
type=None),
_descriptor.EnumValueDescriptor(
name='NEQ', index=1, number=1,
serialized_options=None,
type=None),
_descriptor.EnumValueDescriptor(
name='RE', index=2, number=2,
serialized_options=None,
type=None),
_descriptor.EnumValueDescriptor(
name='NRE', index=3, number=3,
serialized_options=None,
type=None),
],
containing_type=None,
serialized_options=None,
serialized_start=710,
serialized_end=750,
)
_sym_db.RegisterEnumDescriptor(_LABELMATCHER_TYPE)
_WRITEREQUEST = _descriptor.Descriptor(
name='WriteRequest',
full_name='prometheus.WriteRequest',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='timeseries', full_name='prometheus.WriteRequest.timeseries', index=0,
number=1, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=32,
serialized_end=90,
)
_READREQUEST = _descriptor.Descriptor(
name='ReadRequest',
full_name='prometheus.ReadRequest',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='queries', full_name='prometheus.ReadRequest.queries', index=0,
number=1, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=92,
serialized_end=141,
)
_READRESPONSE = _descriptor.Descriptor(
name='ReadResponse',
full_name='prometheus.ReadResponse',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='results', full_name='prometheus.ReadResponse.results', index=0,
number=1, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=143,
serialized_end=199,
)
_QUERY = _descriptor.Descriptor(
name='Query',
full_name='prometheus.Query',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='start_timestamp_ms', full_name='prometheus.Query.start_timestamp_ms', index=0,
number=1, type=3, cpp_type=2, label=1,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
_descriptor.FieldDescriptor(
name='end_timestamp_ms', full_name='prometheus.Query.end_timestamp_ms', index=1,
number=2, type=3, cpp_type=2, label=1,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
_descriptor.FieldDescriptor(
name='matchers', full_name='prometheus.Query.matchers', index=2,
number=3, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
_descriptor.FieldDescriptor(
name='hints', full_name='prometheus.Query.hints', index=3,
number=4, type=11, cpp_type=10, label=1,
has_default_value=False, default_value=None,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=202,
serialized_end=345,
)
_QUERYRESULT = _descriptor.Descriptor(
name='QueryResult',
full_name='prometheus.QueryResult',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='timeseries', full_name='prometheus.QueryResult.timeseries', index=0,
number=1, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=347,
serialized_end=404,
)
_SAMPLE = _descriptor.Descriptor(
name='Sample',
full_name='prometheus.Sample',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='value', full_name='prometheus.Sample.value', index=0,
number=1, type=1, cpp_type=5, label=1,
has_default_value=False, default_value=float(0),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
_descriptor.FieldDescriptor(
name='timestamp', full_name='prometheus.Sample.timestamp', index=1,
number=2, type=3, cpp_type=2, label=1,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=406,
serialized_end=448,
)
_TIMESERIES = _descriptor.Descriptor(
name='TimeSeries',
full_name='prometheus.TimeSeries',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='labels', full_name='prometheus.TimeSeries.labels', index=0,
number=1, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
_descriptor.FieldDescriptor(
name='samples', full_name='prometheus.TimeSeries.samples', index=1,
number=2, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=450,
serialized_end=534,
)
_LABEL = _descriptor.Descriptor(
name='Label',
full_name='prometheus.Label',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='name', full_name='prometheus.Label.name', index=0,
number=1, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
_descriptor.FieldDescriptor(
name='value', full_name='prometheus.Label.value', index=1,
number=2, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=536,
serialized_end=572,
)
_LABELS = _descriptor.Descriptor(
name='Labels',
full_name='prometheus.Labels',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='labels', full_name='prometheus.Labels.labels', index=0,
number=1, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=574,
serialized_end=617,
)
_LABELMATCHER = _descriptor.Descriptor(
name='LabelMatcher',
full_name='prometheus.LabelMatcher',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='type', full_name='prometheus.LabelMatcher.type', index=0,
number=1, type=14, cpp_type=8, label=1,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
_descriptor.FieldDescriptor(
name='name', full_name='prometheus.LabelMatcher.name', index=1,
number=2, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
_descriptor.FieldDescriptor(
name='value', full_name='prometheus.LabelMatcher.value', index=2,
number=3, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
],
extensions=[
],
nested_types=[],
enum_types=[
_LABELMATCHER_TYPE,
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=620,
serialized_end=750,
)
_READHINTS = _descriptor.Descriptor(
name='ReadHints',
full_name='prometheus.ReadHints',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='step_ms', full_name='prometheus.ReadHints.step_ms', index=0,
number=1, type=3, cpp_type=2, label=1,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
_descriptor.FieldDescriptor(
name='func', full_name='prometheus.ReadHints.func', index=1,
number=2, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
_descriptor.FieldDescriptor(
name='start_ms', full_name='prometheus.ReadHints.start_ms', index=2,
number=3, type=3, cpp_type=2, label=1,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
_descriptor.FieldDescriptor(
name='end_ms', full_name='prometheus.ReadHints.end_ms', index=3,
number=4, type=3, cpp_type=2, label=1,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=752,
serialized_end=828,
)
_WRITEREQUEST.fields_by_name['timeseries'].message_type = _TIMESERIES
_READREQUEST.fields_by_name['queries'].message_type = _QUERY
_READRESPONSE.fields_by_name['results'].message_type = _QUERYRESULT
_QUERY.fields_by_name['matchers'].message_type = _LABELMATCHER
_QUERY.fields_by_name['hints'].message_type = _READHINTS
_QUERYRESULT.fields_by_name['timeseries'].message_type = _TIMESERIES
_TIMESERIES.fields_by_name['labels'].message_type = _LABEL
_TIMESERIES.fields_by_name['samples'].message_type = _SAMPLE
_LABELS.fields_by_name['labels'].message_type = _LABEL
_LABELMATCHER.fields_by_name['type'].enum_type = _LABELMATCHER_TYPE
_LABELMATCHER_TYPE.containing_type = _LABELMATCHER
DESCRIPTOR.message_types_by_name['WriteRequest'] = _WRITEREQUEST
DESCRIPTOR.message_types_by_name['ReadRequest'] = _READREQUEST
DESCRIPTOR.message_types_by_name['ReadResponse'] = _READRESPONSE
DESCRIPTOR.message_types_by_name['Query'] = _QUERY
DESCRIPTOR.message_types_by_name['QueryResult'] = _QUERYRESULT
DESCRIPTOR.message_types_by_name['Sample'] = _SAMPLE
DESCRIPTOR.message_types_by_name['TimeSeries'] = _TIMESERIES
DESCRIPTOR.message_types_by_name['Label'] = _LABEL
DESCRIPTOR.message_types_by_name['Labels'] = _LABELS
DESCRIPTOR.message_types_by_name['LabelMatcher'] = _LABELMATCHER
DESCRIPTOR.message_types_by_name['ReadHints'] = _READHINTS
_sym_db.RegisterFileDescriptor(DESCRIPTOR)
WriteRequest = _reflection.GeneratedProtocolMessageType('WriteRequest', (_message.Message,), dict(
DESCRIPTOR = _WRITEREQUEST,
__module__ = 'prometheus_pb2'
# @@protoc_insertion_point(class_scope:prometheus.WriteRequest)
))
_sym_db.RegisterMessage(WriteRequest)
ReadRequest = _reflection.GeneratedProtocolMessageType('ReadRequest', (_message.Message,), dict(
DESCRIPTOR = _READREQUEST,
__module__ = 'prometheus_pb2'
# @@protoc_insertion_point(class_scope:prometheus.ReadRequest)
))
_sym_db.RegisterMessage(ReadRequest)
ReadResponse = _reflection.GeneratedProtocolMessageType('ReadResponse', (_message.Message,), dict(
DESCRIPTOR = _READRESPONSE,
__module__ = 'prometheus_pb2'
# @@protoc_insertion_point(class_scope:prometheus.ReadResponse)
))
_sym_db.RegisterMessage(ReadResponse)
Query = _reflection.GeneratedProtocolMessageType('Query', (_message.Message,), dict(
DESCRIPTOR = _QUERY,
__module__ = 'prometheus_pb2'
# @@protoc_insertion_point(class_scope:prometheus.Query)
))
_sym_db.RegisterMessage(Query)
QueryResult = _reflection.GeneratedProtocolMessageType('QueryResult', (_message.Message,), dict(
DESCRIPTOR = _QUERYRESULT,
__module__ = 'prometheus_pb2'
# @@protoc_insertion_point(class_scope:prometheus.QueryResult)
))
_sym_db.RegisterMessage(QueryResult)
Sample = _reflection.GeneratedProtocolMessageType('Sample', (_message.Message,), dict(
DESCRIPTOR = _SAMPLE,
__module__ = 'prometheus_pb2'
# @@protoc_insertion_point(class_scope:prometheus.Sample)
))
_sym_db.RegisterMessage(Sample)
TimeSeries = _reflection.GeneratedProtocolMessageType('TimeSeries', (_message.Message,), dict(
DESCRIPTOR = _TIMESERIES,
__module__ = 'prometheus_pb2'
# @@protoc_insertion_point(class_scope:prometheus.TimeSeries)
))
_sym_db.RegisterMessage(TimeSeries)
Label = _reflection.GeneratedProtocolMessageType('Label', (_message.Message,), dict(
DESCRIPTOR = _LABEL,
__module__ = 'prometheus_pb2'
# @@protoc_insertion_point(class_scope:prometheus.Label)
))
_sym_db.RegisterMessage(Label)
Labels = _reflection.GeneratedProtocolMessageType('Labels', (_message.Message,), dict(
DESCRIPTOR = _LABELS,
__module__ = 'prometheus_pb2'
# @@protoc_insertion_point(class_scope:prometheus.Labels)
))
_sym_db.RegisterMessage(Labels)
LabelMatcher = _reflection.GeneratedProtocolMessageType('LabelMatcher', (_message.Message,), dict(
DESCRIPTOR = _LABELMATCHER,
__module__ = 'prometheus_pb2'
# @@protoc_insertion_point(class_scope:prometheus.LabelMatcher)
))
_sym_db.RegisterMessage(LabelMatcher)
ReadHints = _reflection.GeneratedProtocolMessageType('ReadHints', (_message.Message,), dict(
DESCRIPTOR = _READHINTS,
__module__ = 'prometheus_pb2'
# @@protoc_insertion_point(class_scope:prometheus.ReadHints)
))
_sym_db.RegisterMessage(ReadHints)
DESCRIPTOR._options = None
# @@protoc_insertion_point(module_scope)
protobuf==3.20.0
python-snappy
requests
import calendar
from datetime import datetime
import requests
import snappy
from prometheus_pb2 import WriteRequest
def dt2ts(dt):
"""Converts a datetime object to UTC timestamp
naive datetime will be considered UTC.
"""
return calendar.timegm(dt.utctimetuple())
def send(write_request, url="http://localhost:9090/api/v1/write"):
uncompressed = write_request.SerializeToString()
compressed = snappy.compress(uncompressed)
headers = {
"Content-Encoding": "snappy",
"Content-Type": "application/x-protobuf",
"X-Prometheus-Remote-Write-Version": "0.1.0",
"User-Agent": "metrics-worker"
}
try:
response = requests.post(url, headers=headers, data=compressed)
print(response.status_code, response.text)
except Exception as e:
print(e)
def add_metric(write_request, name: str, labels: dict, value: float, timestamp: int = None):
series = write_request.timeseries.add()
# name label always required
label = series.labels.add()
label.name = "__name__"
label.value = name
for label_name, label_value in labels.items():
label = series.labels.add()
label.name = label_name
label.value = label_value
sample = series.samples.add()
sample.value = value
sample.timestamp = timestamp or (dt2ts(datetime.utcnow()) * 1000)
def write():
write_request = WriteRequest()
# Create as many of these
add_metric(write_request, "my_metric", {"label1": "value1", "label2": "value2"}, 18)
add_metric(write_request, "my_metric", {"label1": "value3", "label2": "value4"}, 18, 1677713906)
# Send to remote write endpoint
send(write_request)
write()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment