@@ -76,18 +76,75 @@ boundCounter.add(Math.random() > 0.5 ? 1 : -1);
76
76
77
77
### Value Observer
78
78
79
- Choose this kind of metric when only last value is important without worry about aggregation
79
+ Choose this kind of metric when only last value is important without worry about aggregation.
80
+ The callback can be sync or async.
80
81
81
82
``` js
82
83
const { MeterProvider } = require (' @opentelemetry/metrics' );
83
84
84
85
const meter = new MeterProvider ().getMeter (' your-meter-name' );
85
86
86
- meter .createValueObserver (' cpu_core_usage' , {
87
+
88
+ // async callback - for operation that needs to wait for value
89
+ meter .createValueObserver (' your_metric_name' , {
90
+ description: ' Example of an async observer with callback' ,
91
+ }, async (observerResult ) => {
92
+ const value = await getAsyncValue ();
93
+ observerResult .observe (value, { label: ' 1' });
94
+ });
95
+
96
+ function getAsyncValue () {
97
+ return new Promise ((resolve ) => {
98
+ setTimeout (()=> {
99
+ resolve (Math .random ());
100
+ }, 100 );
101
+ });
102
+ }
103
+
104
+ // sync callback in case you don't need to wait for value
105
+ meter .createValueObserver (' your_metric_name' , {
106
+ description: ' Example of a sync observer with callback' ,
107
+ }, (observerResult ) => {
108
+ observerResult .observe (getRandomValue (), { label: ' 1' });
109
+ observerResult .observe (getRandomValue (), { label: ' 2' });
110
+ });
111
+
112
+ function getRandomValue () {
113
+ return Math .random ();
114
+ }
115
+ ```
116
+
117
+ ### UpDownSumObserver
118
+
119
+ Choose this kind of metric when sum is important and you want to capture any value that starts at zero and rises or falls throughout the process lifetime.
120
+ The callback can be sync or async.
121
+
122
+ ``` js
123
+ const { MeterProvider } = require (' @opentelemetry/metrics' );
124
+
125
+ const meter = new MeterProvider ().getMeter (' your-meter-name' );
126
+
127
+ // async callback - for operation that needs to wait for value
128
+ meter .createUpDownSumObserver (' your_metric_name' , {
129
+ description: ' Example of an async observer with callback' ,
130
+ }, async (observerResult ) => {
131
+ const value = await getAsyncValue ();
132
+ observerResult .observe (value, { label: ' 1' });
133
+ });
134
+
135
+ function getAsyncValue () {
136
+ return new Promise ((resolve ) => {
137
+ setTimeout (()=> {
138
+ resolve (Math .random ());
139
+ }, 100 );
140
+ });
141
+ }
142
+
143
+ // sync callback in case you don't need to wait for value
144
+ meter .createUpDownSumObserver (' your_metric_name' , {
87
145
description: ' Example of a sync observer with callback' ,
88
146
}, (observerResult ) => {
89
- observerResult .observe (getRandomValue (), { core: ' 1' });
90
- observerResult .observe (getRandomValue (), { core: ' 2' });
147
+ observerResult .observe (getRandomValue (), { label: ' 1' });
91
148
});
92
149
93
150
function getRandomValue () {
0 commit comments