Skip to content
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
143 changes: 143 additions & 0 deletions test/tendpoint.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
classdef tendpoint < matlab.unittest.TestCase
% tests for setting endpoint in the exporter

% Copyright 2023 The MathWorks, Inc.

properties
OtelConfigFile
JsonFile
PidFile
OtelcolName
Otelcol
ListPid
ReadPidList
ExtractPid
Sigint
Sigterm
end

methods (TestClassSetup)
function setupOnce(testCase)
commonSetupOnce(testCase);
end
end

methods (TestMethodSetup)
function setup(testCase)
commonSetup(testCase, "nondefault_endpoint.yml");
end
end

methods (TestMethodTeardown)
function teardown(testCase)
commonTeardown(testCase);
end
end

methods (Test)
function testNondefaultEndpoint(testCase)
% testNondefaultEndpoint: using an alternative endpoint

testCase.assumeTrue(logical(exist("opentelemetry.exporters.otlp.OtlpHttpSpanExporter", "class")), ...
"Otlp HTTP exporter must be installed.");

tracername = "foo";
spanname = "bar";

exp = opentelemetry.exporters.otlp.OtlpHttpSpanExporter(...
"Endpoint", "http://localhost:9921/v1/traces");
processor = opentelemetry.sdk.trace.SimpleSpanProcessor(exp);
tp = opentelemetry.sdk.trace.TracerProvider(processor);
tr = getTracer(tp, tracername);
sp = startSpan(tr, spanname);
pause(1);
endSpan(sp);

% perform test comparisons
results = readJsonResults(testCase);
results = results{1};

% check span and tracer names
verifyEqual(testCase, string(results.resourceSpans.scopeSpans.spans.name), spanname);
verifyEqual(testCase, string(results.resourceSpans.scopeSpans.scope.name), tracername);
end

function testNondefaultGrpcEndpoint(testCase)
% testNondefaultGrpcEndpoint: using an alternative endpoint

testCase.assumeTrue(logical(exist("opentelemetry.exporters.otlp.OtlpGrpcSpanExporter", "class")), ...
"Otlp gRPC exporter must be installed.");

tracername = "foo";
spanname = "bar";

exp = opentelemetry.exporters.otlp.OtlpGrpcSpanExporter(...
"Endpoint", "http://localhost:9922");
processor = opentelemetry.sdk.trace.SimpleSpanProcessor(exp);
tp = opentelemetry.sdk.trace.TracerProvider(processor);
tr = getTracer(tp, tracername);
sp = startSpan(tr, spanname);
pause(1);
endSpan(sp);

% perform test comparisons
results = readJsonResults(testCase);
results = results{1};

% check span and tracer names
verifyEqual(testCase, string(results.resourceSpans.scopeSpans.spans.name), spanname);
verifyEqual(testCase, string(results.resourceSpans.scopeSpans.scope.name), tracername);
end

function NondefaultMetricsEndpoint(testCase)
% testNondefaultMetricsEndpoint: using an alternative endpoint
testCase.assumeTrue(logical(exist("opentelemetry.exporters.otlp.OtlpHttpMetricExporter", "class")), ...
"Otlp HTTP exporter must be installed.");

exp = opentelemetry.exporters.otlp.OtlpHttpMetricExporter(...
"Endpoint", "http://localhost:9921/v1/metrics");
reader = opentelemetry.sdk.metrics.PeriodicExportingMetricReader(...
exp, "Interval", seconds(2), "Timeout", seconds(1));
p = opentelemetry.sdk.metrics.MeterProvider(reader);
mt = p.getMeter("foo");
ct = mt.createCounter("bar");

val = 4;
ct.add(val);
pause(2.5);

% fetch result
clear p;
results = readJsonResults(testCase);

% verify counter value
verifyEqual(testCase, results{end}.resourceMetrics.scopeMetrics.metrics.sum.dataPoints.asDouble, val);
end

function NondefaultGrpcMetricsEndpoint(testCase)
% testNondefaultGrpcMetricsEndpoint: using an alternative endpoint
testCase.assumeTrue(logical(exist("opentelemetry.exporters.otlp.OtlpGrpcMetricExporter", "class")), ...
"Otlp gRPC exporter must be installed.");

exp = opentelemetry.exporters.otlp.OtlpGrpcMetricExporter(...
"Endpoint", "http://localhost:9922");
reader = opentelemetry.sdk.metrics.PeriodicExportingMetricReader(...
exp, "Interval", seconds(2), "Timeout", seconds(1));
p = opentelemetry.sdk.metrics.MeterProvider(reader);
mt = p.getMeter("foo");
ct = mt.createCounter("bar");

val = 8;
ct.add(val);
pause(2.5);

% fetch result
clear p;
results = readJsonResults(testCase);

% verify counter value
verifyEqual(testCase, results{end}.resourceMetrics.scopeMetrics.metrics.sum.dataPoints.asDouble, val);
end

end
end
82 changes: 8 additions & 74 deletions test/ttrace_sdk.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,75 +22,21 @@ function setupOnce(testCase)
end
end

