Opentelemetry (#1992)

* Add otlp export

* Add jaeger to dev docker

* More instrument

* Update deps

* Merge from main.

* Add comment about viewing traces

* Fixing reqwest-tracing dep.

* Update pre-commit regex

* Move imports to top

Co-authored-by: Dessalines <tyhou13@gmx.com>
Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
This commit is contained in:
Riley 2022-01-06 13:10:20 -06:00 committed by GitHub
parent 661f97a073
commit 3032a2745b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 435 additions and 26 deletions

266
Cargo.lock generated
View file

@ -348,6 +348,27 @@ dependencies = [
"event-listener", "event-listener",
] ]
[[package]]
name = "async-stream"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "171374e7e3b2504e0e5236e3b59260560f9fe94bfe9ac39ba5e4e929c5590625"
dependencies = [
"async-stream-impl",
"futures-core",
]
[[package]]
name = "async-stream-impl"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "648ed8c8d2ce5409ccd57453d9d1b214b342a0d69376a6feda1fd6cae3299308"
dependencies = [
"proc-macro2 1.0.33",
"quote 1.0.10",
"syn 1.0.82",
]
[[package]] [[package]]
name = "async-trait" name = "async-trait"
version = "0.1.52" version = "0.1.52"
@ -1088,6 +1109,12 @@ version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31586bda1b136406162e381a3185a506cdfc1631708dd40cba2f6628d8634499" checksum = "31586bda1b136406162e381a3185a506cdfc1631708dd40cba2f6628d8634499"
[[package]]
name = "fixedbitset"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37ab347416e802de484e4d03c7316c48f1ecb56574dfd4a46a80f173ce1de04d"
[[package]] [[package]]
name = "flate2" name = "flate2"
version = "1.0.22" version = "1.0.22"
@ -1491,6 +1518,18 @@ dependencies = [
"want", "want",
] ]
[[package]]
name = "hyper-timeout"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbb958482e8c7be4bc3cf272a766a2b0bf1a6755e7a6ae777f017a31d11b13b1"
dependencies = [
"hyper",
"pin-project-lite",
"tokio",
"tokio-io-timeout",
]
[[package]] [[package]]
name = "hyper-tls" name = "hyper-tls"
version = "0.5.0" version = "0.5.0"
@ -1949,6 +1988,8 @@ dependencies = [
"lemmy_utils", "lemmy_utils",
"lemmy_websocket", "lemmy_websocket",
"openssl", "openssl",
"opentelemetry",
"opentelemetry-otlp",
"reqwest", "reqwest",
"reqwest-middleware", "reqwest-middleware",
"reqwest-tracing", "reqwest-tracing",
@ -1960,6 +2001,7 @@ dependencies = [
"tracing-actix-web", "tracing-actix-web",
"tracing-error", "tracing-error",
"tracing-log", "tracing-log",
"tracing-opentelemetry",
"tracing-subscriber", "tracing-subscriber",
"url", "url",
] ]
@ -2018,6 +2060,7 @@ dependencies = [
"lemmy_db_views", "lemmy_db_views",
"lemmy_db_views_actor", "lemmy_db_views_actor",
"lemmy_utils", "lemmy_utils",
"opentelemetry",
"rand 0.8.4", "rand 0.8.4",
"reqwest", "reqwest",
"reqwest-middleware", "reqwest-middleware",
@ -2027,6 +2070,7 @@ dependencies = [
"strum_macros", "strum_macros",
"tokio", "tokio",
"tracing", "tracing",
"tracing-opentelemetry",
] ]
[[package]] [[package]]
@ -2287,6 +2331,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "multimap"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a"
[[package]] [[package]]
name = "native-tls" name = "native-tls"
version = "0.2.8" version = "0.2.8"
@ -2463,6 +2513,42 @@ dependencies = [
"vcpkg", "vcpkg",
] ]
[[package]]
name = "opentelemetry"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1cf9b1c4e9a6c4de793c632496fa490bdc0e1eea73f0c91394f7b6990935d22"
dependencies = [
"async-trait",
"crossbeam-channel",
"futures",
"js-sys",
"lazy_static",
"percent-encoding",
"pin-project",
"rand 0.8.4",
"thiserror",
"tokio",
"tokio-stream",
]
[[package]]
name = "opentelemetry-otlp"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f19d4b43842433c420c548c985d158f5628bba5b518e0be64627926d19889992"
dependencies = [
"async-trait",
"futures",
"http",
"opentelemetry",
"prost",
"thiserror",
"tokio",
"tonic",
"tonic-build",
]
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.11.2" version = "0.11.2"
@ -2554,6 +2640,16 @@ dependencies = [
"sha-1 0.8.2", "sha-1 0.8.2",
] ]
[[package]]
name = "petgraph"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "467d164a6de56270bd7c4d070df81d07beace25012d5103ced4e9ff08d6afdb7"
dependencies = [
"fixedbitset",
"indexmap",
]
[[package]] [[package]]
name = "phf" name = "phf"
version = "0.8.0" version = "0.8.0"
@ -2681,6 +2777,57 @@ dependencies = [
"unicode-xid 0.2.2", "unicode-xid 0.2.2",
] ]
[[package]]
name = "prost"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de5e2533f59d08fcf364fd374ebda0692a70bd6d7e66ef97f306f45c6c5d8020"
dependencies = [
"bytes",
"prost-derive",
]
[[package]]
name = "prost-build"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "355f634b43cdd80724ee7848f95770e7e70eefa6dcf14fea676216573b8fd603"
dependencies = [
"bytes",
"heck",
"itertools",
"log",
"multimap",
"petgraph",
"prost",
"prost-types",
"tempfile",
"which",
]
[[package]]
name = "prost-derive"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "600d2f334aa05acb02a755e217ef1ab6dea4d51b58b7846588b747edec04efba"
dependencies = [
"anyhow",
"itertools",
"proc-macro2 1.0.33",
"quote 1.0.10",
"syn 1.0.82",
]
[[package]]
name = "prost-types"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "603bbd6394701d13f3f25aada59c7de9d35a6a5887cfc156181234a44002771b"
dependencies = [
"bytes",
"prost",
]
[[package]] [[package]]
name = "quick-xml" name = "quick-xml"
version = "0.22.0" version = "0.22.0"
@ -2962,11 +3109,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89af431b8c46776b5071a9a739c2b5fadbed6be2c6158d1ac5f71c4da3d2261c" checksum = "89af431b8c46776b5071a9a739c2b5fadbed6be2c6158d1ac5f71c4da3d2261c"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"opentelemetry",
"reqwest", "reqwest",
"reqwest-middleware", "reqwest-middleware",
"task-local-extensions", "task-local-extensions",
"tokio", "tokio",
"tracing", "tracing",
"tracing-opentelemetry",
] ]
[[package]] [[package]]
@ -3553,6 +3702,7 @@ dependencies = [
"libc", "libc",
"memchr", "memchr",
"mio 0.7.14", "mio 0.7.14",
"num_cpus",
"once_cell", "once_cell",
"parking_lot", "parking_lot",
"pin-project-lite", "pin-project-lite",
@ -3561,6 +3711,16 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "tokio-io-timeout"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30b74022ada614a1b4834de765f9bb43877f910cc8ce4be40e89042c9223a8bf"
dependencies = [
"pin-project-lite",
"tokio",
]
[[package]] [[package]]
name = "tokio-macros" name = "tokio-macros"
version = "1.6.0" version = "1.6.0"
@ -3593,6 +3753,17 @@ dependencies = [
"webpki", "webpki",
] ]
[[package]]
name = "tokio-stream"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50145484efff8818b5ccd256697f36863f587da82cf8b409c53adf1e840798e3"
dependencies = [
"futures-core",
"pin-project-lite",
"tokio",
]
[[package]] [[package]]
name = "tokio-util" name = "tokio-util"
version = "0.6.9" version = "0.6.9"
@ -3616,6 +3787,76 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "tonic"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "796c5e1cd49905e65dd8e700d4cb1dffcbfdb4fc9d017de08c1a537afd83627c"
dependencies = [
"async-stream",
"async-trait",
"base64 0.13.0",
"bytes",
"futures-core",
"futures-util",
"h2",
"http",
"http-body",
"hyper",
"hyper-timeout",
"percent-encoding",
"pin-project",
"prost",
"prost-derive",
"tokio",
"tokio-stream",
"tokio-util",
"tower",
"tower-layer",
"tower-service",
"tracing",
"tracing-futures",
]
[[package]]
name = "tonic-build"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12b52d07035516c2b74337d2ac7746075e7dcae7643816c1b12c5ff8a7484c08"
dependencies = [
"proc-macro2 1.0.33",
"prost-build",
"quote 1.0.10",
"syn 1.0.82",
]
[[package]]
name = "tower"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5651b5f6860a99bd1adb59dbfe1db8beb433e73709d9032b413a77e2fb7c066a"
dependencies = [
"futures-core",
"futures-util",
"indexmap",
"pin-project",
"pin-project-lite",
"rand 0.8.4",
"slab",
"tokio",
"tokio-stream",
"tokio-util",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "tower-layer"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "343bc9466d3fe6b0f960ef45960509f84480bf4fd96f92901afe7ff3df9d3a62"
[[package]] [[package]]
name = "tower-service" name = "tower-service"
version = "0.3.1" version = "0.3.1"
@ -3629,6 +3870,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "375a639232caf30edfc78e8d89b2d4c375515393e7af7e16f01cd96917fb2105" checksum = "375a639232caf30edfc78e8d89b2d4c375515393e7af7e16f01cd96917fb2105"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"log",
"pin-project-lite", "pin-project-lite",
"tracing-attributes", "tracing-attributes",
"tracing-core", "tracing-core",
@ -3698,6 +3940,19 @@ dependencies = [
"tracing-core", "tracing-core",
] ]
[[package]]
name = "tracing-opentelemetry"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ffbf13a0f8b054a4e59df3a173b818e9c6177c02789871f2073977fd0062076"
dependencies = [
"opentelemetry",
"tracing",
"tracing-core",
"tracing-log",
"tracing-subscriber",
]
[[package]] [[package]]
name = "tracing-subscriber" name = "tracing-subscriber"
version = "0.3.3" version = "0.3.3"
@ -4027,6 +4282,17 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8b77fdfd5a253be4ab714e4ffa3c49caf146b4de743e97510c0656cf90f1e8e" checksum = "d8b77fdfd5a253be4ab714e4ffa3c49caf146b4de743e97510c0656cf90f1e8e"
[[package]]
name = "which"
version = "4.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea187a8ef279bc014ec368c27a920da2024d2a711109bfbe3440585d5cf27ad9"
dependencies = [
"either",
"lazy_static",
"libc",
]
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

