@@ -485,4 +485,52 @@ test_registry_task_duration_standard_deviation{instance="test",type="create_tabl
485485 registry.deregister_entity (entity);
486486 }
487487}
488+ TEST_F (MetricsTest, PrometheusDuplicateTypeFix) {
489+ MetricRegistry registry (" test_registry" );
490+
491+ // We use a specific scope {} to match the official coding style
492+ {
493+ // 1. Create the first entity and register a metric with a group name
494+ auto entity1 = registry.register_entity (" entity1" );
495+ // Using MetricPrototype is the "official way" in Doris metrics_test
496+ MetricPrototype requests_type (MetricType::COUNTER, MetricUnit::OPERATIONS, " requests_total" , " " , " engine_requests" );
497+ IntCounter* m1 = (IntCounter*)entity1->register_metric <IntCounter>(&requests_type);
498+ m1->increment (1 );
499+
500+ // 2. Create the second entity and register a metric with a DIFFERENT name to break continuity
501+ auto entity2 = registry.register_entity (" entity2" );
502+ MetricPrototype other_type (MetricType::COUNTER, MetricUnit::OPERATIONS, " other_metric" , " " , " " );
503+ IntCounter* m2 = (IntCounter*)entity2->register_metric <IntCounter>(&other_type); // 修复:使用 register_metric
504+ m2->increment (5 );
505+
506+ // 3. Create the third entity and register the SAME group name metric again
507+ // In the old logic, this would trigger a duplicate # TYPE line
508+ auto entity3 = registry.register_entity (" entity3" );
509+ IntCounter* m3 = (IntCounter*)entity3->register_metric <IntCounter>(&requests_type);
510+ m3->increment (10 );
511+
512+ // Execute export
513+ std::string output = registry.to_prometheus ();
514+
515+ // Verification: Count the occurrences of "# TYPE test_registry_engine_requests counter"
516+ // In your official snippet, the format is: # TYPE {registry_name}_{group_name} {type}
517+ std::string target_type_line = " # TYPE test_registry_engine_requests counter" ;
518+
519+ int occurrences = 0 ;
520+ size_t pos = 0 ; // 修复:使用 size_t 而不是 std::string::size_t
521+ while ((pos = output.find (target_type_line, pos)) != std::string::npos) {
522+ occurrences++;
523+ pos += target_type_line.length ();
524+ }
525+
526+ // Must be exactly 1
527+ EXPECT_EQ (1 , occurrences) << " Duplicate TYPE line detected for interleaved metrics!" ;
528+
529+ // Cleanup: Following the official style to deregister entities
530+ registry.deregister_entity (entity1);
531+ registry.deregister_entity (entity2);
532+ registry.deregister_entity (entity3);
533+ }
534+ }
488535} // namespace doris
536+
0 commit comments