coralogix-cli 0.1.26

Coralogix CLI - the observability backbone for AI agents and engineering teams.
Documentation
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! Shared fan-out / fan-in execution layer.
//!
//! This module provides the primitives for running the same command against
//! multiple named profiles concurrently and then combining their results
//! before a single render pass.  The same pattern extends naturally to
//! multi-region fan-out in the future by adding additional target dimensions.

use std::future::Future;
use std::sync::Arc;
use std::time::Duration;

use anyhow::{bail, Result};
use colored::Colorize;
use futures::future::join_all;
use tokio::sync::OnceCell;

use crate::api_client::CxClient;
use crate::config::ResolvedConfig;
use crate::identity;

// ── Execution target ──────────────────────────────────────────────────────────

/// A fully resolved, ready-to-use execution context for one profile.
///
/// Owns the resolved config *and* the pre-built HTTP client so that
/// downstream code never needs to construct a `CxClient` manually.
pub struct ExecutionTarget {
    pub profile_name: String,
    pub cfg: ResolvedConfig,
    pub client: CxClient,
    /// Lazily-resolved console base URL, cached in-process so repeated
    /// console-link lookups within one command invocation don't re-hit
    /// `/identity/whoami`. Backed by a longer-lived on-disk cache in the
    /// profile file (see `config::load_cached_console_url`) so that most
    /// invocations skip the network lookup entirely.
    console_base: OnceCell<Option<String>>,
    /// When true, [`console_base`](Self::console_base) always returns `None`,
    /// suppressing every "View in Coralogix" stderr link for this target. Set
    /// from the `--no-console-link` flag / `CX_NO_CONSOLE_LINK` env var /
    /// `no_console_link` config key via [`build_targets`]. Defaults to
    /// `false` here so direct `new()` callers (tests) are unaffected.
    no_console_link: bool,
}

impl ExecutionTarget {
    /// Build an `ExecutionTarget` from an already-resolved config.
    pub fn new(cfg: ResolvedConfig) -> Result<Self> {
        Self::with_timeout(cfg, None)
    }

    /// Build an `ExecutionTarget` with an optional per-request deadline.
    pub fn with_timeout(cfg: ResolvedConfig, timeout: Option<Duration>) -> Result<Self> {
        let client = CxClient::with_timeout(&cfg.endpoint, &cfg.api_key, timeout)?;
        let profile_name = cfg.profile_name.clone();
        Ok(Self {
            profile_name,
            cfg,
            client,
            console_base: OnceCell::new(),
            no_console_link: false,
        })
    }

    /// Resolve the base URL to build "View in Coralogix" console links
    /// against.
    ///
    /// Resolution order:
    /// 1. An explicit `console_url` configured on the profile TOML - used
    ///    as-is (trailing slash already trimmed by `config::resolve_single`).
    ///    Not available to env-only invocations (`CX_API_KEY` + `CX_REGION`
    ///    with no profile file) - there is no `CX_CONSOLE_URL` equivalent, so
    ///    those callers always fall through to steps 2-4.
    /// 2. A fresh on-disk cache of the team's console URL in the profile file
    ///    (see `config::load_cached_console_url`), written by a previous
    ///    invocation. This is what spares the overwhelming majority of
    ///    invocations - and, crucially, agents making many sequential calls -
    ///    the extra `/identity/whoami` round-trip.
    /// 3. Otherwise, the team's console URL resolved automatically via
    ///    `GET /identity/whoami` (see `identity::lookup_whoami`), then
    ///    persisted to the profile cache for next time. This is the default on
    ///    a cold cache: most teams don't need to configure anything at all to
    ///    get console links.
    /// 4. `None` if `/identity/whoami` fails or returns no usable URL.
    ///
    /// When `cfg.credentials_overridden` is set (the bearer token came from a
    /// `--api-key`/`CX_API_KEY` override rather than this profile's own
    /// credentials), steps 1-2 are skipped and step 3's cache write is
    /// skipped: the profile's `console_url` and cached URL belong to a
    /// different team than the one actually in use, and must never be read
    /// or clobbered by this run. Resolution always falls through to a live
    /// `/identity/whoami` call in that case.
    ///
    /// Best-effort and infallible: any lookup (or cache write) failure results
    /// in `None` rather than an error, since a console link is a "nice to
    /// have" that must never fail an otherwise-successful command. Cached per
    /// target so multiple links printed within one invocation only resolve
    /// once.
    ///
    /// Short-circuits to `None` unconditionally when `no_console_link` is
    /// set, before any of the steps above - so `--no-console-link` /
    /// `CX_NO_CONSOLE_LINK` / `no_console_link` in config also skip the
    /// `/identity/whoami` round-trip on a cold cache, not just the printing.
    pub async fn console_base(&self) -> Option<String> {
        if self.no_console_link {
            return None;
        }
        self.console_base
            .get_or_init(|| async {
                if !self.cfg.credentials_overridden {
                    // 1. Explicit override always wins.
                    if let Some(url) = &self.cfg.console_url {
                        return Some(url.clone());
                    }
                    // 2. Fresh on-disk cache - no network.
                    if let Some(url) = crate::config::load_cached_console_url(&self.profile_name) {
                        return Some(url);
                    }
                }
                // 3. Cold cache (or overridden credentials): resolve live.
                let whoami = identity::lookup_whoami(&self.client).await;
                let resolved = whoami.as_ref().and_then(|w| {
                    let url = w.team_url.as_deref()?.trim_end_matches('/');
                    if url.is_empty() {
                        None
                    } else {
                        Some(url.to_string())
                    }
                });
                if !self.cfg.credentials_overridden {
                    if let Some(w) = &whoami {
                        crate::config::cache_team_identity(&self.profile_name, w);
                    }
                }
                resolved
            })
            .await
            .clone()
    }

