Skip to content

Create basic opentelemetry example #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"models/files",
"models/token",

"poem/opentelemetry-basic",
"poem/starwars",
"poem/subscription",
"poem/subscription-redis",
Expand Down
13 changes: 13 additions & 0 deletions poem/opentelemetry-basic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "poem-opentelemetry-basic"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-graphql = { path = "../../..", features = ["opentelemetry"] }
async-graphql-poem = { path = "../../../integrations/poem" }
tokio = { version = "1.8", features = ["macros", "rt-multi-thread"] }
poem = "1.3.42"
opentelemetry = { version = "0.17.0", features = ["rt-tokio"] }
42 changes: 42 additions & 0 deletions poem/opentelemetry-basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use async_graphql::{
extensions::OpenTelemetry, EmptyMutation, EmptySubscription, Object, Result, Schema,
};
use async_graphql_poem::GraphQL;
use opentelemetry::sdk::export::trace::stdout;
use poem::{listener::TcpListener, post, EndpointExt, Route, Server};

struct QueryRoot;

#[Object]
impl QueryRoot {
async fn hello(&self) -> Result<String> {
Ok("World".to_string())
}
}

#[tokio::main]
async fn main() {
let tracer = stdout::new_pipeline().install_simple();
let opentelemetry_extension = OpenTelemetry::new(tracer);

let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription)
.extension(opentelemetry_extension)
.finish();

let app = Route::new()
.at("/", post(GraphQL::new(schema.clone())))
.data(schema);

let example_curl = "\
curl '0.0.0.0:8000' \
-X POST \
-H 'content-type: application/json' \
--data '{ \"query\": \"{ hello }\" }'";

println!("Run this curl command from another terminal window to see opentelemetry output in this terminal.\n\n{example_curl}\n\n");

Server::new(TcpListener::bind("0.0.0.0:8000"))
.run(app)
.await
.unwrap();
}