|
11 | 11 | from timeout_handling import * |
12 | 12 |
|
13 | 13 | import otel |
| 14 | +from opentelemetry import metrics |
14 | 15 |
|
15 | 16 | try: |
16 | 17 | log.info('Importing senzing_core library . . .') |
@@ -51,29 +52,31 @@ def go(): |
51 | 52 | log.error(fmterr(e)) |
52 | 53 |
|
53 | 54 | # OTel setup # |
| 55 | + log.info('Starting OTel setup.') |
54 | 56 | meter = otel.init('redoer') |
55 | | - |
56 | | - otel_msgs_counter = meter.create_counter( |
57 | | - 'redoer.messages.count', |
58 | | - description='Counter incremented with each message processed by the redoer.') |
59 | | - |
60 | | - otel_durations = meter.create_histogram( |
61 | | - 'redoer.messages.duration', |
62 | | - 'Message processing duration for the redoer.') |
| 57 | + otel_msgs_counter = meter.create_counter('redoer.messages.count') |
| 58 | + otel_durations = meter.create_histogram('redoer.messages.duration') |
63 | 59 |
|
64 | 60 | def _queue_count_steward(tally): |
65 | | - '''Coroutine function; emits like a generator but values can also be |
66 | | - sent into it too. Its generator-like behavior let's us pass it into the Gauge |
67 | | - constructor.''' |
| 61 | + '''Coroutine function; this lets us both: |
| 62 | + - 1) easily pass in updated tally values via `send` |
| 63 | + - 2) accommodate OTel's spec of a "a generator that yields |
| 64 | + iterables of Observation" |
| 65 | + Ref: https://opentelemetry-python.readthedocs.io/en/latest/api/metrics.html#opentelemetry.metrics.Meter.create_observable_gauge |
| 66 | + ''' |
68 | 67 | while 1: |
69 | | - newtally = yield tally |
70 | | - tally = newtally if newtally |
71 | | - queue_count_steward = _queue_count_steward(0) |
| 68 | + newtally = yield [metrics.Observation(tally)] |
| 69 | + # We check the type b/c OTel internals will send in a |
| 70 | + # CallbackOptions object that we'll want to ignore; |
| 71 | + # meanwhile type `int` means we sent in an updated tally value. |
| 72 | + # ourselves. |
| 73 | + if newtally and type(newtally) is int: tally = newtally |
| 74 | + queue_count_steward = _queue_count_steward(-1) |
72 | 75 | next(queue_count_steward) # prime it. |
73 | 76 | otel_queue_gauge = meter.create_observable_gauge( |
74 | | - 'redoer.queue.count', |
75 | | - description='Current number of items in the redo queue.', |
76 | | - [queue_count_steward]) |
| 77 | + 'redoer.queue.count', [queue_count_steward]) |
| 78 | + |
| 79 | + log.info('Finished OTel setup.') |
77 | 80 | # end OTel setup # |
78 | 81 |
|
79 | 82 | log.info('Starting primary loop.') |
@@ -126,20 +129,21 @@ def _queue_count_steward(tally): |
126 | 129 | except sz.SzError as sz_err: |
127 | 130 | log.error(SZ_TAG + fmterr(sz_err)) |
128 | 131 |
|
129 | | - finish = time.perf_counter() |
130 | | - otel_msgs_counter.add(1, |
131 | | - {'status': success_status, |
132 | | - 'service': 'redoer', |
133 | | - 'environment': RUNTIME_ENV}) |
134 | | - otel_durations.record(finish - start, |
135 | | - {'status': success_status, |
136 | | - 'service': 'redoer', |
137 | | - 'environment': RUNTIME_ENV}) |
| 132 | + finish = time.perf_counter() |
| 133 | + otel_msgs_counter.add(1, |
| 134 | + {'status': success_status, |
| 135 | + 'service': 'redoer', |
| 136 | + 'environment': RUNTIME_ENV}) |
| 137 | + otel_durations.record(finish - start, |
| 138 | + {'status': success_status, |
| 139 | + 'service': 'redoer', |
| 140 | + 'environment': RUNTIME_ENV}) |
138 | 141 |
|
139 | 142 | else: |
140 | 143 | try: |
141 | 144 | tally = sz_eng.count_redo_records() |
142 | 145 | queue_count_steward.send(tally) |
| 146 | + #otel_queue_gauge.record(tally) |
143 | 147 | log.debug(SZ_TAG + 'Current redo count: ' + str(tally)) |
144 | 148 | except sz.SzRetryableError as sz_ret_err: |
145 | 149 | log.error(SZ_TAG + fmterr(sz_ret_err)) |
|
0 commit comments