    /// Resolve this target's console base URL, build a "View in Coralogix"
    /// link with `build`, print it to stderr, and return it.
    ///
    /// This is the single place that ties `console_base` resolution to
    /// printing: every command that prints a console link should go through
    /// this method (or [`console_link_for_profile`] when working from a
    /// `(profile_name, T)` pair rather than an `ExecutionTarget` directly)
    /// instead of re-deriving the base/build/print sequence inline, so
    /// there's exactly one place that can forget to print.
    ///
    /// Returns `None` (silently - a console link is always best-effort) if no
    /// console base URL could be resolved for this target.
    pub async fn emit_console_link(&self, build: impl FnOnce(&str) -> String) -> Option<String> {
        let base = self.console_base().await?;
        let url = build(&base);
        crate::render::print_console_link(&url);
        Some(url)
    }
}

/// Look up `profile`'s target in `targets`, then build+print+return its
/// console link via [`ExecutionTarget::emit_console_link`].
///
/// Collapses the `find_target` -> `console_base` -> build URL -> print ->
/// return idiom that used to be repeated inline (or reimplemented behind
/// bespoke per-command wrapper functions like `print_dashboard_console_link`)
/// at every call site across command modules that print a "View in
/// Coralogix" link after a fan-out. Callers only need to supply the
/// `console_url::*` builder for their entity:
///
/// ```ignore
/// execution::console_link_for_profile(targets, &profile, |b| {
///     console_url::alert_url(b, &id)
/// })
/// .await;
/// ```
///
/// Returns `None` if `profile` has no matching target, or if no console base
/// URL could be resolved for it.
pub async fn emit_console_link_for_profile(
    targets: &[Arc<ExecutionTarget>],
    profile: &str,
    build: impl FnOnce(&str) -> String,
) -> Option<String> {
    find_target(targets, profile)?
        .emit_console_link(build)
        .await
}

/// Build a list of `ExecutionTarget`s from a list of resolved configs.
///
/// `no_console_link` is applied to every target so all "View in Coralogix"
/// links (see [`ExecutionTarget::console_base`]) are suppressed uniformly
/// across a fan-out.
pub fn build_targets(
    configs: Vec<ResolvedConfig>,
    no_console_link: bool,
    timeout: Option<Duration>,
) -> Result<Vec<Arc<ExecutionTarget>>> {
    configs
        .into_iter()
        .map(|cfg| {
            let mut target = ExecutionTarget::with_timeout(cfg, timeout)?;
            target.no_console_link = no_console_link;
            Ok(Arc::new(target))
        })
        .collect()
}

/// Find the target whose `profile_name` matches `profile`.
///
/// Used after fan-out/fan-in to look a target back up by the profile name
/// carried in the `(profile_name, T)` result pairs - e.g. to resolve a
/// per-profile console link after printing a success line.
pub fn find_target<'a>(
    targets: &'a [Arc<ExecutionTarget>],
    profile: &str,
) -> Option<&'a Arc<ExecutionTarget>> {
    targets.iter().find(|t| t.profile_name == profile)
}

// ── Fan-out ───────────────────────────────────────────────────────────────────

/// Run `f` concurrently for every target and collect the results.
///
/// Returns a `Vec` of `(profile_name, Result<T>)` in completion order.
/// Errors are preserved per-profile so the caller can report failures
/// while still rendering results from the profiles that succeeded.
pub async fn fan_out<T, F, Fut>(targets: &[Arc<ExecutionTarget>], f: F) -> Vec<(String, Result<T>)>
where
    F: Fn(Arc<ExecutionTarget>) -> Fut,
    Fut: Future<Output = Result<T>>,
{
    let futures = targets.iter().map(|t| {
        let profile_name = t.profile_name.clone();
        let fut = f(Arc::clone(t));
        async move { (profile_name, fut.await) }
    });
    join_all(futures).await
}