View file

@ -62,12 +62,15 @@ tokio = { version = "1.14.0", features = ["sync"] }
anyhow = "1.0.51" anyhow = "1.0.51"
reqwest = { version = "0.11.7", features = ["json"] } reqwest = { version = "0.11.7", features = ["json"] }
reqwest-middleware = "0.1.3" reqwest-middleware = "0.1.3"
reqwest-tracing = "0.2.0" reqwest-tracing = { version = "0.2.0", features = ["opentelemetry_0_16"] }
activitystreams = "0.7.0-alpha.14" activitystreams = "0.7.0-alpha.14"
actix-rt = { version = "2.5.0", default-features = false } actix-rt = { version = "2.5.0", default-features = false }
serde_json = { version = "1.0.72", features = ["preserve_order"] } serde_json = { version = "1.0.72", features = ["preserve_order"] }
clokwerk = "0.3.5" clokwerk = "0.3.5"
doku = "0.10.2" doku = "0.10.2"
opentelemetry = { version = "0.16", features = ["rt-tokio"] }
opentelemetry-otlp = "0.9"
tracing-opentelemetry = "0.16"
[dev-dependencies.cargo-husky] [dev-dependencies.cargo-husky]
version = "1.5.0" version = "1.5.0"

View file

@ -111,10 +111,11 @@
# Whether the site is available over TLS. Needs to be true for federation to work. # Whether the site is available over TLS. Needs to be true for federation to work.
tls_enabled: true tls_enabled: true
# Address where pictrs is available (for image hosting) # Address where pictrs is available (for image hosting)
pictrs_url: "http:#localhost:8080" pictrs_url: "http://localhost:8080"
slur_filter: "(\bThis\b)|(\bis\b)|(\bsample\b)" slur_filter: "(\bThis\b)|(\bis\b)|(\bsample\b)"
# Maximum length of local community and user names # Maximum length of local community and user names
actor_name_max_length: 20 actor_name_max_length: 20
# Maximum number of HTTP requests allowed to handle a single incoming activity (or a single object fetch through the search). # Maximum number of HTTP requests allowed to handle a single incoming activity (or a single object fetch through the search).
http_fetch_retry_limit: 25 http_fetch_retry_limit: 25
opentelemetry_url: "http://localhost:4317"
} }

