-
Notifications
You must be signed in to change notification settings - Fork 431
Expand file tree
/
Copy pathtempo_cli.py
More file actions
executable file
·394 lines (357 loc) · 13.2 KB
/
Copy pathtempo_cli.py
File metadata and controls
executable file
·394 lines (357 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
196
197
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/env python3
"""
Grafana Tempo CLI - Command line utility for interacting with Grafana Tempo API
Usage:
tempo-cli [OPTIONS] COMMAND [ARGS]...
Environment Variables:
TEMPO_URL: Base URL for Tempo instance
TEMPO_API_KEY: API key for authentication
GRAFANA_DATASOURCE_UID: Optional Grafana datasource UID for proxying
"""
import json
import os
from typing import Dict, Optional
import typer
from rich.console import Console
from rich.json import JSON
from holmes.plugins.toolsets.grafana.common import GrafanaTempoConfig
from holmes.plugins.toolsets.grafana.grafana_tempo_api import (
GrafanaTempoAPI,
TempoAPIError,
)
app = typer.Typer(help="Grafana Tempo CLI - Query traces and metrics")
console = Console()
def get_api_client(
url: Optional[str] = None,
api_key: Optional[str] = None,
grafana_datasource_uid: Optional[str] = None,
) -> GrafanaTempoAPI:
"""Create and return a GrafanaTempoAPI client."""
# Get from environment if not provided
url = url or os.environ.get("TEMPO_URL")
api_key = api_key or os.environ.get("TEMPO_API_KEY")
grafana_datasource_uid = grafana_datasource_uid or os.environ.get(
"GRAFANA_DATASOURCE_UID"
)
if not url:
console.print(
"[red]Error: URL is required. Set TEMPO_URL environment variable or use --url flag[/red]"
)
raise typer.Exit(1)
config = GrafanaTempoConfig(
url=url, api_key=api_key, grafana_datasource_uid=grafana_datasource_uid
)
return GrafanaTempoAPI(config)
def print_result(result: Dict, pretty: bool = True):
"""Print API result in JSON format."""
if pretty:
console.print(JSON.from_data(result))
else:
print(json.dumps(result))
def handle_api_error(e: Exception):
"""Handle API errors with detailed messages."""
if isinstance(e, TempoAPIError):
console.print(f"[red]API Error {e.status_code}: {e}[/red]")
console.print(f"[dim]URL: {e.url}[/dim]")
else:
console.print(f"[red]Error: {e}[/red]")
raise typer.Exit(1)
@app.command()
def echo(
url: Optional[str] = typer.Option(None, "--url", "-u", help="Tempo base URL"),
api_key: Optional[str] = typer.Option(
None, "--api-key", "-k", help="API key for authentication"
),
grafana_datasource_uid: Optional[str] = typer.Option(
None, "--grafana-uid", "-g", help="Grafana datasource UID"
),
):
"""Check Tempo status using the echo endpoint."""
try:
client = get_api_client(url, api_key, grafana_datasource_uid)
result = client.query_echo_endpoint()
if result:
console.print("[green]✓ Tempo is responding[/green]")
else:
console.print("[red]✗ Tempo is not responding[/red]")
raise typer.Exit(1)
except Exception as e:
handle_api_error(e)
@app.command()
def trace(
trace_id: str = typer.Argument(..., help="Trace ID to retrieve"),
url: Optional[str] = typer.Option(None, "--url", "-u", help="Tempo base URL"),
api_key: Optional[str] = typer.Option(
None, "--api-key", "-k", help="API key for authentication"
),
grafana_datasource_uid: Optional[str] = typer.Option(
None, "--grafana-uid", "-g", help="Grafana datasource UID"
),
start: Optional[int] = typer.Option(
None, "--start", "-s", help="Start time in Unix epoch seconds"
),
end: Optional[int] = typer.Option(
None, "--end", "-e", help="End time in Unix epoch seconds"
),
pretty: bool = typer.Option(
True, "--pretty/--no-pretty", help="Pretty print JSON output"
),
):
"""Query a trace by its ID."""
try:
client = get_api_client(url, api_key, grafana_datasource_uid)
result = client.query_trace_by_id_v2(trace_id, start, end)
print_result(result, pretty)
except Exception as e:
handle_api_error(e)
@app.command("search-tags")
def search_tags(
tags: str = typer.Argument(..., help="logfmt-encoded span/process attributes"),
url: Optional[str] = typer.Option(None, "--url", "-u", help="Tempo base URL"),
api_key: Optional[str] = typer.Option(
None, "--api-key", "-k", help="API key for authentication"
),
grafana_datasource_uid: Optional[str] = typer.Option(
None, "--grafana-uid", "-g", help="Grafana datasource UID"
),
min_duration: Optional[str] = typer.Option(
None, "--min-duration", help="Minimum trace duration (e.g., '5s')"
),
max_duration: Optional[str] = typer.Option(
None, "--max-duration", help="Maximum trace duration"
),
limit: Optional[int] = typer.Option(
None, "--limit", "-l", help="Max number of traces to return"
),
start: Optional[int] = typer.Option(
None, "--start", "-s", help="Start time in Unix epoch seconds"
),
end: Optional[int] = typer.Option(
None, "--end", "-e", help="End time in Unix epoch seconds"
),
spss: Optional[int] = typer.Option(None, "--spss", help="Spans per span set"),
pretty: bool = typer.Option(
True, "--pretty/--no-pretty", help="Pretty print JSON output"
),
):
"""Search for traces using tag-based search."""
try:
client = get_api_client(url, api_key, grafana_datasource_uid)
result = client.search_traces_by_tags(
tags=tags,
min_duration=min_duration,
max_duration=max_duration,
limit=limit,
start=start,
end=end,
spss=spss,
)
print_result(result, pretty)
except Exception as e:
handle_api_error(e)
@app.command("search-query")
def search_query(
q: str = typer.Argument(..., help="TraceQL query"),
url: Optional[str] = typer.Option(None, "--url", "-u", help="Tempo base URL"),
api_key: Optional[str] = typer.Option(
None, "--api-key", "-k", help="API key for authentication"
),
grafana_datasource_uid: Optional[str] = typer.Option(
None, "--grafana-uid", "-g", help="Grafana datasource UID"
),
limit: Optional[int] = typer.Option(
None, "--limit", "-l", help="Max number of traces to return"
),
start: Optional[int] = typer.Option(
None, "--start", "-s", help="Start time in Unix epoch seconds"
),
end: Optional[int] = typer.Option(
None, "--end", "-e", help="End time in Unix epoch seconds"
),
spss: Optional[int] = typer.Option(None, "--spss", help="Spans per span set"),
pretty: bool = typer.Option(
True, "--pretty/--no-pretty", help="Pretty print JSON output"
),
):
"""Search for traces using TraceQL query."""
try:
client = get_api_client(url, api_key, grafana_datasource_uid)
result = client.search_traces_by_query(
q=q, limit=limit, start=start, end=end, spss=spss
)
print_result(result, pretty)
except Exception as e:
handle_api_error(e)
@app.command("tag-names")
def tag_names(
url: Optional[str] = typer.Option(None, "--url", "-u", help="Tempo base URL"),
api_key: Optional[str] = typer.Option(
None, "--api-key", "-k", help="API key for authentication"
),
grafana_datasource_uid: Optional[str] = typer.Option(
None, "--grafana-uid", "-g", help="Grafana datasource UID"
),
scope: Optional[str] = typer.Option(
None, "--scope", help="Scope filter: 'resource', 'span', or 'intrinsic'"
),
q: Optional[str] = typer.Option(
None, "--query", "-q", help="TraceQL query to filter tags"
),
start: Optional[int] = typer.Option(
None, "--start", "-s", help="Start time in Unix epoch seconds"
),
end: Optional[int] = typer.Option(
None, "--end", "-e", help="End time in Unix epoch seconds"
),
limit: Optional[int] = typer.Option(
None, "--limit", "-l", help="Max number of tag names"
),
max_stale_values: Optional[int] = typer.Option(
None, "--max-stale-values", help="Max stale values parameter"
),
pretty: bool = typer.Option(
True, "--pretty/--no-pretty", help="Pretty print JSON output"
),
):
"""Search for available tag names."""
try:
client = get_api_client(url, api_key, grafana_datasource_uid)
result = client.search_tag_names_v2(
scope=scope,
q=q,
start=start,
end=end,
limit=limit,
max_stale_values=max_stale_values,
)
print_result(result, pretty)
except Exception as e:
handle_api_error(e)
@app.command("tag-values")
def tag_values(
tag: str = typer.Argument(..., help="Tag name to get values for"),
url: Optional[str] = typer.Option(None, "--url", "-u", help="Tempo base URL"),
api_key: Optional[str] = typer.Option(
None, "--api-key", "-k", help="API key for authentication"
),
grafana_datasource_uid: Optional[str] = typer.Option(
None, "--grafana-uid", "-g", help="Grafana datasource UID"
),
q: Optional[str] = typer.Option(
None, "--query", "-q", help="TraceQL query to filter values"
),
start: Optional[int] = typer.Option(
None, "--start", "-s", help="Start time in Unix epoch seconds"
),
end: Optional[int] = typer.Option(
None, "--end", "-e", help="End time in Unix epoch seconds"
),
limit: Optional[int] = typer.Option(
None, "--limit", "-l", help="Max number of values"
),
max_stale_values: Optional[int] = typer.Option(
None, "--max-stale-values", help="Max stale values parameter"
),
pretty: bool = typer.Option(
True, "--pretty/--no-pretty", help="Pretty print JSON output"
),
):
"""Search for values of a specific tag."""
try:
client = get_api_client(url, api_key, grafana_datasource_uid)
result = client.search_tag_values_v2(
tag=tag,
q=q,
start=start,
end=end,
limit=limit,
max_stale_values=max_stale_values,
)
print_result(result, pretty)
except Exception as e:
handle_api_error(e)
@app.command("metrics-instant")
def metrics_instant(
q: str = typer.Argument(..., help="TraceQL metrics query"),
url: Optional[str] = typer.Option(None, "--url", "-u", help="Tempo base URL"),
api_key: Optional[str] = typer.Option(
None, "--api-key", "-k", help="API key for authentication"
),
grafana_datasource_uid: Optional[str] = typer.Option(
None, "--grafana-uid", "-g", help="Grafana datasource UID"
),
start: Optional[str] = typer.Option(
None, "--start", "-s", help="Start time (Unix seconds/nanoseconds/RFC3339)"
),
end: Optional[str] = typer.Option(
None, "--end", "-e", help="End time (Unix seconds/nanoseconds/RFC3339)"
),
since: Optional[str] = typer.Option(
None, "--since", help="Duration string (e.g., '1h')"
),
pretty: bool = typer.Option(
True, "--pretty/--no-pretty", help="Pretty print JSON output"
),
):
"""Query TraceQL metrics for an instant value."""
try:
client = get_api_client(url, api_key, grafana_datasource_uid)
result = client.query_metrics_instant(q=q, start=start, end=end, since=since)
print_result(result, pretty)
except Exception as e:
handle_api_error(e)
@app.command("metrics-range")
def metrics_range(
q: str = typer.Argument(..., help="TraceQL metrics query"),
url: Optional[str] = typer.Option(None, "--url", "-u", help="Tempo base URL"),
api_key: Optional[str] = typer.Option(
None, "--api-key", "-k", help="API key for authentication"
),
grafana_datasource_uid: Optional[str] = typer.Option(
None, "--grafana-uid", "-g", help="Grafana datasource UID"
),
step: Optional[str] = typer.Option(
None, "--step", help="Time series granularity (e.g., '1m', '5m')"
),
start: Optional[str] = typer.Option(
None, "--start", "-s", help="Start time (Unix seconds/nanoseconds/RFC3339)"
),
end: Optional[str] = typer.Option(
None, "--end", "-e", help="End time (Unix seconds/nanoseconds/RFC3339)"
),
since: Optional[str] = typer.Option(
None, "--since", help="Duration string (e.g., '3h')"
),
exemplars: Optional[int] = typer.Option(
None, "--exemplars", help="Maximum number of exemplars to return"
),
pretty: bool = typer.Option(
True, "--pretty/--no-pretty", help="Pretty print JSON output"
),
):
"""Query TraceQL metrics for a time series range."""
try:
client = get_api_client(url, api_key, grafana_datasource_uid)
result = client.query_metrics_range(
q=q, step=step, start=start, end=end, since=since, exemplars=exemplars
)
print_result(result, pretty)
except Exception as e:
handle_api_error(e)
@app.callback()
def main(
version: bool = typer.Option(False, "--version", "-v", help="Show version"),
):
"""
Grafana Tempo CLI - Command line utility for interacting with Grafana Tempo API
Set environment variables:
- TEMPO_URL: Base URL for Tempo instance
- TEMPO_API_KEY: API key for authentication
- GRAFANA_DATASOURCE_UID: Optional Grafana datasource UID
Or provide them as command options.
"""
if version:
console.print("Tempo CLI v1.0.0")
raise typer.Exit(0)
if __name__ == "__main__":
app()