@@ -154,63 +154,69 @@ private static Set<String> immutableLabelNames(@Nullable Set<String> labelNames)
154154 }
155155
156156 public void register (Collector collector ) {
157- if (collectors .contains (collector )) {
157+ if (! collectors .add (collector )) {
158158 throw new IllegalArgumentException ("Collector instance is already registered" );
159159 }
160-
161- String prometheusName = collector .getPrometheusName ();
162- MetricType metricType = collector .getMetricType ();
163- Set <String > normalizedLabels = immutableLabelNames (collector .getLabelNames ());
164- MetricMetadata metadata = collector .getMetadata ();
165- String help = metadata != null ? metadata .getHelp () : null ;
166- Unit unit = metadata != null ? metadata .getUnit () : null ;
167-
168- // Only perform validation if collector provides sufficient metadata.
169- // Collectors that don't implement getPrometheusName()/getMetricType() will skip validation.
170- if (prometheusName != null && metricType != null ) {
171- final String name = prometheusName ;
172- final MetricType type = metricType ;
173- final Set <String > names = normalizedLabels ;
174- final String helpForValidation = help ;
175- final Unit unitForValidation = unit ;
176- registered .compute (
177- prometheusName ,
178- (n , existingInfo ) -> {
179- if (existingInfo == null ) {
180- return RegistrationInfo .of (type , names , helpForValidation , unitForValidation );
181- } else {
182- if (existingInfo .getType () != type ) {
183- throw new IllegalArgumentException (
184- name
185- + ": Conflicting metric types. Existing: "
186- + existingInfo .getType ()
187- + ", new: "
188- + type );
189- }
190- existingInfo .validateMetadata (helpForValidation , unitForValidation );
191- if (!existingInfo .addLabelSet (names )) {
192- throw new IllegalArgumentException (
193- name + ": duplicate metric name with identical label schema " + names );
160+ try {
161+ String prometheusName = collector .getPrometheusName ();
162+ MetricType metricType = collector .getMetricType ();
163+ Set <String > normalizedLabels = immutableLabelNames (collector .getLabelNames ());
164+ MetricMetadata metadata = collector .getMetadata ();
165+ String help = metadata != null ? metadata .getHelp () : null ;
166+ Unit unit = metadata != null ? metadata .getUnit () : null ;
167+
168+ // Only perform validation if collector provides sufficient metadata.
169+ // Collectors that don't implement getPrometheusName()/getMetricType() will skip validation.
170+ if (prometheusName != null && metricType != null ) {
171+ final String name = prometheusName ;
172+ final MetricType type = metricType ;
173+ final Set <String > names = normalizedLabels ;
174+ final String helpForValidation = help ;
175+ final Unit unitForValidation = unit ;
176+ registered .compute (
177+ prometheusName ,
178+ (n , existingInfo ) -> {
179+ if (existingInfo == null ) {
180+ return RegistrationInfo .of (type , names , helpForValidation , unitForValidation );
181+ } else {
182+ if (existingInfo .getType () != type ) {
183+ throw new IllegalArgumentException (
184+ name
185+ + ": Conflicting metric types. Existing: "
186+ + existingInfo .getType ()
187+ + ", new: "
188+ + type );
189+ }
190+ existingInfo .validateMetadata (helpForValidation , unitForValidation );
191+ if (!existingInfo .addLabelSet (names )) {
192+ throw new IllegalArgumentException (
193+ name + ": duplicate metric name with identical label schema " + names );
194+ }
195+ return existingInfo ;
194196 }
195- return existingInfo ;
196- }
197- });
197+ });
198198
199- collectorMetadata .put (collector , new CollectorRegistration (prometheusName , normalizedLabels ));
200- }
199+ collectorMetadata .put (
200+ collector , new CollectorRegistration (prometheusName , normalizedLabels ));
201+ }
201202
202- if (prometheusName != null ) {
203- prometheusNames .add (prometheusName );
203+ if (prometheusName != null ) {
204+ prometheusNames .add (prometheusName );
205+ }
206+ } catch (Exception e ) {
207+ collectors .remove (collector );
208+ CollectorRegistration reg = collectorMetadata .remove (collector );
209+ if (reg != null && reg .prometheusName != null ) {
210+ unregisterLabelSchema (reg .prometheusName , reg .labelNames );
211+ }
212+ throw e ;
204213 }
205-
206- collectors .add (collector );
207214 }
208215
209216 public void register (MultiCollector collector ) {
210- if (multiCollectors .contains (collector )) {
217+ if (! multiCollectors .add (collector )) {
211218 throw new IllegalArgumentException ("MultiCollector instance is already registered" );
212219 }
213-
214220 List <String > prometheusNamesList = collector .getPrometheusNames ();
215221 List <MultiCollectorRegistration > registrations = new ArrayList <>();
216222 Set <String > namesOnlyInPrometheusNames = new HashSet <>();
@@ -264,8 +270,8 @@ public void register(MultiCollector collector) {
264270 }
265271
266272 multiCollectorMetadata .put (collector , registrations );
267- multiCollectors .add (collector );
268273 } catch (Exception e ) {
274+ multiCollectors .remove (collector );
269275 for (MultiCollectorRegistration registration : registrations ) {
270276 unregisterLabelSchema (registration .prometheusName , registration .labelNames );
271277 }
0 commit comments