Skip to content

Commit

Permalink
download collector automatically for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanpo committed Sep 14, 2023
1 parent e2525ed commit c573ada
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
38 changes: 33 additions & 5 deletions test/commonSetupOnce.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ function commonSetupOnce(testCase)

% file definitions
otelcolroot = getenv("OPENTELEMETRY_COLLECTOR_INSTALL");
assert(~isempty(otelcolroot), "OPENTELEMETRY_COLLECTOR_INSTALL environment must be defined.")
testCase.OtelConfigFile = fullfile(fileparts(mfilename("fullpath")), ...
"otelcol_config.yml");
otelroot = getenv("OPENTELEMETRY_MATLAB_INSTALL");
assert(~isempty(otelroot), "OPENTELEMETRY_MATLAB_INSTALL environment must be defined.")
testCase.OtelRoot = otelroot;
testCase.JsonFile = "myoutput.json";
testCase.PidFile = "testoutput.txt";

Expand All @@ -19,7 +16,13 @@ function commonSetupOnce(testCase)
if ispc
testCase.ListPid = @(name)"tasklist /fi ""IMAGENAME eq " + name + ".exe""";
testCase.ReadPidList = @(file)readtable(file, "VariableNamingRule", "preserve", "NumHeaderLines", 3, "MultipleDelimsAsOne", true, "Delimiter", " ");
testCase.ExtractPid = @(table)table.Var2;
testCase.ExtractPid = @(table)table.Var2;

% variables to support downloading OpenTelemetry Collector
otelcol_arch_name = "windows_amd64";
otelcol_exe_ext = ".exe";

% windows_kill
windows_killroot = getenv("WINDOWS_KILL_INSTALL");
windows_killname = "windows-kill";
if isempty(windows_killroot)
Expand All @@ -41,6 +44,10 @@ function commonSetupOnce(testCase)
testCase.ExtractPid = @(table)table.PID;
testCase.Sigint = @(id)"kill " + id; % kill sends a SIGTERM instead of SIGINT but turns out this is sufficient to terminate OTEL collector on Linux
testCase.Sigterm = @(id)"kill " + id;

% variables to support downloading OpenTelemetry Collector
otelcol_arch_name = "linux_amd64";
otelcol_exe_ext = "";
elseif ismac
testCase.ListPid = @(name)"pgrep -x " + name;
testCase.ReadPidList = @readmatrix;
Expand All @@ -53,10 +60,31 @@ function commonSetupOnce(testCase)
end

end

% OpenTelemetry Collector
if isempty(otelcolroot)
% collector not pre-installed
otelcol_version = "0.85.0";
otelcol_url = "https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v" ...
+ otelcol_version;
otelcol_zipfilename = "otelcol_" + otelcol_version + "_" + otelcol_arch_name;
otelcolroot = fullfile(tempdir, otelcol_zipfilename);

% look for it in tempdir, download and install if it doesn't exist
if ~exist(fullfile(otelcolroot, testCase.OtelcolName + otelcol_exe_ext),"file")
otelcol_tar = gunzip(fullfile(otelcol_url, otelcol_zipfilename + ".tar.gz"), otelcolroot);
otelcol_tar = otelcol_tar{1}; % should have only extracted 1 tar file
untar(otelcol_tar, otelcolroot);
delete(otelcol_tar);
end
end

testCase.Otelcol = fullfile(otelcolroot, testCase.OtelcolName);

% set up path
testCase.applyFixture(matlab.unittest.fixtures.PathFixture(testCase.OtelRoot));
if ~isempty(otelroot)
testCase.applyFixture(matlab.unittest.fixtures.PathFixture(otelroot));
end

% remove temporary files if present
if exist(testCase.JsonFile, "file")
Expand Down
3 changes: 1 addition & 2 deletions test/performance/traceTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

properties
OtelConfigFile
OtelRoot
JsonFile
PidFile
OtelcolName
OtelcolName
Otelcol
ListPid
ReadPidList
Expand Down
7 changes: 4 additions & 3 deletions test/tbaggage.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
% Copyright 2023 The MathWorks, Inc.

properties
OtelRoot
BaggageKeys
BaggageValues
BaggageHeaders
end

methods (TestClassSetup)
function setupOnce(testCase)
testCase.OtelRoot = getenv("OPENTELEMETRY_MATLAB_INSTALL");
otelroot = getenv("OPENTELEMETRY_MATLAB_INSTALL");

% set up path
addpath(testCase.OtelRoot);
if ~isempty(otelroot)
addpath(otelroot);
end

testCase.BaggageKeys = ["userId", "serverNode", "isProduction"];
testCase.BaggageValues = ["alice", "DF28", "false"];
Expand Down
3 changes: 1 addition & 2 deletions test/tcontextPropagation.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

properties
OtelConfigFile
OtelRoot
JsonFile
PidFile
OtelcolName
OtelcolName
Otelcol
ListPid
ReadPidList
Expand Down
3 changes: 1 addition & 2 deletions test/ttrace.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

properties
OtelConfigFile
OtelRoot
JsonFile
PidFile
OtelcolName
OtelcolName
Otelcol
ListPid
ReadPidList
Expand Down

0 comments on commit c573ada

Please sign in to comment.