Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(trace): jaeger remote sampler (#797)
- Loading branch information
Showing
18 changed files
with
966 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "jaeger-remote-sampler" | ||
version = "0.1.0" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
opentelemetry-sdk = { path = "../../opentelemetry-sdk", features = ["rt-tokio", "jaeger_remote_sampler"] } | ||
opentelemetry-api = { path = "../../opentelemetry-api" } | ||
opentelemetry-http = { path = "../../opentelemetry-http", features = ["reqwest"] } | ||
reqwest = "0.11.10" | ||
tokio = { version = "1.18", features = ["macros", "rt-multi-thread"] } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Jaeger remote sampler | ||
|
||
When services generate too many spans. We need to sample some spans to save cost and speed up the queries. | ||
|
||
Adaptive sampling works in the Jaeger collector by observing the spans received from services and recalculating sampling | ||
probabilities for each service/endpoint combination to ensure that the volume is relatively constant. | ||
|
||
## Setup | ||
|
||
Start a jaeger collector and an opentelemetry collector locally using docker | ||
|
||
``` | ||
docker-comopse run -d | ||
``` | ||
|
||
It will allow you to | ||
|
||
- query sampling strategies from jaeger collect at port 5578. `http://localhost:5778/sampling?service=foo` | ||
- query sampling strategies from opentelemetry collector at port 5579. `http://localhost:5779/sampling?service=foo` | ||
|
||
## Run the example | ||
|
||
After start the jaeger remote sampling server successfully. We can run | ||
|
||
`cargo run` | ||
|
||
command to start the example, you should see something like only one span is printed out. | ||
|
||
Looking at the example, you will notice we use `AlwaysOff` as our default sampler. It means before the SDK get the sampling strategy from remote server, no span will be sampled. | ||
|
||
Once the SDK fetched the remote strategy, we will start a probability sampler internally. In this case, we set the probability to 1.0 for all spans. This is defined by | ||
|
||
``` | ||
"service": "foo", | ||
"type": "probabilistic", | ||
"param": 1, | ||
``` | ||
|
||
Feel free to tune the `param` and see if the probability of sampling changes. | ||
|
||
## Strategies | ||
|
||
The sampling strategies is defined in `srategies.json` files. It defines two set of strategies. | ||
|
||
The first strategy is returned for `foo` service. The second strategy is catch all default strategy for all other | ||
services. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
version: "3" | ||
services: | ||
|
||
# jaeger collector | ||
jaeger-all-in-one: | ||
image: jaegertracing/all-in-one:latest | ||
ports: | ||
- "16686:16686" | ||
- "14268" | ||
- "14250" | ||
- "5778:5778" | ||
container_name: jaeger-collector | ||
volumes: | ||
- ./strategies.json:/etc/jaeger/custom_strategies.json | ||
environment: | ||
- SAMPLING_STRATEGIES_FILE=/etc/jaeger/custom_strategies.json | ||
|
||
# opentelemetry collector | ||
otel-collector: | ||
image: otel/opentelemetry-collector:latest | ||
command: [ "--config=/etc/otel-collector.yaml" ] | ||
volumes: | ||
- ./otel-collector.yaml:/etc/otel-collector.yaml | ||
- ./strategies.json:/etc/strategies.json | ||
ports: | ||
- "5779:5778" # default jaeger remote sampling port |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
receivers: | ||
jaeger: | ||
protocols: | ||
grpc: | ||
remote_sampling: | ||
host_endpoint: "0.0.0.0:5778" # default port | ||
insecure: true | ||
strategy_file: "/etc/strategies.json" | ||
|
||
exporters: | ||
logging: | ||
|
||
service: | ||
pipelines: | ||
traces: | ||
receivers: [ jaeger ] | ||
exporters: [ logging ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use opentelemetry_api::global; | ||
use opentelemetry_api::trace::Tracer; | ||
use opentelemetry_sdk::export::trace::stdout::Exporter as StdoutExporter; | ||
use opentelemetry_sdk::runtime; | ||
use opentelemetry_sdk::trace::{Sampler, TracerProvider as SdkTracerProvider}; | ||
use std::time::Duration; | ||
|
||
fn setup() { | ||
let client = reqwest::Client::new(); | ||
|
||
let sampler = Sampler::jaeger_remote(runtime::Tokio, client, Sampler::AlwaysOff, "foo") | ||
.with_endpoint("http://localhost:5778/sampling") // setup jaeger remote sampler endpoint | ||
.with_update_interval(Duration::from_secs(5)) // will call jaeger sampling endpoint every 5 secs. | ||
.build() | ||
.unwrap(); | ||
|
||
let config = opentelemetry_sdk::trace::config().with_sampler(sampler); | ||
|
||
let provider = SdkTracerProvider::builder() | ||
.with_config(config) | ||
.with_simple_exporter(StdoutExporter::new(std::io::stdout(), true)) | ||
.build(); | ||
|
||
global::set_tracer_provider(provider); | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
setup(); | ||
let tracer = global::tracer("test"); | ||
|
||
{ | ||
let _not_sampled_span = tracer.start("test"); | ||
} | ||
|
||
tokio::time::sleep(Duration::from_secs(10)).await; | ||
|
||
{ | ||
let _sampled_span = tracer.start("should_record"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"service_strategies": [ | ||
{ | ||
"service": "foo", | ||
"type": "probabilistic", | ||
"param": 1, | ||
"operation_strategies": [ | ||
{ | ||
"operation": "op1", | ||
"type": "probabilistic", | ||
"param": 0.2 | ||
}, | ||
{ | ||
"operation": "op2", | ||
"type": "probabilistic", | ||
"param": 0.4 | ||
} | ||
] | ||
} | ||
], | ||
"default_strategy": { | ||
"type": "probabilistic", | ||
"param": 0.5, | ||
"operation_strategies": [ | ||
{ | ||
"operation": "/health", | ||
"type": "probabilistic", | ||
"param": 0.0 | ||
}, | ||
{ | ||
"operation": "/metrics", | ||
"type": "probabilistic", | ||
"param": 0.0 | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.