Skip to content

Commit 38d3a05

Browse files
committed
HPCC-33298 JTrace support sampling configuration
- Adds Jtrace sampler configuration - Adds OTel sampler initialization logic - Updates JTrace configuration README - Provides samples - Jlog trace/span ids suppressed if not sampled (not sure if this is wanted) Signed-off-by: Rodrigo Pastrana <[email protected]>
1 parent e86aa95 commit 38d3a05

File tree

5 files changed

+104
-2
lines changed

5 files changed

+104
-2
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+
- sampler - 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+
- argument - Optional sampler type configuration value. Currently, only supported value applies to the "Ratio" sampler type. The argument value is a string representing a numeric value betwen 0.0 and 1.0. This value represents the ratio of trace/spans to sample
19+
- parentBased - Optional boolean. Determines if the sampler 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+
<sampler type="Ratio" argument="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+
sampler:
4+
type: Ratio #Head sampling based on simple ratio
5+
argument: "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+
"sampler": {
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+
"argument" : {
1183+
"type": "string",
1184+
"description": "Optional sampler type configuration value"
1185+
},
1186+
"parentBased" : {
1187+
"type": "boolean",
1188+
"description": "Optional sets the sampling decision based on the Span’s parent, or absence of parent, to know which secondary sampler to use."
1189+
},
1190+
"additionalProperties": false
1191+
}
1192+
},
11741193
"exporters": {
11751194
"type": "array",
11761195
"description": "List of trace exporters",

system/jlib/jtrace.cpp

Lines changed: 70 additions & 2 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,56 @@ 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("@argument");
1411+
if (ratio < 0 || ratio > 1)
1412+
{
1413+
OERRLOG("JTrace invalid ratio sampler configuration. Ratio must be LE 1.0 or GE 0.0");
1414+
}
1415+
else
1416+
{
1417+
sampler.reset(new opentelemetry::sdk::trace::TraceIdRatioBasedSampler(ratio));
1418+
}
1419+
}
1420+
else
1421+
{
1422+
WARNLOG("JTrace initialization: Invalid sampler 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+
if (!sampler)
1433+
{
1434+
sampler.reset(new opentelemetry::sdk::trace::AlwaysOnSampler());
1435+
WARNLOG("JTrace sampler set to 'Always ON' by default!");
1436+
}
1437+
1438+
return sampler;
1439+
}
1440+
13851441
void CTraceManager::initTracerProviderAndGlobalInternals(const IPropertyTree * traceConfig)
13861442
{
13871443
/*
@@ -1399,6 +1455,7 @@ void CTraceManager::initTracerProviderAndGlobalInternals(const IPropertyTree * t
13991455
};
14001456

14011457
std::vector<std::unique_ptr<opentelemetry::sdk::trace::SpanProcessor>> processors;
1458+
std::unique_ptr<opentelemetry::sdk::trace::Sampler> sampler;
14021459

14031460
bool enableOTELDebugLogging = false;
14041461

@@ -1407,6 +1464,8 @@ void CTraceManager::initTracerProviderAndGlobalInternals(const IPropertyTree * t
14071464
bool enableDefaultLogExporter = isDebugBuild();
14081465
if (traceConfig)
14091466
{
1467+
sampler = createSampler(traceConfig->queryPropTree("sampler"));
1468+
14101469
IPropertyTree * resourceAttributesTree = traceConfig->queryPropTree("resourceAttributes");
14111470
if (resourceAttributesTree)
14121471
{
@@ -1433,6 +1492,11 @@ void CTraceManager::initTracerProviderAndGlobalInternals(const IPropertyTree * t
14331492
enableDefaultLogExporter = traceConfig->getPropBool("@enableDefaultLogExporter", enableDefaultLogExporter);
14341493
}
14351494

1495+
if (!sampler)
1496+
{
1497+
sampler = std::unique_ptr<opentelemetry::sdk::trace::AlwaysOnSampler>(new opentelemetry::sdk::trace::AlwaysOnSampler);
1498+
}
1499+
14361500
if (enableDefaultLogExporter)
14371501
{
14381502
//Simple option to create logging to the log file - primarily to aid developers.
@@ -1442,9 +1506,9 @@ void CTraceManager::initTracerProviderAndGlobalInternals(const IPropertyTree * t
14421506

14431507
auto jtraceResource = opentelemetry::sdk::resource::Resource::Create(resourceAtts);
14441508

1445-
// Default is an always-on sampler.
14461509
std::unique_ptr<opentelemetry::sdk::trace::TracerContext> context =
1447-
opentelemetry::sdk::trace::TracerContextFactory::Create(std::move(processors), jtraceResource);
1510+
opentelemetry::sdk::trace::TracerContextFactory::Create(std::move(processors), jtraceResource, std::move(sampler));
1511+
14481512
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
14491513
opentelemetry::sdk::trace::TracerProviderFactory::Create(std::move(context));
14501514

@@ -1468,6 +1532,10 @@ Expected Configuration format:
14681532
disabled: true #optional - disable OTel tracing
14691533
alwaysCreateGlobalIds : false #optional - should global ids always be created?
14701534
alwaysCreateTraceIds #optional - should trace ids always be created?
1535+
sampler: #optional - controls how traces are either suppressed or sampled
1536+
type: #"AlwaysOff" | "AlwaysOn" | "Ratio"
1537+
argument: #optional sampler type configuration value
1538+
parentBased: #optional sets the sampling decision based on the Span’s parent, or absence of parent, to know which secondary sampler to use.
14711539
exporters: #optional - Controls how trace data is exported/reported
14721540
- type: OTLP #OS|OTLP|Prometheus|JLOG
14731541
endpoint: "localhost:4317" #exporter specific key/value pairs

0 commit comments

Comments
 (0)