{"id":15988379,"url":"https://github.com/sd2k/rocket_newrelic","last_synced_at":"2025-04-02T06:42:07.379Z","repository":{"id":49113603,"uuid":"188026321","full_name":"sd2k/rocket_newrelic","owner":"sd2k","description":"A Rocket fairing providing New Relic instrumentation","archived":false,"fork":false,"pushed_at":"2024-03-21T12:53:43.000Z","size":26,"stargazers_count":0,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T20:42:54.806Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sd2k.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-22T11:37:49.000Z","updated_at":"2024-03-21T12:53:41.000Z","dependencies_parsed_at":"2025-03-12T01:30:40.336Z","dependency_job_id":null,"html_url":"https://github.com/sd2k/rocket_newrelic","commit_stats":{"total_commits":17,"total_committers":4,"mean_commits":4.25,"dds":"0.47058823529411764","last_synced_commit":"fd4748692175294b889444bf6a32e0f899463f52"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sd2k%2Frocket_newrelic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sd2k%2Frocket_newrelic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sd2k%2Frocket_newrelic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sd2k%2Frocket_newrelic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sd2k","download_url":"https://codeload.github.com/sd2k/rocket_newrelic/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246769984,"owners_count":20830769,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-08T04:03:11.729Z","updated_at":"2025-04-02T06:42:07.362Z","avatar_url":"https://github.com/sd2k.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rocket NewRelic\n\n[![Build Status](https://travis-ci.org/sd2k/rocket_newrelic.svg?branch=master)](https://travis-ci.org/sd2k/rocket_newrelic)\n[![docs.rs](https://docs.rs/rocket_newrelic/badge.svg)](https://docs.rs/rocket_newrelic)\n[![crates.io](https://img.shields.io/crates/v/rocket_newrelic.svg)](https://crates.io/crates/rocket_newrelic)\n\n\nA Rocket fairing instrumenting requests using New Relic.\n\nAttach the fairing to your `Rocket` app, and any requests that include\na [`Transaction`] in their request guard will be instrumented using\nthe handler base path and name as the transaction name.\n\n## Usage\n\n**Important** - this fairing still requires the [New Relic daemon] to be run\nalongside your app in some way, and the underlying [newrelic] and\n[newrelic-sys] crates have some additional build requirements. Make sure these\nare met when trying to use this crate.\n\nCrucially the `libnewrelic` C SDK requires a few functions not provided by musl\n(at least `qsort_r` and `backtrace`), so this won't (currently) build against\nmusl.\n\n---\n\nAdd the crate to your Cargo.toml:\n\n```toml\n[dependencies]\nrocket_newrelic = { git = \"https://github.com/sd2k/rocket_newrelic\" }\n```\n\nThen add a `\u0026Transaction` request guard to any handlers you\nwish to instrument:\n\n```rust\nuse rocket_newrelic::Transaction;\n\n#[get(\"/user/me\")]\npub fn get_me(_transaction: \u0026Transaction) -\u003e \u0026'static str {\n    \"It's me!\"\n}\n```\n\nFinally, attach the fairing to your `Rocket` app:\n\n```rust\nuse rocket_newrelic::NewRelic;\n\nfn main() -\u003e {\n    let newrelic = NewRelic::new(\"MY_APP_NAME\", \"MY_LICENSE_KEY\")\n        .expect(\"Could not register with New Relic\");\n    rocket::ignite()\n        .manage(newrelic)\n        .mount(\"/root\", routes![get_me])\n        .launch();\n}\n```\n\nIn the above example we'd then be able to see these transactions under\n`/root/get_me`.\n\n## Advanced usage\n\nThe [`Transaction`] object used in the request guard provides a few methods\nto allow further instrumentation, such as custom attributes and transaction\nsegments. The below example demonstrates some of this functionality; see the\nmethods of `Transaction` for more details.\n\n```rust\n#![feature(proc_macro_hygiene, decl_macro)]\n\n#[macro_use]\nextern crate rocket;\n\nuse newrelic::{Datastore, ExternalParamsBuilder};\nuse rocket_contrib::json::Json;\nuse rocket_newrelic::{NewRelic, Transaction};\nuse serde_json::Value;\n\nstruct User;\n\n// This would normally connect to a database and perhaps return some data.\nfn insert_into_db(_: \u0026Json\u003cValue\u003e) -\u003e Result\u003cUser, ()\u003e {\n    Ok(User {})\n}\n\n#[post(\"/users\", data = \"\u003cuser\u003e\")]\nfn create_user(transaction: \u0026Transaction, user: Json\u003cValue\u003e) {\n    // Add attributes to a transaction\n    if let Some(Value::String(name)) = user.get(\"name\") {\n        transaction.add_attribute(\"user name\", name);\n    }\n    if let Some(age) = user.get(\"age\").and_then(|a| a.as_i64()) {\n        transaction.add_attribute(\"user age\", age);\n    }\n\n    // Executing a query in a datastore segment\n    let query = \"INSERT INTO users VALUES (%s, %s);\";\n    match transaction.datastore_segment(Datastore::Postgres, \"users\", \"insert\", query, |_| {\n        insert_into_db(\u0026user)\n    }) {\n        Ok(_) =\u003e println!(\"Created user\"),\n        Err(_) =\u003e println!(\"Could not create user\"),\n    }\n\n    // Doing expensive operations in a custom segment\n    let _expensive_value: Result\u003creqwest::blocking::Response, reqwest::Error\u003e =\n        transaction.custom_segment(\"process user\", \"process\", |s| {\n            // Nesting an external segment within the custom segment\n            let url = \"https://logging-thing\";\n            let external_params = ExternalParamsBuilder::new(url)\n                .procedure(\"set\")\n                .library(\"reqwest\")\n                .build()\n                .unwrap();\n            s.external_nested(\u0026external_params, |_| {\n                reqwest::blocking::Client::new().post(url).send()\n            })\n        });\n}\n\nfn main() {\n    let newrelic = NewRelic::from_env();\n    rocket::ignite()\n        .manage(newrelic)\n        .mount(\"/\", routes![create_user])\n        .launch();\n}\n```\n\n### Diesel queries\n\nWith the `diesel` feature enabled it's possible to pass a Diesel query,\nalong with a `\u0026Connection`, into the `diesel_segment_load` and\n`diesel_segment_first` methods of a [`Transaction`]. This will log the SQL query\nand return either all results, or the first result, respectively.\n\n[Rocket]: rocket::Rocket\n[Transaction]: crate::Transaction\n[newrelic]: https://github.com/sd2k/newrelic\n[newrelic-sys]: https://github.com/sd2k/newrelic-sys\n[New Relic daemon]: https://docs.newrelic.com/docs/agents/c-sdk/get-started/introduction-c-sdk#architecture\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsd2k%2Frocket_newrelic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsd2k%2Frocket_newrelic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsd2k%2Frocket_newrelic/lists"}