Skip to content

Commit

Permalink
Merge branch 'tailcallhq:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
alankritdabral authored Mar 10, 2024
2 parents f1b9024 + 7ccbb2e commit f6d261b
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 18 deletions.
13 changes: 0 additions & 13 deletions examples/operations/rest.graphql

This file was deleted.

22 changes: 22 additions & 0 deletions examples/operations/routes.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
query posts @rest(method: "get", path: "/posts") {
posts {
id
title
body
user {
id
name
}
}
}

query users @rest(method: "get", path: "/users") {
users {
id
name
posts {
id
title
}
}
}
2 changes: 1 addition & 1 deletion examples/rest-api.graphql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
schema
@server(port: 8000, graphiql: true, hostname: "0.0.0.0")
@upstream(baseURL: "http://jsonplaceholder.typicode.com", httpCache: true)
@link(type: Operation, src: "./operations/rest.graphql") {
@link(type: Operation, src: "operations/routes.graphql") {
query: Query
}

Expand Down
8 changes: 7 additions & 1 deletion src/cli/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use tracing_subscriber::{Layer, Registry};

use super::metrics::init_metrics;
use crate::blueprint::telemetry::{OtlpExporter, Telemetry, TelemetryExporter};
use crate::cli::CLIError;
use crate::runtime::TargetRuntime;
use crate::tracing::{default_tailcall_tracing, tailcall_filter_target};

Expand Down Expand Up @@ -201,7 +202,12 @@ pub fn init_opentelemetry(config: Telemetry, runtime: &TargetRuntime) -> anyhow:
| global::Error::Metric(MetricsError::Other(_))
| global::Error::Log(LogError::Other(_)),
) {
eprintln!("OpenTelemetry error: {:?}", error);
tracing::subscriber::with_default(default_tailcall_tracing(), || {
let cli = crate::cli::CLIError::new("Open Telemetry Error")
.caused_by(vec![CLIError::new(error.to_string().as_str())])
.trace(vec!["schema".to_string(), "@telemetry".to_string()]);
tracing::error!("{}", cli.color(true));
});
}
})?;

Expand Down
70 changes: 67 additions & 3 deletions src/tracing.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,74 @@
use std::env;
use std::str::FromStr;
use std::{env, fmt};

use tracing::Subscriber;
use colored::Colorize;
use tracing::{Event, Level, Subscriber};
use tracing_subscriber::filter::filter_fn;
use tracing_subscriber::fmt::format::Writer;
use tracing_subscriber::fmt::{FmtContext, FormatEvent, FormatFields};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::registry::LookupSpan;
use tracing_subscriber::Layer;
struct FmtLevel<'a> {
level: &'a Level,
ansi: bool,
}

impl<'a> FmtLevel<'a> {
pub(crate) fn new(level: &'a Level, ansi: bool) -> Self {
Self { level, ansi }
}
}

const TRACE_STR: &str = "TRACE";
const DEBUG_STR: &str = "DEBUG";
const INFO_STR: &str = "INFO";
const WARN_STR: &str = "WARN";
const ERROR_STR: &str = "ERROR";

impl<'a> fmt::Display for FmtLevel<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.ansi {
match *self.level {
Level::TRACE => write!(f, "{:>5} ", TRACE_STR.magenta()),
Level::DEBUG => write!(f, "{:>5} ", DEBUG_STR.blue()),
Level::INFO => write!(f, "{:>5} ", INFO_STR.green()),
Level::WARN => write!(f, "{:>5} ", WARN_STR.yellow()),
Level::ERROR => write!(f, "{:>5} ", ERROR_STR.red()),
}
} else {
match *self.level {
Level::TRACE => f.pad(TRACE_STR),
Level::DEBUG => f.pad(DEBUG_STR),
Level::INFO => f.pad(INFO_STR),
Level::WARN => f.pad(WARN_STR),
Level::ERROR => f.pad(ERROR_STR),
}
}
}
}

struct CliFmt;

impl<S, N> FormatEvent<S, N> for CliFmt
where
S: Subscriber + for<'a> LookupSpan<'a>,
N: for<'a> FormatFields<'a> + 'static,
{
fn format_event(
&self,
ctx: &FmtContext<'_, S, N>,
mut writer: Writer<'_>,
event: &Event<'_>,
) -> fmt::Result {
let meta = event.metadata();
let fmt_level = FmtLevel::new(meta.level(), writer.has_ansi_escapes());
write!(writer, "{}", fmt_level)?;
ctx.field_format().format_fields(writer.by_ref(), event)?;

writeln!(writer)
}
}

pub fn default_tailcall_tracing() -> impl Subscriber {
default_tracing().with(tailcall_filter_target())
Expand All @@ -29,7 +93,7 @@ pub fn default_tracing() -> impl Subscriber {
.with_max_level(level)
.without_time()
.with_target(false)
.compact()
.event_format(CliFmt)
.finish()
}

Expand Down
5 changes: 5 additions & 0 deletions tests/execution_spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ A Markdown-based snapshot testing framework for Tailcall.
- [Test process](#test-process)
- [Snapshots](#snapshots)
- [Porting from `http_spec`/`graphql_spec`](#porting-from-graphql_spechttp_spec)
- [Maintainance](#Maintenance)

## Structure

Expand Down Expand Up @@ -273,3 +274,7 @@ Each test's `.md` file may have a file name suffix of `-error` if the bare file
1. An [`sdl error` instruction](#instruction) is appended.
1. The server SDL is put into a [`server` block](#server).
1. The expected errors are put into an `errors` snapshot.

## Maintenance

1. To clean unused snapshots, run `cargo insta test --delete-unreferenced-snapshots`.

0 comments on commit f6d261b

Please sign in to comment.