Skip to content

Commit 4b0877b

Browse files
authored
Add MATLAB Production Server AutoTrace example (#252)
* Add MATLAB Production Server AutoTrace example * Address pull request review feedback
1 parent 0d90f73 commit 4b0877b

3 files changed

Lines changed: 124 additions & 0 deletions

File tree

‎examples/mps_autotrace/README.md‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# MATLAB Production Server (MPS) AutoTrace Example
2+
This example shows how to use an AutoTrace wrapper function to automatically instrument MATLAB code deployed to MATLAB Production Server. This is an easy way to profile your functions in deployment without needing to make any changes to the original function code.
3+
4+
To use the wrapper, include it as an additional deployed function when packaging the CTF containing your function code. Once deployed, functions can be invoked normally to run without tracing, or through the *autotrace_wrapper* to run with tracing.
5+
6+
For example, if you would normally call your deployed function *my_function* with arguments 3, 2, 1, and "linear" as follows:
7+
```
8+
curl http://localhost:9910/MyFunction/my_function -H "Content-Type: application/json" -X POST -d "{'nargout':1,'rhs':[3, 2, 1, 'linear']}"
9+
```
10+
To run the same call with autotracing, call *autotrace_wrapper* with the function name as the first argument, followed by all other arguments:
11+
```
12+
curl http://localhost:9910/MyFunction/autotrace_wrapper -H "Content-Type: application/json" -X POST -d "{'nargout':1,'rhs':['my_function', 3, 2, 1, 'linear']}"
13+
```
14+
15+
## Required Products
16+
- MATLAB (R2022b or newer)
17+
- MATLAB Compiler SDK
18+
- MATLAB Production Server
19+
20+
## Testing the Example
21+
1. Start an instance of [OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector).
22+
2. Generate and test a .ctf archive using the [testing interface](https://www.mathworks.com/help/compiler_sdk/mps_dev_test/test-web-request-handler.html) in the Production Server Compiler app.
23+
1. In MATLAB, navigate to the example directory.
24+
2. Start the Production Server Compiler app.
25+
```
26+
productionServerCompiler
27+
```
28+
3. In the app, select Type as "Deployable Archive (.ctf)", and add my_function.m and autotrace_wrapper.m as exported functions.
29+
4. Specify "MyFunction" as the archive name.
30+
5. Click on the "Test Client" button.
31+
6. Ensure the port is 9910, then start the test.
32+
7. Using a command prompt, send a cURL request to call my_function through the autotrace_wrapper function.
33+
```
34+
curl http://localhost:9910/MyFunction/autotrace_wrapper -H "Content-Type: application/json" -X POST -d "{'nargout':1,'rhs':['my_function', 3, 2, 1, 'linear']}"
35+
```
36+
8. Check for expected spans in the OpenTelemetry Collector or in a specified tracing backend.
37+
38+
**NOTE:** You may need to manually add your OpenTelemetry-matlab installation as "Files Required for Archive to Run" during packaging, as certain file dependencies may not be automatically detected by dependency analysis. Failing to include these dependencies can result in errors when running the generated CTF on a different machine.
39+
40+
## Deploying the Example
41+
1. If testing works, proceed to deploy the example. [Create](https://www.mathworks.com/help/mps/server/creating-a-server.html) and [start](https://www.mathworks.com/help/mps/qs/starting-and-stopping.html) a MATLAB Production Server instance.
42+
2. [Deploy](https://www.mathworks.com/help/mps/qs/share-a-ctf-archive-on-the-server-instance.html) archive MyFunction.ctf to the server instance by copying to the auto_deploy directory.
43+
3. Using a command prompt, send a cURL request to call my_function through the autotrace_wrapper function.
44+
```
45+
curl http://localhost:9910/MyFunction/autotrace_wrapper -H "Content-Type: application/json" -X POST -d "{'nargout':1,'rhs':['my_function', 3, 2, 1, 'linear']}"
46+
```
47+
4. Check for expected spans in the OpenTelemetry Collector or in a specified tracing backend.
48+
49+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function yf = autotrace_wrapper(varargin)
2+
% Wrapper that automatically instruments and invokes a target function.
3+
% The first input must be the function name (string or char). Any remaining
4+
% inputs are passed as arguments to that function.
5+
%
6+
% Example:
7+
% yf = autotrace_wrapper("myfun", arg1, arg2, ...)
8+
9+
% Copyright 2026 The MathWorks, Inc.
10+
11+
% validate inputs
12+
if nargin < 1
13+
error('autotrace_wrapper requires at least the function name as the first argument.');
14+
end
15+
16+
func = varargin{1};
17+
if ~(ischar(func) || isStringScalar(func))
18+
error('First argument must be a function name (char or string).');
19+
end
20+
21+
% convert input func to handle
22+
fh = str2func(func);
23+
24+
% remaining arguments to pass to the traced function
25+
args = varargin(2:end);
26+
27+
% create tracer name
28+
tn = "autotrace_" + func;
29+
30+
% configure tracer provider
31+
resource = dictionary("service.name", "OpenTelemetry-Matlab_examples");
32+
tp = opentelemetry.sdk.trace.TracerProvider(Resource=resource);
33+
setTracerProvider(tp);
34+
35+
% create AutoTrace object
36+
at = opentelemetry.autoinstrument.AutoTrace(fh, TracerName=tn);
37+
38+
% run the example
39+
yf = beginTrace(at, args{:});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function result = my_function(x,y,z,mode)
2+
% Combine three numbers into one value according to mode.
3+
% mode: 'equal' | 'linear' | 'custom'
4+
5+
% Copyright 2026 The MathWorks, Inc.
6+
7+
values = [x, y, z];
8+
9+
w = get_weights(mode, values);
10+
result = aggregate(values, w);
11+
end
12+
13+
function w = get_weights(mode, v)
14+
switch lower(mode)
15+
case 'equal'
16+
w = ones(1,3) / 3;
17+
case 'linear'
18+
% weight proportional to value magnitude (nonnegative)
19+
vv = abs(v);
20+
if sum(vv) == 0
21+
w = ones(1,3) / 3;
22+
else
23+
w = vv / sum(vv);
24+
end
25+
case 'custom'
26+
% simple custom rule: emphasize middle element
27+
w = [0.2, 0.6, 0.2];
28+
otherwise
29+
error('Unknown mode. Use ''equal'', ''linear'', or ''custom''.');
30+
end
31+
end
32+
33+
function s = aggregate(v, w)
34+
% weighted sum, then normalize to return scalar
35+
s = sum(w .* v);
36+
end

0 commit comments

Comments
 (0)