Skip to content

Commit

Permalink
Init tracer using .env (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
stano45 authored Jul 9, 2023
1 parent 37648d4 commit 830669a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 14 deletions.
3 changes: 1 addition & 2 deletions src/generator/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ macro_rules! cargo_command {
/// checks if project with name already exists, if yes asks for permission to overwrite
pub fn check_for_overwrite(output_path: &Path, project_title: &str) {
if output_path.exists() {
let warn_message = format!("A project with the name {} already exists in the current directory, do you want to overwrite the existing project? \nWARNING: This will delete all files in the directory and all applied. \nType 'y' to continue or anything else to exit.",project_title);
println!("{}", warn_message);
println!("A project with the name {} already exists in the current directory, do you want to overwrite the existing project? \nWARNING: This will delete all files in the directory and all applied. \nType 'y' to continue or anything else to exit.", project_title);
let mut input = String::new();
match std::io::stdin().read_line(&mut input) {
Ok(_) => {
Expand Down
1 change: 0 additions & 1 deletion src/generator/template_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub static TEMPLATE_FUNCTIONS: &[(&str, Func)] = &[
pub fn key_exists(
args: &[gtmpl_value::Value],
) -> Result<gtmpl_value::Value, gtmpl_value::FuncError> {
println!("{:?}", args);
if args.is_empty() {
return Err(gtmpl_value::FuncError::AtLeastXArgs(
"Need at least 1 arg for key exists".to_string(),
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fn main() {
"src/utils/streams.go",
"src/utils/common.go",
"src/config/mod.go",
"src/tracing/mod.go",
]
.into_iter(),
);
Expand Down
1 change: 1 addition & 0 deletions templates/.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ SERVICE_PORT = "http://localhost:8080"
SERVER_URL = "{{ .server.url }}"
LOG_LEVEL = "DEBUG"
OPA_RULES= "path/to/admin/policy"
TRACING_ENABLED = false

################Channel wise Config################
{{ range .subscribe_channels }}
Expand Down
2 changes: 2 additions & 0 deletions templates/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Open the documentation with the following command:
## Tracing
The generated microservice uses OpenTelemetry for tracing. Each handler function is wrapped in a span, which can be modified to fit your tracing needs.

Enable the tracer in the `.env` file by setting `TRACING_ENABLED = true`.

The default exporter is the Jaeger exporter. The default configuration is set to export to a Jaeger instance running on `localhost:6831`.

Jaeger can be started in Docker using the following command:
Expand Down
25 changes: 14 additions & 11 deletions templates/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,21 @@ use async_nats::jetstream::{self};
use std::{collections::HashMap};
use dotenv::dotenv;
mod config;
use opentelemetry::global;
use opentelemetry::trace::Tracer;
mod tracing;


#[tokio::main]
async fn main() -> Result<(), async_nats::Error> {
// Initialize Jaeger Tracer
global::set_text_map_propagator(opentelemetry_jaeger::Propagator::new());
let tracer = opentelemetry_jaeger::new_agent_pipeline()
.with_service_name("{{ .title}}")
.install_batch(opentelemetry::runtime::Tokio)
.expect("Failed to initialize Jaeger Tracer");

let env: HashMap<String,String> = config::get_env();
let args = cli::Args::parse();

let tracing_enabled: bool = env.get("TRACING_ENABLED").unwrap().parse().unwrap();
if (tracing_enabled) {
// Initialize Jaeger Tracer
let tracer = tracing::init_jaeger_tracer("{{ .title}}");
}

let client = async_nats::connect(env.get("SERVER_URL").unwrap()).await?;
handle_cli(&client, &args.command, &args.message).await?;

{{ range .publish_channels }}
{{ if (index . 1).original_operation.bindings }}
Expand All @@ -45,6 +42,8 @@ async fn main() -> Result<(), async_nats::Error> {
{{end}}
{{end}}

handle_cli(&client, &args.command, &args.message).await?;


tokio::join!(
{{ range .subscribe_channels }}
Expand All @@ -65,7 +64,11 @@ async fn main() -> Result<(), async_nats::Error> {
{{ end }}
{{ end }}
);
opentelemetry::global::shutdown_tracer_provider();

if (tracing_enabled) {
// Shutdown Jaeger Tracer
tracing::shutdown_tracer_provider();
}
println!("fin");
Ok(())
}
15 changes: 15 additions & 0 deletions templates/src/tracing/mod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use opentelemetry::global;
use opentelemetry::trace::Tracer;


pub fn init_jaeger_tracer(service_name: &str) {
global::set_text_map_propagator(opentelemetry_jaeger::Propagator::new());
let tracer = opentelemetry_jaeger::new_agent_pipeline()
.with_service_name(service_name)
.install_batch(opentelemetry::runtime::Tokio)
.expect("Failed to initialize Jaeger Tracer");
}

pub fn shutdown_tracer_provider() {
opentelemetry::global::shutdown_tracer_provider();
}

0 comments on commit 830669a

Please sign in to comment.