Skip to content

Commit 97754c9

Browse files
committed
Add help texts for metrics
1 parent 99f23be commit 97754c9

File tree

12 files changed

+263
-86
lines changed

12 files changed

+263
-86
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
MATLAB® interface to [OpenTelemetry™](https://opentelemetry.io/), based on the [OpenTelemetry Specification](https://opentelemetry.io/docs/reference/specification/). OpenTelemetry is an observability framework for creating and managing telemetry data, such as traces, metrics, and logs. This data can then be sent to an observability back-end for monitoring, alerts, and analysis.
55

66
### Status
7-
- Currently only tracing is supported. Metrics and logs will be in the future.
7+
- Currently only tracing and metrics are supported. Logs will be in the future.
88
- This package is supported and has been tested on Windows®, Linux®, and macOS.
99

1010
### MathWorks Products (https://www.mathworks.com)
@@ -43,6 +43,7 @@ otelcol --config <otelcol-config-yaml>
4343
>> addpath <OpenTelemetry MATLAB installdir>
4444
```
4545
## Examples
46+
### Tracing
4647
1. Create a default tracer provider and save it.
4748
```
4849
>> p = opentelemetry.sdk.trace.TracerProvider();
@@ -58,6 +59,22 @@ otelcol --config <otelcol-config-yaml>
5859
>> sp.endSpan();
5960
```
6061
4. If your collector is configured to display the data, you should see your span displayed.
62+
### Metrics
63+
1. Create a default meter provider and save it.
64+
```
65+
>> p = opentelemetry.sdk.metrics.MeterProvider();
66+
>> setMeterProvider(p);
67+
```
68+
2. Create a counter
69+
```
70+
>> m = opentelemetry.metrics.getMeter("First Meter");
71+
>> c = m.createCounter("FirstCounter");
72+
```
73+
3. Increment the counter
74+
```
75+
>> c.add(10);
76+
```
77+
4. If your collector is configured to display the data, you should see your counter displayed after 1 minute.
6178

6279
For more examples, see the "examples" folder.
6380

api/metrics/+opentelemetry/+metrics/Counter.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414

1515
methods
1616
function add(obj, value, varargin)
17+
% ADD Add to counter value
18+
% ADD(C, VALUE) adds a nonnegative scalar numeric value to
19+
% the counter.
20+
%
21+
% ADD(C, VALUE, ATTRIBUTES) also specifies attributes as a
22+
% dictionary
23+
%
24+
% ADD(C, VALUE, ATTRNAME1, ATTRVALUE1, ATTRNAME2,
25+
% ATTRVALUE2, ...) specifies attributes as trailing
26+
% name-value pairs.
1727
obj.processValue(value, varargin{:});
1828
end
1929
end

api/metrics/+opentelemetry/+metrics/Histogram.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313

1414
methods
1515
function record(obj, value, varargin)
16+
% RECORD Aggregate a value into a histogram bin
17+
% RECORD(H, VALUE) determine which bin VALUE falls into and
18+
% increment that bin by 1.
19+
%
20+
% RECORD(H, VALUE, ATTRIBUTES) also specifies attributes as a
21+
% dictionary
22+
%
23+
% RECORD(H, VALUE, ATTRNAME1, ATTRVALUE1, ATTRNAME2,
24+
% ATTRVALUE2, ...) specifies attributes as trailing
25+
% name-value pairs.
1626
obj.processValue(value, varargin{:});
1727
end
1828
end

api/metrics/+opentelemetry/+metrics/Meter.m

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030
methods
3131

3232
function counter = createCounter(obj, ctname, ctdescription, ctunit)
33+
% CREATECOUNTER Create a counter
34+
% C = CREATECOUNTER(M, NAME) creates a counter with the specified
35+
% name. A counter's value can only increase but not
36+
% decrease.
37+
%
38+
% C = CREATECOUNTER(M, NAME, DESCRIPTION, UNIT) also
39+
% specifies a description and a unit.
40+
%
41+
% See also CREATEUPDOWNCOUNTER, CREATEHISTOGRAM
3342
arguments
3443
obj
3544
ctname
@@ -38,8 +47,6 @@
3847
end
3948
import opentelemetry.common.mustBeScalarString
4049
ctname = mustBeScalarString(ctname);
41-
% cpp-opentelemetry end does not allow string input with spaces,
42-
% replace any spaces with underscores as a temporary fix
4350
ctdescription = mustBeScalarString(ctdescription);
4451
ctunit = mustBeScalarString(ctunit);
4552
id = obj.Proxy.createCounter(ctname, ctdescription, ctunit);
@@ -50,6 +57,15 @@
5057

5158

5259
function updowncounter = createUpDownCounter(obj, ctname, ctdescription, ctunit)
60+
% CREATEUPDOWNCOUNTER Create an UpDownCounter
61+
% C = CREATEUPDOWNCOUNTER(M, NAME) creates an UpDownCounter
62+
% with the specified name. An UpDownCounter's value can
63+
% increase or decrease.
64+
%
65+
% C = CREATEUPDOWNCOUNTER(M, NAME, DESCRIPTION, UNIT) also
66+
% specifies a description and a unit.
67+
%
68+
% See also CREATECOUNTER, CREATEHISTOGRAM
5369
arguments
5470
obj
5571
ctname
@@ -59,8 +75,6 @@
5975

6076
import opentelemetry.common.mustBeScalarString
6177
ctname = mustBeScalarString(ctname);
62-
% cpp-opentelemetry end does not allow string input with spaces,
63-
% replace any spaces with underscores as a temporary fix
6478
ctdescription = mustBeScalarString(ctdescription);
6579
ctunit = mustBeScalarString(ctunit);
6680
id = obj.Proxy.createUpDownCounter(ctname, ctdescription, ctunit);
@@ -71,6 +85,16 @@
7185

7286

7387
function histogram = createHistogram(obj, hiname, hidescription, hiunit)
88+
% CREATEHISTOGRAM Create a histogram
89+
% H = CREATEHISTOGRAM(M, NAME) creates a histogram with the specified
90+
% name. A histogram aggregates values into bins. Bins can be
91+
% customized using a View object.
92+
%
93+
% H = CREATEHISTOGRAM(M, NAME, DESCRIPTION, UNIT) also
94+
% specifies a description and a unit.
95+
%
96+
% See also CREATECOUNTER, CREATEUPDOWNCOUNTER,
97+
% OPENTELEMETRY.SDK.METRICS.VIEW
7498
arguments
7599
obj
76100
hiname
@@ -80,8 +104,6 @@
80104

81105
import opentelemetry.common.mustBeScalarString
82106
hiname = mustBeScalarString(hiname);
83-
% cpp-opentelemetry end does not allow string input with spaces,
84-
% replace any spaces with underscores as a temporary fix
85107
hidescription = mustBeScalarString(hidescription);
86108
hiunit = mustBeScalarString(hiunit);
87109
id = obj.Proxy.createHistogram(hiname, hidescription, hiunit);

api/metrics/+opentelemetry/+metrics/UpDownCounter.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313

1414
methods
1515
function add(obj, value, varargin)
16+
% ADD Add to UpDownCounter value
17+
% ADD(C, VALUE) adds scalar numeric value to the
18+
% UpDownCounter. VALUE can be positive or negative.
19+
%
20+
% ADD(C, VALUE, ATTRIBUTES) also specifies attributes as a
21+
% dictionary
22+
%
23+
% ADD(C, VALUE, ATTRNAME1, ATTRVALUE1, ATTRNAME2,
24+
% ATTRVALUE2, ...) specifies attributes as trailing
25+
% name-value pairs.
1626
obj.processValue(value, varargin{:});
1727
end
1828
end

sdk/metrics/+opentelemetry/+sdk/+metrics/MeterProvider.m

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88
isShutdown (1,1) logical = false
99
end
1010

11-
properties (Access=public)
12-
MetricReader
13-
View
14-
Resource
11+
properties (SetAccess=private)
12+
MetricReader % Metric reader controls how often metrics are exported
13+
View % View object used to customize collected metrics
14+
Resource % Attributes attached to all metrics
1515
end
1616

1717
methods
1818
function obj = MeterProvider(reader, optionnames, optionvalues)
1919
% SDK implementation of meter provider
20-
% MP = OPENTELEMETRY.SDK.METRICS.METERPROVIDER creates a meter
20+
% MP = OPENTELEMETRY.SDK.METRICS.METERPROVIDER creates a meter
2121
% provider that uses a periodic exporting metric reader and default configurations.
2222
%
2323
% MP = OPENTELEMETRY.SDK.METRICS.METERPROVIDER(R) uses metric
24-
% reader R. Currently, the only supported metric reader is the periodic
25-
% exporting metric reader.
24+
% reader R. Currently, the only supported metric reader is the periodic
25+
% exporting metric reader.
2626
%
27-
% TP = OPENTELEMETRY.SDK.METRICS.METERPROVIDER(R, PARAM1, VALUE1,
27+
% TP = OPENTELEMETRY.SDK.METRICS.METERPROVIDER(R, PARAM1, VALUE1,
2828
% PARAM2, VALUE2, ...) specifies optional parameter name/value pairs.
2929
% Parameters are:
3030
% "View" - View object used to customize collected metrics.
@@ -59,7 +59,7 @@
5959
"ConstructorArguments", {mpproxy.ID});
6060
% leave other properties unassigned, they won't be used
6161
else
62-
validnames = ["Resource"];
62+
validnames = "Resource";
6363
resourcekeys = string.empty();
6464
resourcevalues = {};
6565
resource = dictionary(resourcekeys, resourcevalues);
@@ -89,21 +89,31 @@
8989
obj.Resource = resource;
9090
end
9191
end
92-
92+
9393
function addMetricReader(obj, reader)
94-
arguments
95-
obj
96-
reader (1,1) {mustBeA(reader, "opentelemetry.sdk.metrics.PeriodicExportingMetricReader")}
97-
end
94+
% ADDMETRICREADER Add an additional metric reader
95+
% ADDMETRICREADER(MP, R) adds an additional metric reader
96+
% R to the list of metric readers used by meter provider
97+
% MP.
98+
%
99+
% See also ADDVIEW, OPENTELEMETRY.SDK.METRICS.PERIODICEXPORTINGMETRICREADER
100+
arguments
101+
obj
102+
reader (1,1) {mustBeA(reader, "opentelemetry.sdk.metrics.PeriodicExportingMetricReader")}
103+
end
98104
obj.Proxy.addMetricReader(reader.Proxy.ID);
99105
obj.MetricReader = [obj.MetricReader, reader];
100106
end
101107

102108
function addView(obj, view)
103-
arguments
104-
obj
105-
view (1,1) {mustBeA(view, "opentelemetry.sdk.metrics.View")}
106-
end
109+
% ADDVIEW Add an additional view
110+
% ADDVIEW(MP, V) adds an additional view V.
111+
%
112+
% See also ADDMETRICREADER, OPENTELEMETRY.SDK.METRICS.VIEW
113+
arguments
114+
obj
115+
view (1,1) {mustBeA(view, "opentelemetry.sdk.metrics.View")}
116+
end
107117
obj.Proxy.addView(view.Proxy.ID);
108118
obj.View = [obj.View, view];
109119
end

sdk/metrics/+opentelemetry/+sdk/+metrics/MetricExporter.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
methods
2525
function obj = set.PreferredAggregationTemporality(obj, temporality)
2626
temporality = validatestring(temporality, ["cumulative", "delta"]);
27-
obj.Proxy.setTemporality(temporality);
27+
obj.Proxy.setTemporality(temporality); %#ok<MCSUP>
2828
obj.PreferredAggregationTemporality = temporality;
2929
end
3030

sdk/metrics/+opentelemetry/+sdk/+metrics/PeriodicExportingMetricReader.m

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
classdef PeriodicExportingMetricReader < matlab.mixin.Heterogeneous
2-
% Base class of metric reader
2+
% Periodic exporting metric reader passes collected metrics to an exporter
3+
% periodically at a fixed time interval.
34

45
% Copyright 2023 The MathWorks, Inc.
56

@@ -12,13 +13,35 @@
1213
end
1314

1415
properties
15-
Interval (1,1) duration = minutes(1)
16-
Timeout (1,1) duration = seconds(30)
16+
Interval (1,1) duration = minutes(1) % Time interval between exports
17+
Timeout (1,1) duration = seconds(30) % Maximum time before export is timed out and gets aborted
1718
end
1819

19-
methods %(Access=?opentelemetry.sdk.metrics.MeterProvider)
20+
methods
2021
function obj = PeriodicExportingMetricReader(metricexporter, optionnames, optionvalues)
21-
22+
% Periodic exporting metric reader passes collected metrics to
23+
% an exporter periodically at a fixed time interval.
24+
% R = OPENTELEMETRY.SDK.METRICS.PERIODICEXPORTINGMETRICREADER
25+
% creates a periodic exporting metric reader that exports
26+
% every minute using an OTLP HTTP exporter, which exports in
27+
% OpenTelemetry Protocol (OTLP) format through HTTP.
28+
%
29+
% R = OPENTELEMETRY.SDK.METRICS.PERIODICEXPORTINGMETRICREADER(EXP)
30+
% specifies the metric exporter. Supported metric exporters
31+
% are OTLP HTTP exporter and OTLP gRPC exporter.
32+
%
33+
% R = OPENTELEMETRY.SDK.METRICS.PERIODICEXPORTINGMETRICREADER(
34+
% EXP, PARAM1, VALUE1, PARAM2, VALUE2, ...) specifies
35+
% optional parameter name/value pairs. Parameters are:
36+
% "Interval" - Time interval between exports specified as
37+
% a duration. Default is 1 minute.
38+
% "Timeout" - Maximum time before export is timed out
39+
% and gets aborted, specified as a duration.
40+
% Default is 30 seconds.
41+
%
42+
% See also OPENTELEMETRY.EXPORTERS.OTLP.OTLPHTTPMETRICEXPORTER,
43+
% OPENTELEMETRY.EXPORTERS.OTLP.OTLPGRPCMETRICEXPORTER,
44+
% OPENTELEMETRY.SDK.METRICS.METERPROVIDER
2245
arguments
2346
metricexporter {mustBeA(metricexporter, "opentelemetry.sdk.metrics.MetricExporter")} = ...
2447
opentelemetry.exporters.otlp.defaultMetricExporter()

0 commit comments

Comments
 (0)