// ── Error collection ──────────────────────────────────────────────────────────

/// Drain per-profile fan-out results: print every failure to stderr in the
/// standard red format, and return the successful `(profile, value)` pairs in
/// input order.
///
/// Bails when there was at least one target and *all* of them failed, so a
/// total failure surfaces as an error instead of rendering as a silently
/// empty result (FORGE-482). Partial failure (some targets ok) returns `Ok`
/// with the survivors - behavior per docs/multi-profile.md is unchanged.
pub fn report_errors_and_collect_successes<T>(
    per_profile: Vec<(String, Result<T>)>,
) -> Result<Vec<(String, T)>> {
    let target_count = per_profile.len();
    let mut successes = Vec::with_capacity(target_count);
    let mut errors: Vec<(String, anyhow::Error)> = Vec::new();
    for (profile, result) in per_profile {
        match result {
            Ok(v) => successes.push((profile, v)),
            Err(e) => errors.push((profile, e)),
        }
    }

    // Total failure: surface as an error instead of a silently empty result
    // (FORGE-482).
    if successes.is_empty() && target_count > 0 {
        // With a single target there is nothing to aggregate - propagate its
        // actual error so the user sees the real cause, not a generic
        // "all profiles failed" line stacked on top of the printed error.
        if let [_] = errors.as_slice() {
            let (profile, e) = errors.into_iter().next().expect("one error present");
            return Err(e.context(format!("profile '{profile}' failed")));
        }
        for (profile, e) in &errors {
            eprintln!("{}", format!("error from profile '{profile}': {e:#}").red());
        }
        bail!("all {target_count} profiles returned errors");
    }

    // Partial failure: report the failed profiles and return the survivors,
    // per docs/multi-profile.md.
    for (profile, e) in errors {
        eprintln!("{}", format!("error from profile '{profile}': {e:#}").red());
    }
    Ok(successes)
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    fn test_cfg(endpoint: &str, console_url: Option<&str>) -> ResolvedConfig {
        ResolvedConfig {
            profile_name: "test-profile".to_string(),
            api_key: "test-key".to_string(),
            auth_kind: crate::config::AuthKind::ApiKey,
            endpoint: endpoint.to_string(),
            default_tier: crate::Tier::Archive,
            console_url: console_url.map(str::to_string),
            credentials_overridden: false,
        }
    }

    fn install_rustls_provider() {
        rustls::crypto::ring::default_provider()
            .install_default()
            .ok();
    }

    #[test]
    fn find_target_locates_by_profile_name() {
        install_rustls_provider();
        let a = Arc::new(ExecutionTarget::new(test_cfg("http://127.0.0.1:1", None)).unwrap());
        let targets = vec![a.clone()];
        assert!(find_target(&targets, "test-profile").is_some());
        assert!(find_target(&targets, "missing-profile").is_none());
    }

    #[tokio::test]
    async fn console_base_prefers_explicit_console_url() {
        install_rustls_provider();
        // No mock server is started for this test, so a whoami call would
        // fail the test outright (connection refused) - the explicit
        // console_url must short-circuit before that.
        let target = ExecutionTarget::new(test_cfg(
            "http://127.0.0.1:1",
            Some("https://c4c.example.com/"),
        ))
        .unwrap();
        assert_eq!(
            target.console_base().await,
            Some("https://c4c.example.com/".to_string())
        );
    }

    #[tokio::test]
    async fn console_base_uses_team_url_from_whoami_by_default() {
        install_rustls_provider();
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/identity/whoami"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "team_id": 1,
                "team_name": "C4C",
                "team_url": "https://c4c.app.eu2.coralogix.com"
            })))
            .mount(&server)
            .await;

        // No explicit console_url - must resolve via /identity/whoami by
        // default.
        let target = ExecutionTarget::new(test_cfg(&server.uri(), None)).unwrap();
        assert_eq!(
            target.console_base().await,
            Some("https://c4c.app.eu2.coralogix.com".to_string())
        );
    }

    #[tokio::test]
    async fn console_base_none_when_whoami_omits_team_url() {
        install_rustls_provider();
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/identity/whoami"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "team_id": 1,
                "team_name": "C4C"
            })))
            .mount(&server)
            .await;

        let target = ExecutionTarget::new(test_cfg(&server.uri(), None)).unwrap();
        assert_eq!(target.console_base().await, None);
    }

    #[tokio::test]
    async fn console_base_none_when_whoami_fails() {
        install_rustls_provider();
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/identity/whoami"))
            .respond_with(ResponseTemplate::new(500))
            .mount(&server)
            .await;

        let target = ExecutionTarget::new(test_cfg(&server.uri(), None)).unwrap();
        // Best-effort: a failing /identity/whoami must not error the caller,
        // just result in no console link.
        assert_eq!(target.console_base().await, None);
    }

    #[tokio::test]
    async fn console_base_explicit_console_url_takes_precedence_over_whoami() {
        install_rustls_provider();
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/identity/whoami"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "team_id": 1,
                "team_name": "Totally Different Name",
                "team_url": "https://totally-different.app.eu2.coralogix.com"
            })))
            .expect(0)
            .mount(&server)
            .await;

        // An explicit console_url must win outright - whoami must not even
        // be called (wiremock's `.expect(0)` above enforces this at drop
        // time).
        let target = ExecutionTarget::new(test_cfg(
            &server.uri(),
            Some("https://c4c.app.eu2.coralogix.com"),
        ))
        .unwrap();
        assert_eq!(
            target.console_base().await,
            Some("https://c4c.app.eu2.coralogix.com".to_string())
        );
    }

    #[tokio::test]
    async fn console_base_is_cached_after_first_resolution() {
        install_rustls_provider();
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/identity/whoami"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "team_id": 1,
                "team_name": "C4C",
                "team_url": "https://c4c.app.eu2.coralogix.com"
            })))
            .expect(1)
            .mount(&server)
            .await;

        let target = ExecutionTarget::new(test_cfg(&server.uri(), None)).unwrap();
        // Two calls must only hit /identity/whoami once (wiremock's `.expect(1)`
        // above would fail the mock verification otherwise).
        let first = target.console_base().await;
        let second = target.console_base().await;
        assert_eq!(first, second);
        assert_eq!(first, Some("https://c4c.app.eu2.coralogix.com".to_string()));
    }

    #[tokio::test]
    async fn console_base_none_when_no_console_link_set_even_with_explicit_console_url() {
        install_rustls_provider();
        // No mock server is started for this test either - if `no_console_link`
        // didn't short-circuit before the explicit `console_url` check, this
        // test would still pass (explicit URL wins), so the real assertion is
        // that suppression overrides *everything*, not just the whoami path.
        let mut target = ExecutionTarget::new(test_cfg(
            "http://127.0.0.1:1",
            Some("https://c4c.example.com/"),
        ))
        .unwrap();
        target.no_console_link = true;
        assert_eq!(target.console_base().await, None);
    }

    #[test]
    fn build_targets_propagates_no_console_link_to_every_target() {
        install_rustls_provider();
        let configs = vec![
            test_cfg("http://127.0.0.1:1", Some("https://c4c.example.com/")),
            test_cfg("http://127.0.0.1:2", Some("https://other.example.com/")),
        ];
        let targets = build_targets(configs, true, None).unwrap();
        assert_eq!(targets.len(), 2);
        for target in &targets {
            assert!(target.no_console_link);
        }
    }

    #[test]
    fn collect_successes_returns_all_when_none_fail() {
        let per_profile: Vec<(String, Result<i32>)> =
            vec![("prod".to_string(), Ok(1)), ("staging".to_string(), Ok(2))];
        let successes = report_errors_and_collect_successes(per_profile).expect("should not bail");
        assert_eq!(
            successes,
            vec![("prod".to_string(), 1), ("staging".to_string(), 2)]
        );
    }

    #[test]
    fn collect_successes_skips_errored_profiles_on_partial_failure() {
        let per_profile: Vec<(String, Result<i32>)> = vec![
            ("good".to_string(), Ok(1)),
            ("bad".to_string(), Err(anyhow::anyhow!("simulated error"))),
            ("also-good".to_string(), Ok(2)),
        ];
        let successes = report_errors_and_collect_successes(per_profile)
            .expect("partial failure should not bail");
        assert_eq!(
            successes,
            vec![("good".to_string(), 1), ("also-good".to_string(), 2)]
        );
    }

    #[test]
    fn collect_successes_bails_when_every_target_fails() {
        let per_profile: Vec<(String, Result<i32>)> = vec![
            ("a".to_string(), Err(anyhow::anyhow!("boom"))),
            ("b".to_string(), Err(anyhow::anyhow!("boom"))),
        ];
        let result = report_errors_and_collect_successes(per_profile);
        assert!(
            result.is_err(),
            "a total failure must surface as an error, not a silent empty result"
        );
    }

    #[test]
    fn collect_successes_does_not_bail_on_empty_input() {
        let per_profile: Vec<(String, Result<i32>)> = vec![];
        let successes = report_errors_and_collect_successes(per_profile)
            .expect("empty fan-out is not a failure");
        assert!(successes.is_empty());
    }
}