methods (TestMethodSetup)
function setup(testCase)
commonSetup(testCase);
end
end

methods (TestMethodTeardown)
function teardown(testCase)
commonTeardown(testCase);
end
end

methods (Test)
function testNondefaultEndpoint(testCase)
% testNondefaultEndpoint: using an alternative endpoint

testCase.assumeTrue(logical(exist("opentelemetry.exporters.otlp.OtlpHttpSpanExporter", "class")), ...
"Otlp HTTP exporter must be installed.");

commonSetup(testCase, "nondefault_endpoint.yml")

tracername = "foo";
spanname = "bar";

exp = opentelemetry.exporters.otlp.OtlpHttpSpanExporter(...
"Endpoint", "http://localhost:9921/v1/traces");
processor = opentelemetry.sdk.trace.SimpleSpanProcessor(exp);
tp = opentelemetry.sdk.trace.TracerProvider(processor);
tr = getTracer(tp, tracername);
sp = startSpan(tr, spanname);
pause(1);
endSpan(sp);

% perform test comparisons
results = readJsonResults(testCase);
results = results{1};

% check span and tracer names
verifyEqual(testCase, string(results.resourceSpans.scopeSpans.spans.name), spanname);
verifyEqual(testCase, string(results.resourceSpans.scopeSpans.scope.name), tracername);
end

function testNondefaultGrpcEndpoint(testCase)
% testNondefaultEndpoint: using an alternative endpoint

testCase.assumeTrue(logical(exist("opentelemetry.exporters.otlp.OtlpGrpcSpanExporter", "class")), ...
"Otlp gRPC exporter must be installed.");

commonSetup(testCase, "nondefault_endpoint.yml")

tracername = "foo";
spanname = "bar";

exp = opentelemetry.exporters.otlp.OtlpGrpcSpanExporter(...
"Endpoint", "http://localhost:9922");
processor = opentelemetry.sdk.trace.SimpleSpanProcessor(exp);
tp = opentelemetry.sdk.trace.TracerProvider(processor);
tr = getTracer(tp, tracername);
sp = startSpan(tr, spanname);
pause(1);
endSpan(sp);

% perform test comparisons
results = readJsonResults(testCase);
results = results{1};

% check span and tracer names
verifyEqual(testCase, string(results.resourceSpans.scopeSpans.spans.name), spanname);
verifyEqual(testCase, string(results.resourceSpans.scopeSpans.scope.name), tracername);
end

methods (Test)
function testAlwaysOffSampler(testCase)
% testAlwaysOffSampler: should not produce any spans
commonSetup(testCase)

tp = opentelemetry.sdk.trace.TracerProvider( ...
opentelemetry.sdk.trace.SimpleSpanProcessor, ...
"Sampler", opentelemetry.sdk.trace.AlwaysOffSampler);
Expand All @@ -106,8 +52,6 @@ function testAlwaysOffSampler(testCase)

function testAlwaysOnSampler(testCase)
% testAlwaysOnSampler: should produce all spans
commonSetup(testCase)

tracername = "foo";
spanname = "bar";

Expand All @@ -130,8 +74,6 @@ function testAlwaysOnSampler(testCase)

function testTraceIdRatioBasedSampler(testCase)
% testTraceIdRatioBasedSampler: filter spans based on a ratio
commonSetup(testCase)

s = opentelemetry.sdk.trace.TraceIdRatioBasedSampler(0); % equivalent to always off

tracername = "mytracer";
Expand Down Expand Up @@ -187,8 +129,6 @@ function testTraceIdRatioBasedSampler(testCase)
function testCustomResource(testCase)
% testCustomResource: check custom resources are included in
% emitted spans
commonSetup(testCase)

customkeys = ["foo" "bar"];
customvalues = [1 5];
tp = opentelemetry.sdk.trace.TracerProvider(opentelemetry.sdk.trace.SimpleSpanProcessor, ...
Expand All @@ -213,8 +153,6 @@ function testCustomResource(testCase)
function testShutdown(testCase)
% testShutdown: shutdown method should stop exporting
% of spans
commonSetup(testCase)

tp = opentelemetry.sdk.trace.TracerProvider();
tr = getTracer(tp, "foo");

Expand All @@ -238,8 +176,6 @@ function testShutdown(testCase)

function testCleanupSdk(testCase)
% testCleanupSdk: shutdown an SDK tracer provider through the Cleanup class
commonSetup(testCase)

tp = opentelemetry.sdk.trace.TracerProvider();
tr = getTracer(tp, "foo");

Expand All @@ -262,9 +198,7 @@ function testCleanupSdk(testCase)
end

function testCleanupApi(testCase)
% testCleanupApi: shutdown an API tracer provider through the Cleanup class
commonSetup(testCase)

% testCleanupApi: shutdown an API tracer provider through the Cleanup class
tp = opentelemetry.sdk.trace.TracerProvider();
setTracerProvider(tp);
clear("tp");
Expand Down