codecraftai-agent-kit 1.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ccai/agentkit/__init__.py +304 -0
- ccai/agentkit/addressresolver.py +177 -0
- ccai/agentkit/agent.py +2945 -0
- ccai/agentkit/agentaddress.py +187 -0
- ccai/agentkit/agentconfiguration.py +581 -0
- ccai/agentkit/agentidentity.py +152 -0
- ccai/agentkit/agentregistry.py +221 -0
- ccai/agentkit/agentstatemachine.py +134 -0
- ccai/agentkit/alternateproviderstrategy.py +36 -0
- ccai/agentkit/approvalgate.py +238 -0
- ccai/agentkit/approvalrequest.py +250 -0
- ccai/agentkit/asyncagent.py +187 -0
- ccai/agentkit/breakpoint.py +150 -0
- ccai/agentkit/builtinagentmetrics.py +114 -0
- ccai/agentkit/builtins/__init__.py +11 -0
- ccai/agentkit/builtins/filereadtool.py +90 -0
- ccai/agentkit/builtins/filewritetool.py +93 -0
- ccai/agentkit/builtins/httprequesttool.py +169 -0
- ccai/agentkit/builtins/shellcommandtool.py +190 -0
- ccai/agentkit/cachedresultstrategy.py +43 -0
- ccai/agentkit/checkpointmanager.py +219 -0
- ccai/agentkit/circuitbreaker.py +230 -0
- ccai/agentkit/circuitbreakerconfiguration.py +158 -0
- ccai/agentkit/circuitbreakerevent.py +158 -0
- ccai/agentkit/circuitbreakerhealthcheck.py +32 -0
- ccai/agentkit/circuitbreakerregistry.py +116 -0
- ccai/agentkit/circuitbreakerstate.py +44 -0
- ccai/agentkit/clearcacheaction.py +46 -0
- ccai/agentkit/concurrencybridge.py +143 -0
- ccai/agentkit/concurrencymodel.py +19 -0
- ccai/agentkit/conditionevaluator.py +594 -0
- ccai/agentkit/configurationchangeevent.py +179 -0
- ccai/agentkit/configurationerrors.py +152 -0
- ccai/agentkit/configurationlayer.py +160 -0
- ccai/agentkit/configurationlayersource.py +46 -0
- ccai/agentkit/configurationmanager.py +680 -0
- ccai/agentkit/configurationschema.py +224 -0
- ccai/agentkit/configurationschemafield.py +217 -0
- ccai/agentkit/configurationserializer.py +163 -0
- ccai/agentkit/configurationvalidationresult.py +118 -0
- ccai/agentkit/contextsummarizer.py +170 -0
- ccai/agentkit/contextwindow.py +264 -0
- ccai/agentkit/contextwindowstrategy.py +57 -0
- ccai/agentkit/correlationcontext.py +156 -0
- ccai/agentkit/costestimate.py +139 -0
- ccai/agentkit/costtracker.py +121 -0
- ccai/agentkit/deadletterentry.py +134 -0
- ccai/agentkit/deadletterquery.py +119 -0
- ccai/agentkit/deadletterqueue.py +123 -0
- ccai/agentkit/deadletterstore.py +86 -0
- ccai/agentkit/debuginspector.py +211 -0
- ccai/agentkit/debugsession.py +223 -0
- ccai/agentkit/defaultvaluestrategy.py +43 -0
- ccai/agentkit/degradationchangeevent.py +120 -0
- ccai/agentkit/degradationlevel.py +44 -0
- ccai/agentkit/degradationmanager.py +212 -0
- ccai/agentkit/degradationpolicy.py +130 -0
- ccai/agentkit/degradationstrategy.py +66 -0
- ccai/agentkit/embeddingprovider.py +106 -0
- ccai/agentkit/errors.py +95 -0
- ccai/agentkit/executionrecord.py +213 -0
- ccai/agentkit/executionrecorder.py +180 -0
- ccai/agentkit/executionrecording.py +230 -0
- ccai/agentkit/executionreplayer.py +187 -0
- ccai/agentkit/expirationpolicy.py +116 -0
- ccai/agentkit/fallbackchain.py +82 -0
- ccai/agentkit/featureflag.py +154 -0
- ccai/agentkit/featureflagevaluator.py +109 -0
- ccai/agentkit/featureflagregistry.py +258 -0
- ccai/agentkit/filedeadletterstore.py +112 -0
- ccai/agentkit/filememorystore.py +291 -0
- ccai/agentkit/healthcheck.py +46 -0
- ccai/agentkit/healthcheckregistry.py +51 -0
- ccai/agentkit/healthmonitor.py +258 -0
- ccai/agentkit/healthstatus.py +105 -0
- ccai/agentkit/hotreloadmonitor.py +214 -0
- ccai/agentkit/hotreloadpolicy.py +160 -0
- ccai/agentkit/inmemorydeadletterstore.py +61 -0
- ccai/agentkit/inmemorystore.py +173 -0
- ccai/agentkit/jsonmessageserializer.py +89 -0
- ccai/agentkit/jsonoutputparser.py +50 -0
- ccai/agentkit/lifecyclestate.py +22 -0
- ccai/agentkit/llmconversation.py +153 -0
- ccai/agentkit/llmerrors.py +193 -0
- ccai/agentkit/llmmessage.py +158 -0
- ccai/agentkit/llmprovider.py +158 -0
- ccai/agentkit/llmproviderregistry.py +200 -0
- ccai/agentkit/llmresponse.py +190 -0
- ccai/agentkit/llmstreamchunk.py +65 -0
- ccai/agentkit/llmstreamhandler.py +48 -0
- ccai/agentkit/llmtoolbridge.py +154 -0
- ccai/agentkit/logexporter.py +316 -0
- ccai/agentkit/logformatter.py +169 -0
- ccai/agentkit/memoryconfiguration.py +243 -0
- ccai/agentkit/memoryentry.py +254 -0
- ccai/agentkit/memoryerrors.py +135 -0
- ccai/agentkit/memoryindex.py +213 -0
- ccai/agentkit/memoryquery.py +248 -0
- ccai/agentkit/memoryqueryresult.py +86 -0
- ccai/agentkit/memoryscope.py +54 -0
- ccai/agentkit/memorystore.py +260 -0
- ccai/agentkit/memorystorehealthcheck.py +33 -0
- ccai/agentkit/memorystoreregistry.py +106 -0
- ccai/agentkit/message.py +310 -0
- ccai/agentkit/messageenvelope.py +144 -0
- ccai/agentkit/messagepattern.py +50 -0
- ccai/agentkit/messagerouter.py +290 -0
- ccai/agentkit/messageserializer.py +106 -0
- ccai/agentkit/messageserializerregistry.py +163 -0
- ccai/agentkit/messagingconfiguration.py +397 -0
- ccai/agentkit/messagingerrors.py +338 -0
- ccai/agentkit/metricdefinition.py +164 -0
- ccai/agentkit/metricmeasurement.py +178 -0
- ccai/agentkit/metricscollector.py +258 -0
- ccai/agentkit/metricsexporter.py +303 -0
- ccai/agentkit/metricsregistry.py +130 -0
- ccai/agentkit/metricssnapshot.py +155 -0
- ccai/agentkit/metrictype.py +51 -0
- ccai/agentkit/msgpackmessageserializer.py +97 -0
- ccai/agentkit/observabilityconfiguration.py +546 -0
- ccai/agentkit/observabilityerror.py +67 -0
- ccai/agentkit/observabilitypipeline.py +252 -0
- ccai/agentkit/outputparser.py +82 -0
- ccai/agentkit/outputschema.py +142 -0
- ccai/agentkit/parallelbranch.py +87 -0
- ccai/agentkit/platform/__init__.py +43 -0
- ccai/agentkit/platform/adapters/__init__.py +18 -0
- ccai/agentkit/platform/adapters/awsplatformadapter.py +1272 -0
- ccai/agentkit/platform/adapters/azureplatformadapter.py +1227 -0
- ccai/agentkit/platform/adapters/gcpplatformadapter.py +1257 -0
- ccai/agentkit/platform/adapters/localplatformadapter.py +956 -0
- ccai/agentkit/platform/adapters/platformadapter.py +139 -0
- ccai/agentkit/platform/deployment/__init__.py +15 -0
- ccai/agentkit/platform/deployment/deploymentdescriptor.py +234 -0
- ccai/agentkit/platform/deployment/deploymentdescriptorparser.py +178 -0
- ccai/agentkit/platform/deployment/deploymentdescriptorprinter.py +97 -0
- ccai/agentkit/platform/errors.py +224 -0
- ccai/agentkit/platform/platformconfiguration.py +412 -0
- ccai/agentkit/platform/platformdetector.py +125 -0
- ccai/agentkit/platform/platformruntime.py +331 -0
- ccai/agentkit/platform/platformserviceregistry.py +149 -0
- ccai/agentkit/platform/platformtype.py +60 -0
- ccai/agentkit/platform/services/__init__.py +19 -0
- ccai/agentkit/platform/services/computeservice.py +112 -0
- ccai/agentkit/platform/services/invocationresult.py +173 -0
- ccai/agentkit/platform/services/messagingservice.py +135 -0
- ccai/agentkit/platform/services/platformservice.py +111 -0
- ccai/agentkit/platform/services/queuemessage.py +193 -0
- ccai/agentkit/platform/services/storageobject.py +195 -0
- ccai/agentkit/platform/services/storageservice.py +159 -0
- ccai/agentkit/pluginconfiguration.py +241 -0
- ccai/agentkit/plugindependency.py +129 -0
- ccai/agentkit/plugindescriptor.py +225 -0
- ccai/agentkit/pluginlifecyclehook.py +41 -0
- ccai/agentkit/pluginmanager.py +360 -0
- ccai/agentkit/pluginstatus.py +47 -0
- ccai/agentkit/pooledagent.py +116 -0
- ccai/agentkit/prompttemplate.py +221 -0
- ccai/agentkit/prompttemplateregistry.py +145 -0
- ccai/agentkit/protobufmessageserializer.py +144 -0
- ccai/agentkit/pubsubbroker.py +311 -0
- ccai/agentkit/py.typed +0 -0
- ccai/agentkit/reconnecttransportaction.py +47 -0
- ccai/agentkit/regexoutputparser.py +68 -0
- ccai/agentkit/requestresponsehandler.py +274 -0
- ccai/agentkit/resetcircuitbreakeraction.py +48 -0
- ccai/agentkit/resilienceconfiguration.py +191 -0
- ccai/agentkit/resilienceerrors.py +103 -0
- ccai/agentkit/resiliencepipeline.py +201 -0
- ccai/agentkit/retryattempt.py +158 -0
- ccai/agentkit/retryexecutor.py +157 -0
- ccai/agentkit/retrypolicy.py +195 -0
- ccai/agentkit/retryresult.py +186 -0
- ccai/agentkit/selfhealingaction.py +42 -0
- ccai/agentkit/selfhealingpolicy.py +67 -0
- ccai/agentkit/selfhealingresult.py +130 -0
- ccai/agentkit/stepexecutionstatus.py +52 -0
- ccai/agentkit/stepresult.py +198 -0
- ccai/agentkit/steps/__init__.py +9 -0
- ccai/agentkit/steps/agentstep.py +247 -0
- ccai/agentkit/steps/approvalstep.py +276 -0
- ccai/agentkit/steps/llmstep.py +403 -0
- ccai/agentkit/steps/subworkflowstep.py +270 -0
- ccai/agentkit/steps/toolstep.py +265 -0
- ccai/agentkit/structuredlogentry.py +222 -0
- ccai/agentkit/structuredlogger.py +189 -0
- ccai/agentkit/subscription.py +141 -0
- ccai/agentkit/threadedagent.py +94 -0
- ccai/agentkit/tokenusage.py +136 -0
- ccai/agentkit/tool.py +203 -0
- ccai/agentkit/toolchain.py +268 -0
- ccai/agentkit/toolchainexecutor.py +134 -0
- ccai/agentkit/toolchainresult.py +166 -0
- ccai/agentkit/tooldescriptor.py +203 -0
- ccai/agentkit/toolerrors.py +160 -0
- ccai/agentkit/toolexecutor.py +168 -0
- ccai/agentkit/toolfactory.py +60 -0
- ccai/agentkit/toolregistry.py +184 -0
- ccai/agentkit/toolresult.py +142 -0
- ccai/agentkit/toolschema.py +197 -0
- ccai/agentkit/tracecontext.py +294 -0
- ccai/agentkit/traceevent.py +166 -0
- ccai/agentkit/traceexporter.py +305 -0
- ccai/agentkit/tracer.py +234 -0
- ccai/agentkit/tracespan.py +349 -0
- ccai/agentkit/transitionrecord.py +84 -0
- ccai/agentkit/transport.py +334 -0
- ccai/agentkit/transportconfiguration.py +436 -0
- ccai/agentkit/transporthealthcheck.py +36 -0
- ccai/agentkit/transportregistry.py +224 -0
- ccai/agentkit/transports/__init__.py +28 -0
- ccai/agentkit/transports/grpctransport.py +410 -0
- ccai/agentkit/transports/httptransport.py +423 -0
- ccai/agentkit/transports/messagequeuetransport.py +489 -0
- ccai/agentkit/transports/websockettransport.py +473 -0
- ccai/agentkit/workflowcheckpoint.py +208 -0
- ccai/agentkit/workflowconfiguration.py +295 -0
- ccai/agentkit/workflowcontext.py +249 -0
- ccai/agentkit/workflowdefinition.py +790 -0
- ccai/agentkit/workflowedge.py +128 -0
- ccai/agentkit/workflowerrors.py +295 -0
- ccai/agentkit/workflowexecutionstatus.py +48 -0
- ccai/agentkit/workflowexecutor.py +1121 -0
- ccai/agentkit/workflowregistry.py +185 -0
- ccai/agentkit/workflowresult.py +250 -0
- ccai/agentkit/workflowstep.py +195 -0
- codecraftai_agent_kit-1.3.0.dist-info/METADATA +99 -0
- codecraftai_agent_kit-1.3.0.dist-info/RECORD +230 -0
- codecraftai_agent_kit-1.3.0.dist-info/WHEEL +4 -0
- codecraftai_agent_kit-1.3.0.dist-info/licenses/LICENSE.md +204 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the MetricsCollector class that records MetricMeasurement
|
|
3
|
+
objects for defined metrics.
|
|
4
|
+
|
|
5
|
+
The MetricsCollector validates metric names against a MetricsRegistry,
|
|
6
|
+
enforces metric type constraints, accumulates counter, gauge, and
|
|
7
|
+
histogram values, delivers measurements to registered exporters,
|
|
8
|
+
and provides snapshot and reset capabilities.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from datetime import datetime
|
|
12
|
+
|
|
13
|
+
from ccai.agentkit.errors import SemanticError
|
|
14
|
+
from ccai.agentkit.metricmeasurement import MetricMeasurement
|
|
15
|
+
from ccai.agentkit.metricsregistry import MetricsRegistry
|
|
16
|
+
from ccai.agentkit.metricssnapshot import MetricsSnapshot
|
|
17
|
+
from ccai.agentkit.metrictype import MetricType
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class MetricsCollector:
|
|
21
|
+
"""
|
|
22
|
+
A component that records MetricMeasurement objects for defined
|
|
23
|
+
metrics.
|
|
24
|
+
|
|
25
|
+
The collector validates each recording operation against the
|
|
26
|
+
MetricsRegistry to ensure the metric exists and the operation
|
|
27
|
+
matches the metric type. Counter values are accumulated, gauge
|
|
28
|
+
values are set, and histogram values are appended to a list.
|
|
29
|
+
|
|
30
|
+
:param registry: The MetricsRegistry instance used for
|
|
31
|
+
validating metric names and types.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(self, registry: MetricsRegistry) -> None:
|
|
35
|
+
|
|
36
|
+
self._registry = registry
|
|
37
|
+
self._counter_values = {}
|
|
38
|
+
self._gauge_values = {}
|
|
39
|
+
self._histogram_values = {}
|
|
40
|
+
self._exporters = []
|
|
41
|
+
|
|
42
|
+
return
|
|
43
|
+
|
|
44
|
+
def _validate_metric(self, metric_name: str, expected_type: MetricType) -> None:
|
|
45
|
+
"""
|
|
46
|
+
Validates that the metric name is registered and matches
|
|
47
|
+
the expected MetricType.
|
|
48
|
+
|
|
49
|
+
:param metric_name: The metric name string to validate.
|
|
50
|
+
:param expected_type: The expected MetricType enum value.
|
|
51
|
+
|
|
52
|
+
:raises SemanticError: If the metric is not registered or
|
|
53
|
+
the type does not match.
|
|
54
|
+
"""
|
|
55
|
+
definition = self._registry.lookup(metric_name)
|
|
56
|
+
|
|
57
|
+
if definition is None:
|
|
58
|
+
raise SemanticError(
|
|
59
|
+
"Metric {!r} is not registered in the MetricsRegistry".format(
|
|
60
|
+
metric_name
|
|
61
|
+
)
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
if definition.metric_type != expected_type:
|
|
65
|
+
raise SemanticError(
|
|
66
|
+
"Metric {!r} has type {}, expected {}".format(
|
|
67
|
+
metric_name,
|
|
68
|
+
definition.metric_type.value,
|
|
69
|
+
expected_type.value
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
return
|
|
74
|
+
|
|
75
|
+
def _deliver_to_exporters(self, measurement: MetricMeasurement) -> None:
|
|
76
|
+
"""
|
|
77
|
+
Delivers a MetricMeasurement to all registered exporters.
|
|
78
|
+
|
|
79
|
+
:param measurement: The MetricMeasurement to deliver.
|
|
80
|
+
"""
|
|
81
|
+
for exporter in self._exporters:
|
|
82
|
+
exporter.export([measurement])
|
|
83
|
+
|
|
84
|
+
return
|
|
85
|
+
|
|
86
|
+
def increment_counter(self, metric_name: str, value: float = 1.0,
|
|
87
|
+
labels: dict = None) -> None:
|
|
88
|
+
"""
|
|
89
|
+
Records a counter increment for the specified metric.
|
|
90
|
+
|
|
91
|
+
Validates that the metric is registered and has type COUNTER.
|
|
92
|
+
Adds the value to the accumulated counter total and delivers
|
|
93
|
+
a MetricMeasurement to all registered exporters.
|
|
94
|
+
|
|
95
|
+
:param metric_name: The name of the counter metric.
|
|
96
|
+
:param value: The increment value as a float, defaulting to 1.0.
|
|
97
|
+
:param labels: Optional dictionary of string key-value labels.
|
|
98
|
+
|
|
99
|
+
:raises SemanticError: If the metric is not registered or
|
|
100
|
+
is not a COUNTER type.
|
|
101
|
+
"""
|
|
102
|
+
self._validate_metric(metric_name, MetricType.COUNTER)
|
|
103
|
+
|
|
104
|
+
if metric_name not in self._counter_values:
|
|
105
|
+
self._counter_values[metric_name] = 0.0
|
|
106
|
+
|
|
107
|
+
self._counter_values[metric_name] += value
|
|
108
|
+
|
|
109
|
+
measurement = MetricMeasurement(
|
|
110
|
+
metric_name=metric_name,
|
|
111
|
+
value=value,
|
|
112
|
+
labels=labels
|
|
113
|
+
)
|
|
114
|
+
self._deliver_to_exporters(measurement)
|
|
115
|
+
|
|
116
|
+
return
|
|
117
|
+
|
|
118
|
+
def set_gauge(self, metric_name: str, value: float,
|
|
119
|
+
labels: dict = None) -> None:
|
|
120
|
+
"""
|
|
121
|
+
Records a gauge value for the specified metric.
|
|
122
|
+
|
|
123
|
+
Validates that the metric is registered and has type GAUGE.
|
|
124
|
+
Sets the gauge value and delivers a MetricMeasurement to
|
|
125
|
+
all registered exporters.
|
|
126
|
+
|
|
127
|
+
:param metric_name: The name of the gauge metric.
|
|
128
|
+
:param value: The gauge value as a float.
|
|
129
|
+
:param labels: Optional dictionary of string key-value labels.
|
|
130
|
+
|
|
131
|
+
:raises SemanticError: If the metric is not registered or
|
|
132
|
+
is not a GAUGE type.
|
|
133
|
+
"""
|
|
134
|
+
self._validate_metric(metric_name, MetricType.GAUGE)
|
|
135
|
+
|
|
136
|
+
self._gauge_values[metric_name] = value
|
|
137
|
+
|
|
138
|
+
measurement = MetricMeasurement(
|
|
139
|
+
metric_name=metric_name,
|
|
140
|
+
value=value,
|
|
141
|
+
labels=labels
|
|
142
|
+
)
|
|
143
|
+
self._deliver_to_exporters(measurement)
|
|
144
|
+
|
|
145
|
+
return
|
|
146
|
+
|
|
147
|
+
def record_histogram(self, metric_name: str, value: float,
|
|
148
|
+
labels: dict = None) -> None:
|
|
149
|
+
"""
|
|
150
|
+
Records a histogram observation for the specified metric.
|
|
151
|
+
|
|
152
|
+
Validates that the metric is registered and has type HISTOGRAM.
|
|
153
|
+
Appends the value to the histogram observations list and
|
|
154
|
+
delivers a MetricMeasurement to all registered exporters.
|
|
155
|
+
|
|
156
|
+
:param metric_name: The name of the histogram metric.
|
|
157
|
+
:param value: The observation value as a float.
|
|
158
|
+
:param labels: Optional dictionary of string key-value labels.
|
|
159
|
+
|
|
160
|
+
:raises SemanticError: If the metric is not registered or
|
|
161
|
+
is not a HISTOGRAM type.
|
|
162
|
+
"""
|
|
163
|
+
self._validate_metric(metric_name, MetricType.HISTOGRAM)
|
|
164
|
+
|
|
165
|
+
if metric_name not in self._histogram_values:
|
|
166
|
+
self._histogram_values[metric_name] = []
|
|
167
|
+
|
|
168
|
+
self._histogram_values[metric_name].append(value)
|
|
169
|
+
|
|
170
|
+
measurement = MetricMeasurement(
|
|
171
|
+
metric_name=metric_name,
|
|
172
|
+
value=value,
|
|
173
|
+
labels=labels
|
|
174
|
+
)
|
|
175
|
+
self._deliver_to_exporters(measurement)
|
|
176
|
+
|
|
177
|
+
return
|
|
178
|
+
|
|
179
|
+
def snapshot(self) -> MetricsSnapshot:
|
|
180
|
+
"""
|
|
181
|
+
Returns a MetricsSnapshot containing the current values
|
|
182
|
+
of all recorded metrics.
|
|
183
|
+
|
|
184
|
+
Counter metrics are represented by their accumulated total.
|
|
185
|
+
Gauge metrics are represented by their current value.
|
|
186
|
+
Histogram metrics are represented by their list of observations.
|
|
187
|
+
|
|
188
|
+
:returns: A MetricsSnapshot instance with current metric values.
|
|
189
|
+
"""
|
|
190
|
+
metrics = {}
|
|
191
|
+
|
|
192
|
+
for name, value in self._counter_values.items():
|
|
193
|
+
metrics[name] = {"value": value, "type": "COUNTER"}
|
|
194
|
+
|
|
195
|
+
for name, value in self._gauge_values.items():
|
|
196
|
+
metrics[name] = {"value": value, "type": "GAUGE"}
|
|
197
|
+
|
|
198
|
+
for name, values in self._histogram_values.items():
|
|
199
|
+
metrics[name] = {"values": list(values), "type": "HISTOGRAM"}
|
|
200
|
+
|
|
201
|
+
snapshot = MetricsSnapshot(
|
|
202
|
+
timestamp=datetime.utcnow(),
|
|
203
|
+
metrics=metrics
|
|
204
|
+
)
|
|
205
|
+
return snapshot
|
|
206
|
+
|
|
207
|
+
def reset(self) -> None:
|
|
208
|
+
"""
|
|
209
|
+
Clears all accumulated metric values.
|
|
210
|
+
|
|
211
|
+
Resets counter, gauge, and histogram value dictionaries
|
|
212
|
+
to empty.
|
|
213
|
+
"""
|
|
214
|
+
self._counter_values = {}
|
|
215
|
+
self._gauge_values = {}
|
|
216
|
+
self._histogram_values = {}
|
|
217
|
+
|
|
218
|
+
return
|
|
219
|
+
|
|
220
|
+
def register_exporter(self, exporter) -> None:
|
|
221
|
+
"""
|
|
222
|
+
Registers a MetricsExporter instance to receive metric
|
|
223
|
+
measurements.
|
|
224
|
+
|
|
225
|
+
:param exporter: The MetricsExporter instance to register.
|
|
226
|
+
"""
|
|
227
|
+
self._exporters.append(exporter)
|
|
228
|
+
|
|
229
|
+
return
|
|
230
|
+
|
|
231
|
+
def __repr__(self) -> str:
|
|
232
|
+
"""
|
|
233
|
+
Returns a detailed string representation including the
|
|
234
|
+
class name and the count of registered metrics.
|
|
235
|
+
|
|
236
|
+
:returns: A repr string for debugging.
|
|
237
|
+
"""
|
|
238
|
+
repr_str = "MetricsCollector(registry_count={})".format(
|
|
239
|
+
self._registry.count
|
|
240
|
+
)
|
|
241
|
+
return repr_str
|
|
242
|
+
|
|
243
|
+
def __str__(self) -> str:
|
|
244
|
+
"""
|
|
245
|
+
Returns a human-readable summary including the count of
|
|
246
|
+
registered metrics and the count of recorded measurements.
|
|
247
|
+
|
|
248
|
+
:returns: A human-readable string.
|
|
249
|
+
"""
|
|
250
|
+
total_measurements = (
|
|
251
|
+
len(self._counter_values) +
|
|
252
|
+
len(self._gauge_values) +
|
|
253
|
+
len(self._histogram_values)
|
|
254
|
+
)
|
|
255
|
+
str_val = (
|
|
256
|
+
"MetricsCollector: registry_count={}, recorded_metrics={}"
|
|
257
|
+
).format(self._registry.count, total_measurements)
|
|
258
|
+
return str_val
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the MetricsExporter abstract base class and concrete exporter
|
|
3
|
+
implementations for exporting MetricMeasurement objects to external
|
|
4
|
+
metrics backends.
|
|
5
|
+
|
|
6
|
+
Provides two concrete exporters:
|
|
7
|
+
- ConsoleMetricsExporter: writes MetricMeasurement objects as
|
|
8
|
+
formatted text to standard output
|
|
9
|
+
- FileMetricsExporter: writes MetricMeasurement objects as JSON
|
|
10
|
+
lines to a specified file path, resolved relative to the
|
|
11
|
+
AppContext directory provided by codecraftai-this-app
|
|
12
|
+
|
|
13
|
+
All exporters accept an optional list of credential reference names
|
|
14
|
+
at construction time. Export failures raise ObservabilityError with
|
|
15
|
+
the exporter class name and underlying error detail.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
import abc
|
|
19
|
+
import os
|
|
20
|
+
import sys
|
|
21
|
+
|
|
22
|
+
from ccai.agentkit.observabilityerror import ObservabilityError
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class MetricsExporter(abc.ABC):
|
|
26
|
+
"""
|
|
27
|
+
Abstract base class for exporting MetricMeasurement objects
|
|
28
|
+
to external metrics backends.
|
|
29
|
+
|
|
30
|
+
Subclasses must implement the export, flush, and shutdown methods
|
|
31
|
+
to handle specific export destinations. Each exporter accepts an
|
|
32
|
+
optional list of credential reference names declaring which
|
|
33
|
+
credentials the exporter requires.
|
|
34
|
+
|
|
35
|
+
:param credential_references: Optional list of credential
|
|
36
|
+
reference name strings declaring which credentials the
|
|
37
|
+
exporter requires.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
def __init__(self, credential_references: list = None) -> None:
|
|
41
|
+
|
|
42
|
+
if credential_references is not None:
|
|
43
|
+
self._credential_references = list(credential_references)
|
|
44
|
+
else:
|
|
45
|
+
self._credential_references = []
|
|
46
|
+
|
|
47
|
+
return
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
def credential_references(self) -> list:
|
|
51
|
+
"""
|
|
52
|
+
The list of credential reference name strings declaring
|
|
53
|
+
which credentials the exporter requires.
|
|
54
|
+
|
|
55
|
+
:returns: A list of credential reference name strings.
|
|
56
|
+
"""
|
|
57
|
+
return list(self._credential_references)
|
|
58
|
+
|
|
59
|
+
@abc.abstractmethod
|
|
60
|
+
def export(self, measurements: list) -> None:
|
|
61
|
+
"""
|
|
62
|
+
Exports a list of MetricMeasurement objects to the
|
|
63
|
+
configured destination.
|
|
64
|
+
|
|
65
|
+
:param measurements: A list of MetricMeasurement objects
|
|
66
|
+
to export.
|
|
67
|
+
|
|
68
|
+
:raises: ObservabilityError
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
@abc.abstractmethod
|
|
72
|
+
def flush(self) -> None:
|
|
73
|
+
"""
|
|
74
|
+
Ensures all buffered measurements are delivered to the
|
|
75
|
+
configured destination.
|
|
76
|
+
|
|
77
|
+
:raises: ObservabilityError
|
|
78
|
+
"""
|
|
79
|
+
|
|
80
|
+
@abc.abstractmethod
|
|
81
|
+
def shutdown(self) -> None:
|
|
82
|
+
"""
|
|
83
|
+
Releases resources held by the exporter.
|
|
84
|
+
|
|
85
|
+
:raises: ObservabilityError
|
|
86
|
+
"""
|
|
87
|
+
|
|
88
|
+
def __repr__(self) -> str:
|
|
89
|
+
"""
|
|
90
|
+
Returns a detailed string representation including the
|
|
91
|
+
class name.
|
|
92
|
+
|
|
93
|
+
:returns: A repr string for debugging.
|
|
94
|
+
"""
|
|
95
|
+
repr_str = "{}()".format(self.__class__.__name__)
|
|
96
|
+
return repr_str
|
|
97
|
+
|
|
98
|
+
def __str__(self) -> str:
|
|
99
|
+
"""
|
|
100
|
+
Returns a human-readable summary including the exporter
|
|
101
|
+
class name.
|
|
102
|
+
|
|
103
|
+
:returns: A human-readable string.
|
|
104
|
+
"""
|
|
105
|
+
str_val = "{}".format(self.__class__.__name__)
|
|
106
|
+
return str_val
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class ConsoleMetricsExporter(MetricsExporter):
|
|
110
|
+
"""
|
|
111
|
+
Writes MetricMeasurement objects as formatted text to standard
|
|
112
|
+
output.
|
|
113
|
+
|
|
114
|
+
Each measurement is formatted as a human-readable text line
|
|
115
|
+
containing the metric name, value, timestamp, and labels.
|
|
116
|
+
The formatted text is written to sys.stdout followed by a
|
|
117
|
+
newline.
|
|
118
|
+
"""
|
|
119
|
+
|
|
120
|
+
def __init__(self) -> None:
|
|
121
|
+
|
|
122
|
+
super().__init__()
|
|
123
|
+
|
|
124
|
+
return
|
|
125
|
+
|
|
126
|
+
def export(self, measurements: list) -> None:
|
|
127
|
+
"""
|
|
128
|
+
Writes a list of MetricMeasurement objects as formatted
|
|
129
|
+
text to standard output.
|
|
130
|
+
|
|
131
|
+
:param measurements: A list of MetricMeasurement objects
|
|
132
|
+
to export.
|
|
133
|
+
|
|
134
|
+
:raises: ObservabilityError
|
|
135
|
+
"""
|
|
136
|
+
try:
|
|
137
|
+
for measurement in measurements:
|
|
138
|
+
formatted = (
|
|
139
|
+
"MetricMeasurement: metric_name={}, value={}, "
|
|
140
|
+
"timestamp={}, labels={}"
|
|
141
|
+
).format(
|
|
142
|
+
measurement.metric_name,
|
|
143
|
+
measurement.value,
|
|
144
|
+
measurement.timestamp.isoformat(),
|
|
145
|
+
measurement.labels
|
|
146
|
+
)
|
|
147
|
+
sys.stdout.write(formatted + "\n")
|
|
148
|
+
except Exception as exc:
|
|
149
|
+
raise ObservabilityError(
|
|
150
|
+
component_name=self.__class__.__name__,
|
|
151
|
+
operation_name="export",
|
|
152
|
+
message="Failed to export measurements to console: {}".format(
|
|
153
|
+
str(exc)
|
|
154
|
+
),
|
|
155
|
+
cause=exc
|
|
156
|
+
) from exc
|
|
157
|
+
|
|
158
|
+
return
|
|
159
|
+
|
|
160
|
+
def flush(self) -> None:
|
|
161
|
+
"""
|
|
162
|
+
Flushes standard output to ensure all buffered measurements
|
|
163
|
+
are delivered.
|
|
164
|
+
|
|
165
|
+
:raises: ObservabilityError
|
|
166
|
+
"""
|
|
167
|
+
try:
|
|
168
|
+
sys.stdout.flush()
|
|
169
|
+
except Exception as exc:
|
|
170
|
+
raise ObservabilityError(
|
|
171
|
+
component_name=self.__class__.__name__,
|
|
172
|
+
operation_name="flush",
|
|
173
|
+
message="Failed to flush console output: {}".format(str(exc)),
|
|
174
|
+
cause=exc
|
|
175
|
+
) from exc
|
|
176
|
+
|
|
177
|
+
return
|
|
178
|
+
|
|
179
|
+
def shutdown(self) -> None:
|
|
180
|
+
"""
|
|
181
|
+
Releases resources held by the console exporter.
|
|
182
|
+
|
|
183
|
+
The ConsoleMetricsExporter does not hold resources that
|
|
184
|
+
require explicit release, so this method performs no
|
|
185
|
+
action.
|
|
186
|
+
"""
|
|
187
|
+
return
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class FileMetricsExporter(MetricsExporter):
|
|
191
|
+
"""
|
|
192
|
+
Writes MetricMeasurement objects as JSON lines to a specified
|
|
193
|
+
file path, resolved relative to the AppContext directory provided
|
|
194
|
+
by codecraftai-this-app.
|
|
195
|
+
|
|
196
|
+
Each measurement is serialized as a single-line JSON object and
|
|
197
|
+
appended to the target file. If an app_context is provided, the
|
|
198
|
+
file_path is resolved relative to the app_context directory.
|
|
199
|
+
Otherwise, the file_path is used as-is.
|
|
200
|
+
|
|
201
|
+
:param file_path: The file path string where measurement JSON
|
|
202
|
+
lines are written.
|
|
203
|
+
:param app_context: Optional AppContext instance providing the
|
|
204
|
+
directory for resolving relative file paths.
|
|
205
|
+
"""
|
|
206
|
+
|
|
207
|
+
def __init__(self, file_path: str, app_context=None) -> None:
|
|
208
|
+
|
|
209
|
+
super().__init__()
|
|
210
|
+
|
|
211
|
+
self._file_path = file_path
|
|
212
|
+
self._app_context = app_context
|
|
213
|
+
self._resolved_path = self._resolve_path()
|
|
214
|
+
|
|
215
|
+
return
|
|
216
|
+
|
|
217
|
+
def _resolve_path(self) -> str:
|
|
218
|
+
"""
|
|
219
|
+
Resolves the file path relative to the AppContext directory
|
|
220
|
+
if an app_context is provided, otherwise returns the
|
|
221
|
+
file_path as-is.
|
|
222
|
+
|
|
223
|
+
:returns: The resolved file path string.
|
|
224
|
+
"""
|
|
225
|
+
if self._app_context is not None:
|
|
226
|
+
resolved = os.path.join(self._app_context.directory, self._file_path)
|
|
227
|
+
else:
|
|
228
|
+
resolved = self._file_path
|
|
229
|
+
|
|
230
|
+
return resolved
|
|
231
|
+
|
|
232
|
+
def export(self, measurements: list) -> None:
|
|
233
|
+
"""
|
|
234
|
+
Writes a list of MetricMeasurement objects as JSON lines
|
|
235
|
+
to the configured file path.
|
|
236
|
+
|
|
237
|
+
Each measurement is serialized using its to_json method and
|
|
238
|
+
appended as a single line to the target file.
|
|
239
|
+
|
|
240
|
+
:param measurements: A list of MetricMeasurement objects
|
|
241
|
+
to export.
|
|
242
|
+
|
|
243
|
+
:raises: ObservabilityError
|
|
244
|
+
"""
|
|
245
|
+
try:
|
|
246
|
+
with open(self._resolved_path, "a") as file_handle:
|
|
247
|
+
for measurement in measurements:
|
|
248
|
+
json_line = measurement.to_json()
|
|
249
|
+
file_handle.write(json_line + "\n")
|
|
250
|
+
except Exception as exc:
|
|
251
|
+
raise ObservabilityError(
|
|
252
|
+
component_name=self.__class__.__name__,
|
|
253
|
+
operation_name="export",
|
|
254
|
+
message="Failed to export measurements to file {!r}: {}".format(
|
|
255
|
+
self._resolved_path, str(exc)
|
|
256
|
+
),
|
|
257
|
+
cause=exc
|
|
258
|
+
) from exc
|
|
259
|
+
|
|
260
|
+
return
|
|
261
|
+
|
|
262
|
+
def flush(self) -> None:
|
|
263
|
+
"""
|
|
264
|
+
Flushes the file to ensure all buffered measurements are
|
|
265
|
+
delivered. Since each export call opens and closes the
|
|
266
|
+
file, this method performs no additional action.
|
|
267
|
+
"""
|
|
268
|
+
return
|
|
269
|
+
|
|
270
|
+
def shutdown(self) -> None:
|
|
271
|
+
"""
|
|
272
|
+
Releases resources held by the file exporter.
|
|
273
|
+
|
|
274
|
+
The FileMetricsExporter opens and closes the file on each
|
|
275
|
+
export call, so this method performs no additional action.
|
|
276
|
+
"""
|
|
277
|
+
return
|
|
278
|
+
|
|
279
|
+
def __repr__(self) -> str:
|
|
280
|
+
"""
|
|
281
|
+
Returns a detailed string representation including the
|
|
282
|
+
class name and file path.
|
|
283
|
+
|
|
284
|
+
:returns: A repr string for debugging.
|
|
285
|
+
"""
|
|
286
|
+
repr_str = "{}(file_path={!r})".format(
|
|
287
|
+
self.__class__.__name__,
|
|
288
|
+
self._resolved_path
|
|
289
|
+
)
|
|
290
|
+
return repr_str
|
|
291
|
+
|
|
292
|
+
def __str__(self) -> str:
|
|
293
|
+
"""
|
|
294
|
+
Returns a human-readable summary including the exporter
|
|
295
|
+
class name and file path.
|
|
296
|
+
|
|
297
|
+
:returns: A human-readable string.
|
|
298
|
+
"""
|
|
299
|
+
str_val = "{}: file_path={}".format(
|
|
300
|
+
self.__class__.__name__,
|
|
301
|
+
self._resolved_path
|
|
302
|
+
)
|
|
303
|
+
return str_val
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the MetricsRegistry class that tracks all MetricDefinition
|
|
3
|
+
instances available to an Agent.
|
|
4
|
+
|
|
5
|
+
The registry provides registration, deregistration, lookup by name,
|
|
6
|
+
enumeration, and count of registered metric definitions. Duplicate
|
|
7
|
+
name registration raises SemanticError.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from typing import Optional
|
|
11
|
+
|
|
12
|
+
from ccai.agentkit.errors import SemanticError
|
|
13
|
+
from ccai.agentkit.metricdefinition import MetricDefinition
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class MetricsRegistry:
|
|
17
|
+
"""
|
|
18
|
+
A registry that tracks all MetricDefinition instances available
|
|
19
|
+
to an Agent, providing lookup by name and enumeration.
|
|
20
|
+
|
|
21
|
+
Metric definitions are stored in a dictionary keyed by metric
|
|
22
|
+
name. Attempting to register a definition with a name that
|
|
23
|
+
already exists raises a SemanticError.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(self) -> None:
|
|
27
|
+
|
|
28
|
+
self._definitions = {}
|
|
29
|
+
|
|
30
|
+
return
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def count(self) -> int:
|
|
34
|
+
"""
|
|
35
|
+
The count of currently registered metric definitions.
|
|
36
|
+
|
|
37
|
+
:returns: The number of registered metrics as an integer.
|
|
38
|
+
"""
|
|
39
|
+
return len(self._definitions)
|
|
40
|
+
|
|
41
|
+
def register(self, definition: MetricDefinition) -> None:
|
|
42
|
+
"""
|
|
43
|
+
Registers a MetricDefinition instance using the metric name
|
|
44
|
+
as the registration key.
|
|
45
|
+
|
|
46
|
+
:param definition: The MetricDefinition instance to register.
|
|
47
|
+
|
|
48
|
+
:raises SemanticError: If a MetricDefinition with the same
|
|
49
|
+
name is already registered.
|
|
50
|
+
"""
|
|
51
|
+
name = definition.name
|
|
52
|
+
|
|
53
|
+
if name in self._definitions:
|
|
54
|
+
raise SemanticError(
|
|
55
|
+
"Duplicate metric name: {!r} is already registered".format(name)
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
self._definitions[name] = definition
|
|
59
|
+
|
|
60
|
+
return
|
|
61
|
+
|
|
62
|
+
def deregister(self, name: str) -> None:
|
|
63
|
+
"""
|
|
64
|
+
Removes a MetricDefinition from the registry by name.
|
|
65
|
+
|
|
66
|
+
If the name is not found, the method silently returns
|
|
67
|
+
without raising an error.
|
|
68
|
+
|
|
69
|
+
:param name: The metric name string to deregister.
|
|
70
|
+
"""
|
|
71
|
+
self._definitions.pop(name, None)
|
|
72
|
+
|
|
73
|
+
return
|
|
74
|
+
|
|
75
|
+
def lookup(self, name: str) -> Optional[MetricDefinition]:
|
|
76
|
+
"""
|
|
77
|
+
Looks up a registered MetricDefinition by name.
|
|
78
|
+
|
|
79
|
+
:param name: The metric name string to look up.
|
|
80
|
+
|
|
81
|
+
:returns: The MetricDefinition instance if found, or None
|
|
82
|
+
when the name is not registered.
|
|
83
|
+
"""
|
|
84
|
+
result = self._definitions.get(name, None)
|
|
85
|
+
return result
|
|
86
|
+
|
|
87
|
+
def has_metric(self, name: str) -> bool:
|
|
88
|
+
"""
|
|
89
|
+
Checks whether a MetricDefinition with the given name is
|
|
90
|
+
registered.
|
|
91
|
+
|
|
92
|
+
:param name: The metric name string to check.
|
|
93
|
+
|
|
94
|
+
:returns: True if the metric is registered, False otherwise.
|
|
95
|
+
"""
|
|
96
|
+
result = name in self._definitions
|
|
97
|
+
return result
|
|
98
|
+
|
|
99
|
+
def list_all(self) -> list:
|
|
100
|
+
"""
|
|
101
|
+
Returns a list of all registered MetricDefinition instances.
|
|
102
|
+
|
|
103
|
+
:returns: A list of MetricDefinition objects.
|
|
104
|
+
"""
|
|
105
|
+
result = list(self._definitions.values())
|
|
106
|
+
return result
|
|
107
|
+
|
|
108
|
+
def __repr__(self) -> str:
|
|
109
|
+
"""
|
|
110
|
+
Returns a detailed string representation including the
|
|
111
|
+
class name and the count of registered metrics.
|
|
112
|
+
|
|
113
|
+
:returns: A repr string for debugging.
|
|
114
|
+
"""
|
|
115
|
+
repr_str = "MetricsRegistry(count={})".format(len(self._definitions))
|
|
116
|
+
return repr_str
|
|
117
|
+
|
|
118
|
+
def __str__(self) -> str:
|
|
119
|
+
"""
|
|
120
|
+
Returns a human-readable summary listing the registered
|
|
121
|
+
metric names and types.
|
|
122
|
+
|
|
123
|
+
:returns: A human-readable string.
|
|
124
|
+
"""
|
|
125
|
+
entries = []
|
|
126
|
+
for name, defn in self._definitions.items():
|
|
127
|
+
entries.append("{}({})".format(name, defn.metric_type.value))
|
|
128
|
+
|
|
129
|
+
str_val = "MetricsRegistry: metrics=[{}]".format(", ".join(entries))
|
|
130
|
+
return str_val
|