You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/sources/reference/python-client-examples.md
+98-8Lines changed: 98 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ pip install httpx
25
25
26
26
## Authentication
27
27
28
-
The examples on this page connect to a local Loki instance without authentication. To use these examples with a multi-tenant or Grafana Cloud deployment, add the appropriate authentication as shown below.
28
+
The examples on this page connect to a local Loki instance without authentication. To use these examples with a multi-tenant or Grafana Cloud deployment, add the appropriate authentication as shown below. All functions defined on this page accept `headers`, `auth`, and `verify` parameters for this purpose.
You can find the **User** and **URL** values in the Loki logging service details of your [Grafana Cloud stack](https://grafana.com/docs/grafana-cloud/account-management/cloud-portal/#your-grafana-cloud-stack).
54
78
55
79
### Self-signed certificates
@@ -66,6 +90,18 @@ For production use, pass the path to your CA bundle instead:
[`GET /loki/api/v1/query_range`](../loki-http-api/#query-logs-within-a-range-of-time) queries logs over a time range. This is the most common query operation.
@@ -83,6 +119,9 @@ def query_range(
83
119
start: datetime,
84
120
end: datetime,
85
121
limit: int=1000,
122
+
headers: dict[str, str] |None=None,
123
+
auth: tuple[str, str] |None=None,
124
+
verify: bool|str=True, # False to skip TLS, or path to CA bundle
86
125
) -> list:
87
126
"""Query Loki for log entries within a time range."""
88
127
resp = requests.get(
@@ -94,6 +133,9 @@ def query_range(
94
133
"limit": limit,
95
134
"direction": "backward",
96
135
},
136
+
headers=headers,
137
+
auth=auth,
138
+
verify=verify,
97
139
)
98
140
resp.raise_for_status()
99
141
return resp.json()["data"]["result"]
@@ -125,6 +167,9 @@ def query_range(
125
167
start: datetime,
126
168
end: datetime,
127
169
limit: int=1000,
170
+
headers: dict[str, str] |None=None,
171
+
auth: tuple[str, str] |None=None,
172
+
verify: bool|str=True, # False to skip TLS, or path to CA bundle; httpx also accepts ssl.SSLContext
128
173
) -> list:
129
174
"""Query Loki for log entries within a time range."""
130
175
resp = httpx.get(
@@ -136,6 +181,9 @@ def query_range(
136
181
"limit": limit,
137
182
"direction": "backward",
138
183
},
184
+
headers=headers,
185
+
auth=auth,
186
+
verify=verify,
139
187
)
140
188
resp.raise_for_status()
141
189
return resp.json()["data"]["result"]
@@ -163,14 +211,23 @@ import requests
163
211
from datetime import datetime
164
212
165
213
166
-
defquery_instant(url: str, query: str) -> list:
214
+
defquery_instant(
215
+
url: str,
216
+
query: str,
217
+
headers: dict[str, str] |None=None,
218
+
auth: tuple[str, str] |None=None,
219
+
verify: bool|str=True, # False to skip TLS, or path to CA bundle
0 commit comments