Tracing logs to loki

  1. I have an instance of grafana loki running locally, relevant config being:
server:
  http_listen_port: 3100
  grpc_listen_port: 9096

ruler:
  alertmanager_url: http://localhost:9093

So now my question is: how do I shovel logs from Rust to loki? Is there a way to do it using the more popular https://crates.io/crates/tracing-opentelemetry or do I use https://crates.io/crates/tracing-loki ?

Thanks!

oh my, self hosting those cloud services is a deep deep rabbit hole to go. it would be much easier to just use their cloud services, just grab the API key and copy the endpoint URL, you are good to go, anyway, I digress.

I'd suggest use tracing-loki crate directly. loki is the log storage service from grafana, although this page lists grafana as a vendor supporting native OTLP, from what I can tell, native OTLP is supported on the grafana cloud platform as an integrated whole, but loki itself uses an prometheus compatible API.

opentelemetry defines OTLP as a canonical interchange protocol, but not every backend speaks OTLP natively, some need a translation layer in between, called exporters(OTLP to vendor specific protocol) and/or receivers(vendor specific protocol to OTLP). according to this list, loki has both a receiver and an exporter, so the translation is bidirectional. but why bother the indirection if you can use the tracing-loki crate directly? however, if you are targeting an backend which speaks OTLP natively, opentelemetry could worth a try.

back to the loki crate, I tried the example code in the crate documentation with their sample set up using docker compose, it works, but I feel overwhelmed by the grafana UI. following the guide, I managed to query the logs using the label {lang=rust}.

I basically copy pasted the code from the document. note: at least one label is required to be able to do queries on the logs, and the X-Scope-OrgID header is mentioned in the crate doc, I just looked at the docker config files and find tenant1 is the correct value for the example setup. I don't want to spend time to read all the documents on loki.

	let fmt_layer = fmt::layer().with_filter(filter::LevelFilter::from_level(tr::Level::INFO));
	let (loki_layer, loki_task) = tracing_loki::builder()
		.label("lang", "rust")
		.unwrap()
		.http_header("X-Scope-OrgID", "tenant1")
		.unwrap()
		.build_url("http://127.0.0.1:3100".try_into().unwrap())
		.unwrap();
	registry().with(loki_layer).with(fmt_layer).init();
	tokio::spawn(loki_task);
1 Like

Yeah, the sample code from the tracing-loki readme worked verbatim. The hardest parts were installing / querying loki.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.