Skip to content
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

Tracing #87

Merged
merged 3 commits into from
Jul 8, 2023
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
14 changes: 14 additions & 0 deletions templates/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,17 @@ Open the documentation with the following command:
```
cargo doc --no-deps --open
```

## 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.

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:
```
docker run -d -p6831:6831/udp -p6832:6832/udp -p16686:16686 -p14268:14268 jaegertracing/all-in-one:latest
```

Access the Jaeger UI at http://localhost:16686 and look for your service name in the dropdown menu.

For more information, visit the [Jaeger website](https://www.jaegertracing.io/docs/getting-started/).
3 changes: 2 additions & 1 deletion templates/dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ serde_json = "1.0.97"
tokio = { version = "1.28.2", features = ["full"] }
dotenv = "0.15.0"
clap = {version = "4.3.0", features = ["derive"]}

opentelemetry = { version = "*", features = ["rt-tokio"] }
opentelemetry-jaeger = { version = "*", features = ["rt-tokio", "isahc_collector_client"] }
11 changes: 10 additions & 1 deletion templates/src/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use async_nats::{Client, Message, jetstream};
use async_nats::jetstream::Context;
use crate::{publish_message,stream_publish_message,model::*,config::*};
use std::time;

use opentelemetry::global;
use opentelemetry::trace::Tracer;


{{ range .publish_channels }}
Expand All @@ -18,6 +19,8 @@ use std::time;
{{end}}
{{if $isStream}}
pub fn stream_handler_{{ (index . 1).unique_id }}(message: jetstream::Message, client: &Client) {
let tracer = global::tracer("stream_handler_{{ (index . 1).unique_id }}");
let _span = tracer.start("{{ (index . 1).unique_id }}_stream_handler");
{{ range (index . 1).messages }}
{{ if .payload}}
match serde_json::from_slice::<{{ .payload.struct_reference }}>(&message.message.payload.as_ref()) {
Expand Down Expand Up @@ -46,6 +49,8 @@ use std::time;
}
{{else}}
pub async fn handler_{{ (index . 1).unique_id }}(message: Message, client: &Client) {
let tracer = global::tracer("handler_{{ (index . 1).unique_id }}");
let _span = tracer.start("{{ (index . 1).unique_id }}_handler");
{{ range (index . 1).messages }}
{{ if .payload}}
match serde_json::from_slice::<{{ .payload.struct_reference }}>(&message.payload.as_ref()) {
Expand Down Expand Up @@ -93,6 +98,8 @@ use std::time;
{{ if $isStream }}
{{ range (index . 1).messages }}
pub async fn stream_producer_{{ (index $channel 1).unique_id }}(context_stream: &Context, payload : {{ if .payload}} {{ .payload.struct_reference }} {{ else }} () {{ end }}) { //context instead of client
let tracer = global::tracer("{{ (index $channel 1).unique_id }}_stream_producer");
let _span = tracer.start("stream_producer_{{ (index $channel 1).unique_id }}");
let subject = get_env().get("{{ (index $channel 1).unique_id }}_SUBJECT").unwrap().clone();
{{ if .payload }}
let payload = match serde_json::to_string(&payload) {
Expand All @@ -111,6 +118,8 @@ use std::time;
{{ else }}
{{ range (index . 1).messages }}
pub async fn producer_{{ (index $channel 1).unique_id }}(client: &Client, payload: {{ if .payload }} {{.payload.struct_reference}} {{else}} () {{end}}) {
let tracer = global::tracer("{{ (index $channel 1).unique_id }}_producer");
let _span = tracer.start("producer_{{ (index $channel 1).unique_id }}");
let subject = get_env().get("{{ (index $channel 1).unique_id }}_SUBJECT").unwrap().clone();
{{ if .payload }}
let payload = match serde_json::to_string(&payload) {
Expand Down
11 changes: 10 additions & 1 deletion templates/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ use async_nats::jetstream::{self};
use std::{collections::HashMap};
use dotenv::dotenv;
mod config;
use opentelemetry::global;
use opentelemetry::trace::Tracer;


#[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();

Expand Down Expand Up @@ -56,7 +65,7 @@ async fn main() -> Result<(), async_nats::Error> {
{{ end }}
{{ end }}
);

opentelemetry::global::shutdown_tracer_provider();
println!("fin");
Ok(())
}