-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.rs
69 lines (57 loc) · 2.25 KB
/
main.rs
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
use anyhow::Result;
use arrow::util::pretty::pretty_format_batches;
use opentelemetry::global;
use opentelemetry::trace::TracerProvider;
use opentelemetry::KeyValue;
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::Resource;
use reqwest_middleware::Extension;
use reqwest_tracing::{OtelName, SpanBackendWithUrl};
use tracing_subscriber::layer::SubscriberExt;
use snowflake_api::connection::Connection;
use snowflake_api::{AuthArgs, QueryResult, SnowflakeApiBuilder};
#[tokio::main]
async fn main() -> Result<()> {
let exporter = opentelemetry_otlp::SpanExporter::builder()
.with_tonic()
.with_endpoint("http://localhost:4317")
.build()?;
let provider = opentelemetry_sdk::trace::TracerProvider::builder()
.with_batch_exporter(exporter, opentelemetry_sdk::runtime::Tokio)
.with_resource(Resource::new(vec![KeyValue::new(
opentelemetry_semantic_conventions::resource::SERVICE_NAME,
"snowflake-rust-client-demo",
)]))
.build();
let tracer = provider.tracer("snowflake");
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
let subscriber = tracing_subscriber::Registry::default().with(telemetry);
tracing::subscriber::set_global_default(subscriber)?;
dotenv::dotenv().ok();
let client = Connection::default_client_builder()?
.with_init(Extension(OtelName(std::borrow::Cow::Borrowed(
"snowflake-api",
))))
.with(reqwest_tracing::TracingMiddleware::<SpanBackendWithUrl>::new());
let builder = SnowflakeApiBuilder::new(AuthArgs::from_env()?).with_client(client.build());
let api = builder.build()?;
run_in_span(&api).await?;
global::shutdown_tracer_provider();
Ok(())
}
#[tracing::instrument(name = "snowflake_api", skip(api))]
async fn run_in_span(api: &snowflake_api::SnowflakeApi) -> anyhow::Result<()> {
let res = api.exec("select 'hello from snowflake' as col1;").await?;
match res {
QueryResult::Arrow(a) => {
println!("{}", pretty_format_batches(&a).unwrap());
}
QueryResult::Json(j) => {
println!("{}", j);
}
QueryResult::Empty => {
println!("Query finished successfully")
}
}
Ok(())
}