Skip to content

Commit b3b6ad6

Browse files
authored
Merge pull request #19456 from rpastrana/HPCC-33298-JTrace-Sampling
HPCC-33298 JTrace support sampling configuration Reviewed-by: Jake Smith <[email protected]> Reviewed-by: Gavin Halliday <[email protected]> Merged-by: Gavin Halliday <[email protected]>
2 parents dc1142c + 2bc7186 commit b3b6ad6

File tree

5 files changed

+102
-5
lines changed

5 files changed

+102
-5
lines changed

helm/examples/tracing/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ All configuration options detailed here are part of the HPCC Systems Helm chart,
1313
- optAlwaysCreateTraceIds - If true components generate trace/span ids if none are provided by the remote caller.
1414
- enableDefaultLogExporter - If true, creates a trace exporter outputting to the log using the default options
1515
- enableOTELDebugLogging - If true, OTel library logging level set to debug, otherwise warning
16+
- sampling - Defines head sampling strategy. Decision to sample or drop a span or trace is not made by inspecting the trace as a whole. https://opentelemetry.io/docs/concepts/sampling/
17+
- type "AlwaysOff" | "AlwaysOn" | "Ratio"
18+
- ratio - Required if Ratio sampling type enabled. Represents the ratio of trace/spans to sample Must be a numeric value betwen 0.0 and 1.0.
19+
- parentBased - Optional boolean. Determines if the sampling policy honors the remote root span sampled flag
1620
- resourceAttributes: - Defines OTel specific resource attribute configuration values
1721
which are appended to the runtime OTEL_RESOURCE_ATTRIBUTES. See OTel doc: https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#general-sdk-configuration
1822
- deploymentEnvironment - Defines deployment.environment, which is used to specify

helm/examples/tracing/baremetal-otlp-http-localhost-sample.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<exporters consoleDebug="true" endpoint="http://localhost:4318/v1/traces" type="OTLP-HTTP">
66
<batch enabled="false" maxQueueSize="4095" scheduledDelayMillis="6001" maxExportBatchSize="511"/>
77
</exporters>
8+
<sampling type="Ratio" ratio="0.1" parentBased="true"/>
89
</tracing>
910
</Software>
1011
</Environment>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
global:
2+
tracing:
3+
sampling:
4+
type: Ratio #Head sampling based on simple ratio
5+
ratio: 0.1 #only sample 10% of traces/spans
6+
parentBased: true #Sampling strategy based on parent's sampling
7+
exporters:
8+
- type: OTLP-HTTP
9+
endpoint: "localhost:4318/v1/traces"
10+
consoleDebug: true

helm/hpcc/values.schema.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,25 @@
11711171
"type": "boolean",
11721172
"description": "If true, sets OTEL library logging to debug level (otherwise set to warning)"
11731173
},
1174+
"sampling": {
1175+
"type": "object",
1176+
"properties": {
1177+
"type": {
1178+
"type": "string",
1179+
"enum": ["AlwaysOff", "AlwaysOn", "Ratio"],
1180+
"description": "Name of the Head Sampling type AlwaysOff|AlwaysOn|Ratio"
1181+
},
1182+
"ratio" : {
1183+
"type": "number",
1184+
"description": "Required if sampling type set to Ratio - Represents the sampling ratio value."
1185+
},
1186+
"parentBased" : {
1187+
"type": "boolean",
1188+
"description": "Optional - Determines if the sampling policy honors the remote root span sampled flag."
1189+
},
1190+
"additionalProperties": false
1191+
}
1192+
},
11741193
"exporters": {
11751194
"type": "array",
11761195
"description": "List of trace exporters",

system/jlib/jtrace.cpp

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
#include "opentelemetry/sdk/trace/tracer_context_factory.h" //opentelemetry::sdk::trace::TracerContextFactory::Create(std::move(processors));
2424
#include "opentelemetry/sdk/trace/simple_processor_factory.h"
2525
#include "opentelemetry/sdk/trace/batch_span_processor_factory.h"
26+
#include <opentelemetry/sdk/trace/samplers/always_on_factory.h>
27+
#include <opentelemetry/sdk/trace/samplers/always_off_factory.h>
28+
#include <opentelemetry/sdk/trace/samplers/trace_id_ratio_factory.h>
29+
#include <opentelemetry/sdk/trace/samplers/trace_id_ratio.h>
30+
#include <opentelemetry/sdk/trace/samplers/always_off.h>
31+
#include <opentelemetry/sdk/trace/samplers/parent.h>
2632
#include "opentelemetry/exporters/ostream/span_exporter_factory.h"// auto exporter = opentelemetry::exporter::trace::OStreamSpanExporterFactory::Create();
2733
#include "opentelemetry/exporters/ostream/common_utils.h"
2834
#include "opentelemetry/exporters/memory/in_memory_span_exporter_factory.h"
@@ -1382,6 +1388,50 @@ std::unique_ptr<opentelemetry::sdk::trace::SpanProcessor> CTraceManager::createP
13821388
return opentelemetry::sdk::trace::SimpleSpanProcessorFactory::Create(std::move(exporter));
13831389
}
13841390