View file

@ -55,6 +55,7 @@ where
res res
} }
#[tracing::instrument(skip_all)]
pub async fn is_mod_or_admin( pub async fn is_mod_or_admin(
pool: &DbPool, pool: &DbPool,
person_id: PersonId, person_id: PersonId,
@ -77,6 +78,7 @@ pub fn is_admin(local_user_view: &LocalUserView) -> Result<(), LemmyError> {
Ok(()) Ok(())
} }
#[tracing::instrument(skip_all)]
pub async fn get_post(post_id: PostId, pool: &DbPool) -> Result<Post, LemmyError> { pub async fn get_post(post_id: PostId, pool: &DbPool) -> Result<Post, LemmyError> {
blocking(pool, move |conn| Post::read(conn, post_id)) blocking(pool, move |conn| Post::read(conn, post_id))
.await? .await?
@ -84,6 +86,7 @@ pub async fn get_post(post_id: PostId, pool: &DbPool) -> Result<Post, LemmyError
.map_err(|e| e.with_message("couldnt_find_post")) .map_err(|e| e.with_message("couldnt_find_post"))
} }
#[tracing::instrument(skip_all)]
pub async fn mark_post_as_read( pub async fn mark_post_as_read(
person_id: PersonId, person_id: PersonId,
post_id: PostId, post_id: PostId,
@ -99,6 +102,7 @@ pub async fn mark_post_as_read(
.map_err(|e| e.with_message("couldnt_mark_post_as_read")) .map_err(|e| e.with_message("couldnt_mark_post_as_read"))
} }
#[tracing::instrument(skip_all)]
pub async fn mark_post_as_unread( pub async fn mark_post_as_unread(
person_id: PersonId, person_id: PersonId,
post_id: PostId, post_id: PostId,
@ -114,6 +118,7 @@ pub async fn mark_post_as_unread(
.map_err(|e| e.with_message("couldnt_mark_post_as_read")) .map_err(|e| e.with_message("couldnt_mark_post_as_read"))
} }
#[tracing::instrument(skip_all)]
pub async fn get_local_user_view_from_jwt( pub async fn get_local_user_view_from_jwt(
jwt: &str, jwt: &str,
pool: &DbPool, pool: &DbPool,
@ -154,6 +159,7 @@ pub fn check_validator_time(
} }
} }
#[tracing::instrument(skip_all)]
pub async fn get_local_user_view_from_jwt_opt( pub async fn get_local_user_view_from_jwt_opt(
jwt: Option<&Sensitive<String>>, jwt: Option<&Sensitive<String>>,
pool: &DbPool, pool: &DbPool,
@ -165,6 +171,7 @@ pub async fn get_local_user_view_from_jwt_opt(
} }
} }
#[tracing::instrument(skip_all)]
pub async fn get_local_user_settings_view_from_jwt( pub async fn get_local_user_settings_view_from_jwt(
jwt: &Sensitive<String>, jwt: &Sensitive<String>,
pool: &DbPool, pool: &DbPool,
@ -189,6 +196,7 @@ pub async fn get_local_user_settings_view_from_jwt(
Ok(local_user_view) Ok(local_user_view)
} }
#[tracing::instrument(skip_all)]
pub async fn get_local_user_settings_view_from_jwt_opt( pub async fn get_local_user_settings_view_from_jwt_opt(
jwt: Option<&Sensitive<String>>, jwt: Option<&Sensitive<String>>,
pool: &DbPool, pool: &DbPool,
@ -202,6 +210,7 @@ pub async fn get_local_user_settings_view_from_jwt_opt(
} }
} }
#[tracing::instrument(skip_all)]
pub async fn check_community_ban( pub async fn check_community_ban(
person_id: PersonId, person_id: PersonId,
community_id: CommunityId, community_id: CommunityId,
@ -216,6 +225,7 @@ pub async fn check_community_ban(
} }
} }
#[tracing::instrument(skip_all)]
pub async fn check_community_deleted_or_removed( pub async fn check_community_deleted_or_removed(
community_id: CommunityId, community_id: CommunityId,
pool: &DbPool, pool: &DbPool,
@ -239,6 +249,7 @@ pub fn check_post_deleted_or_removed(post: &Post) -> Result<(), LemmyError> {
} }
} }
#[tracing::instrument(skip_all)]
pub async fn check_person_block( pub async fn check_person_block(
my_id: PersonId, my_id: PersonId,
potential_blocker_id: PersonId, potential_blocker_id: PersonId,
@ -252,6 +263,7 @@ pub async fn check_person_block(
} }
} }
#[tracing::instrument(skip_all)]
pub async fn check_downvotes_enabled(score: i16, pool: &DbPool) -> Result<(), LemmyError> { pub async fn check_downvotes_enabled(score: i16, pool: &DbPool) -> Result<(), LemmyError> {
if score == -1 { if score == -1 {
let site = blocking(pool, Site::read_simple).await??; let site = blocking(pool, Site::read_simple).await??;
@ -262,6 +274,7 @@ pub async fn check_downvotes_enabled(score: i16, pool: &DbPool) -> Result<(), Le
Ok(()) Ok(())
} }
#[tracing::instrument(skip_all)]
pub async fn check_private_instance( pub async fn check_private_instance(
local_user_view: &Option<LocalUserView>, local_user_view: &Option<LocalUserView>,
pool: &DbPool, pool: &DbPool,
@ -275,6 +288,7 @@ pub async fn check_private_instance(
Ok(()) Ok(())
} }
#[tracing::instrument(skip_all)]
pub async fn build_federated_instances( pub async fn build_federated_instances(
pool: &DbPool, pool: &DbPool,
federation_config: &FederationConfig, federation_config: &FederationConfig,

View file

@ -31,7 +31,7 @@ use lemmy_utils::{
LemmyError, LemmyError,
}; };
use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud}; use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
use tracing::warn; use tracing::{warn, Instrument};
use url::Url; use url::Url;
use webmention::{Webmention, WebmentionError}; use webmention::{Webmention, WebmentionError};
@ -132,7 +132,11 @@ impl PerformCrud for CreatePost {
let mut webmention = let mut webmention =
Webmention::new::<Url>(updated_post.ap_id.clone().into(), url.clone().into())?; Webmention::new::<Url>(updated_post.ap_id.clone().into(), url.clone().into())?;
webmention.set_checked(true); webmention.set_checked(true);
match webmention.send().await { match webmention
.send()
.instrument(tracing::info_span!("Sending webmention"))
.await
{
Ok(_) => {} Ok(_) => {}
Err(WebmentionError::NoEndpointDiscovered(_)) => {} Err(WebmentionError::NoEndpointDiscovered(_)) => {}
Err(e) => warn!("Failed to send webmention: {}", e), Err(e) => warn!("Failed to send webmention: {}", e),

View file

@ -80,12 +80,11 @@ async fn do_send(task: SendActivityTask, client: &ClientWithMiddleware) -> Resul
match result { match result {
Ok(o) => { Ok(o) => {
if !o.status().is_success() { if !o.status().is_success() {
let status = o.status();
let text = o.text().await?;
warn!( warn!(
"Send {} to {} failed with status {}: {}", "Send {} to {} failed with status {}: {}",
task.activity_id, task.activity_id, task.inbox, status, text
task.inbox,
o.status(),
o.text().await?
); );
} }
} }

View file

@ -18,6 +18,7 @@ struct SendError(pub String);
#[error("Error receiving response, {0}")] #[error("Error receiving response, {0}")]
pub struct RecvError(pub String); pub struct RecvError(pub String);
#[tracing::instrument(skip_all)]
pub async fn retry<F, Fut, T>(f: F) -> Result<T, reqwest_middleware::Error> pub async fn retry<F, Fut, T>(f: F) -> Result<T, reqwest_middleware::Error>
where where
F: Fn() -> Fut, F: Fn() -> Fut,
@ -26,6 +27,7 @@ where
retry_custom(|| async { Ok((f)().await) }).await retry_custom(|| async { Ok((f)().await) }).await
} }
#[tracing::instrument(skip_all)]
async fn retry_custom<F, Fut, T>(f: F) -> Result<T, reqwest_middleware::Error> async fn retry_custom<F, Fut, T>(f: F) -> Result<T, reqwest_middleware::Error>
where where
F: Fn() -> Fut, F: Fn() -> Fut,
@ -61,6 +63,7 @@ pub struct SiteMetadata {
} }
/// Fetches the post link html tags (like title, description, image, etc) /// Fetches the post link html tags (like title, description, image, etc)
#[tracing::instrument(skip_all)]
pub async fn fetch_site_metadata( pub async fn fetch_site_metadata(
client: &ClientWithMiddleware, client: &ClientWithMiddleware,
url: &Url, url: &Url,
@ -159,6 +162,7 @@ pub(crate) struct PictrsFile {
delete_token: String, delete_token: String,
} }
#[tracing::instrument(skip_all)]
pub(crate) async fn fetch_pictrs( pub(crate) async fn fetch_pictrs(
client: &ClientWithMiddleware, client: &ClientWithMiddleware,
settings: &Settings, settings: &Settings,
@ -192,6 +196,7 @@ pub(crate) async fn fetch_pictrs(
/// Both are options, since the URL might be either an html page, or an image /// Both are options, since the URL might be either an html page, or an image
/// Returns the SiteMetadata, and a Pictrs URL, if there is a picture associated /// Returns the SiteMetadata, and a Pictrs URL, if there is a picture associated
#[tracing::instrument(skip_all)]
pub async fn fetch_site_data( pub async fn fetch_site_data(
client: &ClientWithMiddleware, client: &ClientWithMiddleware,
settings: &Settings, settings: &Settings,
@ -242,6 +247,7 @@ pub async fn fetch_site_data(
} }
} }
#[tracing::instrument(skip_all)]
async fn is_image_content_type(client: &ClientWithMiddleware, url: &Url) -> Result<(), LemmyError> { async fn is_image_content_type(client: &ClientWithMiddleware, url: &Url) -> Result<(), LemmyError> {
let response = client.get(url.as_str()).send().await?; let response = client.get(url.as_str()).send().await?;
if response if response

View file

@ -49,6 +49,10 @@ pub struct Settings {
/// Maximum number of HTTP requests allowed to handle a single incoming activity (or a single object fetch through the search). /// Maximum number of HTTP requests allowed to handle a single incoming activity (or a single object fetch through the search).
#[default(25)] #[default(25)]
pub http_fetch_retry_limit: i32, pub http_fetch_retry_limit: i32,
#[default(None)]
#[doku(example = "http://localhost:4317")]
pub opentelemetry_url: Option<String>,
} }
#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)] #[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]

View file

@ -34,3 +34,5 @@ strum_macros = "0.23.1"
chrono = { version = "0.4.19", features = ["serde"] } chrono = { version = "0.4.19", features = ["serde"] }
actix-web = { version = "4.0.0-beta.18", default-features = false, features = ["rustls"] } actix-web = { version = "4.0.0-beta.18", default-features = false, features = ["rustls"] }
actix-web-actors = { version = "4.0.0-beta.8", default-features = false } actix-web-actors = { version = "4.0.0-beta.8", default-features = false }
opentelemetry = "0.16"
tracing-opentelemetry = "0.16"

View file

@ -6,9 +6,11 @@ use crate::{
use actix::{Actor, Context, Handler, ResponseFuture}; use actix::{Actor, Context, Handler, ResponseFuture};
use lemmy_db_schema::naive_now; use lemmy_db_schema::naive_now;
use lemmy_utils::ConnectionId; use lemmy_utils::ConnectionId;
use opentelemetry::trace::TraceContextExt;
use rand::Rng; use rand::Rng;
use serde::Serialize; use serde::Serialize;
use tracing::{error, info}; use tracing::{error, info};
use tracing_opentelemetry::OpenTelemetrySpanExt;
/// Make actor from `ChatServer` /// Make actor from `ChatServer`
impl Actor for ChatServer { impl Actor for ChatServer {
@ -62,13 +64,32 @@ impl Handler<Disconnect> for ChatServer {
} }
} }
fn root_span() -> tracing::Span {
let span = tracing::info_span!(
parent: None,
"Websocket Request",
trace_id = tracing::field::Empty,
);
{
let trace_id = span.context().span().span_context().trace_id().to_hex();
span.record("trace_id", &tracing::field::display(trace_id));
}
span
}
/// Handler for Message message. /// Handler for Message message.
impl Handler<StandardMessage> for ChatServer { impl Handler<StandardMessage> for ChatServer {
type Result = ResponseFuture<Result<String, std::convert::Infallible>>; type Result = ResponseFuture<Result<String, std::convert::Infallible>>;
fn handle(&mut self, msg: StandardMessage, ctx: &mut Context<Self>) -> Self::Result { fn handle(&mut self, msg: StandardMessage, ctx: &mut Context<Self>) -> Self::Result {
let fut = self.parse_json_message(msg, ctx); let fut = self.parse_json_message(msg, ctx);
Box::pin(async move { let span = root_span();
use tracing::Instrument;
Box::pin(
async move {
match fut.await { match fut.await {
Ok(m) => { Ok(m) => {
// info!("Message Sent: {}", m); // info!("Message Sent: {}", m);
@ -82,7 +103,9 @@ impl Handler<StandardMessage> for ChatServer {
) )
} }
} }
}) }
.instrument(span),
)
} }
} }

View file

@ -31,6 +31,7 @@ use lemmy_db_views::{
use lemmy_db_views_actor::community_view::CommunityView; use lemmy_db_views_actor::community_view::CommunityView;
use lemmy_utils::{utils::MentionData, ConnectionId, LemmyError}; use lemmy_utils::{utils::MentionData, ConnectionId, LemmyError};
#[tracing::instrument(skip_all)]
pub async fn send_post_ws_message<OP: ToString + Send + OperationType + 'static>( pub async fn send_post_ws_message<OP: ToString + Send + OperationType + 'static>(
post_id: PostId, post_id: PostId,
op: OP, op: OP,
@ -56,6 +57,7 @@ pub async fn send_post_ws_message<OP: ToString + Send + OperationType + 'static>
// TODO: in many call sites in apub crate, we are setting an empty vec for recipient_ids, // TODO: in many call sites in apub crate, we are setting an empty vec for recipient_ids,
// we should get the actual recipient actors from somewhere // we should get the actual recipient actors from somewhere
#[tracing::instrument(skip_all)]
pub async fn send_comment_ws_message_simple<OP: ToString + Send + OperationType + 'static>( pub async fn send_comment_ws_message_simple<OP: ToString + Send + OperationType + 'static>(
comment_id: CommentId, comment_id: CommentId,
op: OP, op: OP,
@ -64,6 +66,7 @@ pub async fn send_comment_ws_message_simple<OP: ToString + Send + OperationType
send_comment_ws_message(comment_id, op, None, None, None, vec![], context).await send_comment_ws_message(comment_id, op, None, None, None, vec![], context).await
} }
#[tracing::instrument(skip_all)]
pub async fn send_comment_ws_message<OP: ToString + Send + OperationType + 'static>( pub async fn send_comment_ws_message<OP: ToString + Send + OperationType + 'static>(
comment_id: CommentId, comment_id: CommentId,
op: OP, op: OP,
@ -102,6 +105,7 @@ pub async fn send_comment_ws_message<OP: ToString + Send + OperationType + 'stat
Ok(res) Ok(res)
} }
#[tracing::instrument(skip_all)]
pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'static>( pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'static>(
community_id: CommunityId, community_id: CommunityId,
op: OP, op: OP,
@ -130,6 +134,7 @@ pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'st
Ok(res) Ok(res)
} }
#[tracing::instrument(skip_all)]
pub async fn send_pm_ws_message<OP: ToString + Send + OperationType + 'static>( pub async fn send_pm_ws_message<OP: ToString + Send + OperationType + 'static>(
private_message_id: PrivateMessageId, private_message_id: PrivateMessageId,
op: OP, op: OP,
@ -168,6 +173,7 @@ pub async fn send_pm_ws_message<OP: ToString + Send + OperationType + 'static>(
Ok(res) Ok(res)
} }
#[tracing::instrument(skip_all)]
pub async fn send_local_notifs( pub async fn send_local_notifs(
mentions: Vec<MentionData>, mentions: Vec<MentionData>,
comment: &Comment, comment: &Comment,

View file

@ -24,6 +24,7 @@ services:
depends_on: depends_on:
- pictrs - pictrs
- postgres - postgres
- otel
lemmy-ui: lemmy-ui:
image: dessalines/lemmy-ui:0.14.3 image: dessalines/lemmy-ui:0.14.3
@ -51,6 +52,31 @@ services:
pictrs: pictrs:
image: asonix/pictrs:0.3.0-beta.11 image: asonix/pictrs:0.3.0-beta.11
user: 991:991 user: 991:991
environment:
- PICTRS_OPENTELEMETRY_URL=http://otel:4137
volumes: volumes:
- ./volumes/pictrs:/mnt - ./volumes/pictrs:/mnt
restart: always restart: always
depends_on:
- otel
otel:
image: otel/opentelemetry-collector:latest
command: --config otel-local-config.yaml
ports:
- "4317:4317"
volumes:
- type: bind
source: ./otel.yml
target: /otel-local-config.yaml
restart: always
depends_on:
- jaeger
jaeger:
image: jaegertracing/all-in-one:1
ports:
- "14250:14250"
# To view traces, visit http://localhost:16686
- "16686:16686"
restart: always

25
docker/dev/otel.yml Normal file
View file

@ -0,0 +1,25 @@
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4137
processors:
batch:
exporters:
logging:
jaeger:
endpoint: jaeger:14250
insecure: true
service:
pipelines:
traces:
receivers:
- otlp
processors:
- batch
exporters:
- logging
- jaeger

View file

@ -11,6 +11,8 @@
site_name: "lemmy-test" site_name: "lemmy-test"
} }
opentelemetry_url: "http://otel:4137"
# the domain name of your instance (eg "lemmy.ml") # the domain name of your instance (eg "lemmy.ml")
hostname: "my_domain" hostname: "my_domain"
# address where lemmy should listen for incoming requests # address where lemmy should listen for incoming requests

View file

@ -5,7 +5,7 @@ dest=${1-config/defaults.hjson}
cargo run -- --print-config-docs > "$dest" cargo run -- --print-config-docs > "$dest"
# replace // comments with # # replace // comments with #
sed -i "s/\/\//#/" "$dest" sed -i "s/^\([[:space:]]*\)\/\//\1#/" "$dest"
# remove trailing commas # remove trailing commas
sed -i "s/,\$//" "$dest" sed -i "s/,\$//" "$dest"
# remove quotes around json keys # remove quotes around json keys

View file

@ -5,14 +5,21 @@ pub mod root_span_builder;
pub mod scheduled_tasks; pub mod scheduled_tasks;
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use opentelemetry::{
sdk::{propagation::TraceContextPropagator, Resource},
KeyValue,
};
use opentelemetry_otlp::WithExportConfig;
use tracing::subscriber::set_global_default; use tracing::subscriber::set_global_default;
use tracing_error::ErrorLayer; use tracing_error::ErrorLayer;
use tracing_log::LogTracer; use tracing_log::LogTracer;
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry}; use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
pub fn init_tracing() -> Result<(), LemmyError> { pub fn init_tracing(opentelemetry_url: Option<&str>) -> Result<(), LemmyError> {
LogTracer::init()?; LogTracer::init()?;
opentelemetry::global::set_text_map_propagator(TraceContextPropagator::new());
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")); let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
let format_layer = tracing_subscriber::fmt::layer(); let format_layer = tracing_subscriber::fmt::layer();
@ -21,7 +28,28 @@ pub fn init_tracing() -> Result<(), LemmyError> {
.with(format_layer) .with(format_layer)
.with(ErrorLayer::default()); .with(ErrorLayer::default());
if let Some(url) = opentelemetry_url {
let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_trace_config(
opentelemetry::sdk::trace::config()
.with_resource(Resource::new(vec![KeyValue::new("service.name", "lemmy")])),
)
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint(url),
)
.install_batch(opentelemetry::runtime::Tokio)?;
let otel_layer = tracing_opentelemetry::layer().with_tracer(tracer);
let subscriber = subscriber.with(otel_layer);
set_global_default(subscriber)?; set_global_default(subscriber)?;
} else {
set_global_default(subscriber)?;
}
Ok(()) Ok(())
} }

View file

@ -49,10 +49,10 @@ async fn main() -> Result<(), LemmyError> {
return Ok(()); return Ok(());
} }
init_tracing()?;
let settings = Settings::init().expect("Couldn't initialize settings."); let settings = Settings::init().expect("Couldn't initialize settings.");
init_tracing(settings.opentelemetry_url.as_deref())?;
// Set up the r2d2 connection pool // Set up the r2d2 connection pool
let db_url = match get_database_url_from_env() { let db_url = match get_database_url_from_env() {
Ok(url) => url, Ok(url) => url,