1391+
static std::unique_ptr<opentelemetry::sdk::trace::Sampler> createSampler(IPropertyTree * samplerTree)
1392+
{
1393+
std::unique_ptr<opentelemetry::sdk::trace::Sampler> sampler;
1394+
1395+
if (samplerTree)
1396+
{
1397+
const char * samplerType = samplerTree->queryProp("@type");
1398+
if (!isEmptyString(samplerType))
1399+
{
1400+
if (streq(samplerType, "AlwaysOff"))
1401+
{
1402+
sampler.reset(new opentelemetry::sdk::trace::AlwaysOffSampler());
1403+
}
1404+
else if (streq(samplerType, "AlwaysOn"))
1405+
{
1406+
sampler.reset(new opentelemetry::sdk::trace::AlwaysOnSampler());
1407+
}
1408+
else if (streq(samplerType,"Ratio"))
1409+
{
1410+
double ratio = samplerTree->getPropReal("@ratio", -1.0);
1411+
if (ratio >= 0.0 && ratio <= 1.0)
1412+
{
1413+
sampler.reset(new opentelemetry::sdk::trace::TraceIdRatioBasedSampler(ratio));
1414+
}
1415+
else
1416+
{
1417+
OERRLOG("JTrace invalid ratio sampling configuration. Ratio must be between 0.0 and 1.0");
1418+
}
1419+
}
1420+
else
1421+
{
1422+
WARNLOG("JTrace initialization: Invalid sampling type configured: '%s'", samplerType);
1423+
}
1424+
1425+
if (sampler && samplerTree->getPropBool("@parentBased", true))
1426+
{
1427+
return std::unique_ptr<opentelemetry::sdk::trace::ParentBasedSampler>(new opentelemetry::sdk::trace::ParentBasedSampler(std::move(sampler)));
1428+
}
1429+
}
1430+
}
1431+
1432+
return sampler;
1433+
}
1434+
13851435
void CTraceManager::initTracerProviderAndGlobalInternals(const IPropertyTree * traceConfig)
13861436
{
13871437
/*
@@ -1399,6 +1449,7 @@ void CTraceManager::initTracerProviderAndGlobalInternals(const IPropertyTree * t
13991449
};
14001450

14011451
std::vector<std::unique_ptr<opentelemetry::sdk::trace::SpanProcessor>> processors;
1452+
std::unique_ptr<opentelemetry::sdk::trace::Sampler> sampler;
14021453

14031454
bool enableOTELDebugLogging = false;
14041455

@@ -1407,6 +1458,8 @@ void CTraceManager::initTracerProviderAndGlobalInternals(const IPropertyTree * t
14071458
bool enableDefaultLogExporter = isDebugBuild();
14081459
if (traceConfig)
14091460
{
1461+
sampler = createSampler(traceConfig->queryPropTree("sampling"));
1462+
14101463
IPropertyTree * resourceAttributesTree = traceConfig->queryPropTree("resourceAttributes");
14111464
if (resourceAttributesTree)
14121465
{
@@ -1433,6 +1486,11 @@ void CTraceManager::initTracerProviderAndGlobalInternals(const IPropertyTree * t
14331486
enableDefaultLogExporter = traceConfig->getPropBool("@enableDefaultLogExporter", enableDefaultLogExporter);
14341487
}
14351488

1489+
if (!sampler)
1490+
{
1491+
sampler = std::unique_ptr<opentelemetry::sdk::trace::AlwaysOnSampler>(new opentelemetry::sdk::trace::AlwaysOnSampler);
1492+
}
1493+
14361494
if (enableDefaultLogExporter)
14371495
{
14381496
//Simple option to create logging to the log file - primarily to aid developers.
@@ -1442,9 +1500,9 @@ void CTraceManager::initTracerProviderAndGlobalInternals(const IPropertyTree * t
14421500

14431501
auto jtraceResource = opentelemetry::sdk::resource::Resource::Create(resourceAtts);
14441502

1445-
// Default is an always-on sampler.
14461503
std::unique_ptr<opentelemetry::sdk::trace::TracerContext> context =
1447-
opentelemetry::sdk::trace::TracerContextFactory::Create(std::move(processors), jtraceResource);
1504+
opentelemetry::sdk::trace::TracerContextFactory::Create(std::move(processors), jtraceResource, std::move(sampler));
1505+
14481506
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
14491507
opentelemetry::sdk::trace::TracerProviderFactory::Create(std::move(context));
14501508

@@ -1468,9 +1526,14 @@ Expected Configuration format:
14681526
disabled: true #optional - disable OTel tracing
14691527
alwaysCreateGlobalIds : false #optional - should global ids always be created?
14701528
alwaysCreateTraceIds #optional - should trace ids always be created?
1471-
exporters: #optional - Controls how trace data is exported/reported
1472-
- type: OTLP #OS|OTLP|Prometheus|JLOG
1473-
endpoint: "localhost:4317" #exporter specific key/value pairs
1529+
sampling: #optional - controls how traces are either suppressed or sampled
1530+
type: #"AlwaysOff" | "AlwaysOn" | "Ratio"
1531+
ratio: #optional - Required if Ratio sampling used, represents the sampling ratio [0.0 - 1.0]
1532+
parentBased: #optional - Sets OTel's parentbased sampling option as defined here:
1533+
# https://opentelemetry.io/docs/languages/erlang/sampling/#parentbasedsampler
1534+
exporters: #optional - Controls how trace data is exported/reported
1535+
- type: OTLP #OS|OTLP|Prometheus|JLOG
1536+
endpoint: "localhost:4317" #exporter specific key/value pairs
14741537
useSslCredentials: true
14751538
sslCredentialsCACcert: "ssl-certificate"
14761539
batch: #optional - Controls span processing style

0 commit comments

Comments
 (0)