@@ -464,7 +464,7 @@ static void cb_send_metric_chunk(struct flb_config *config, void *data)
464464 if (ctx -> cmt == NULL || ctx -> input_ins == NULL ) {
465465 return ;
466466 }
467-
467+
468468 if (ctx -> new_data ) {
469469 ret = flb_input_metrics_append (ctx -> input_ins , ctx -> tag ,
470470 strlen (ctx -> tag ), ctx -> cmt );
@@ -486,6 +486,7 @@ static void cb_send_metric_chunk(struct flb_config *config, void *data)
486486static int cb_log_to_metrics_init (struct flb_filter_instance * f_ins ,
487487 struct flb_config * config , void * data )
488488{
489+ int i ;
489490 int ret ;
490491 struct log_to_metrics_ctx * ctx ;
491492 flb_sds_t tmp ;
@@ -498,63 +499,56 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
498499 struct flb_sched * sched ;
499500
500501
501- int i ;
502502 /* Create context */
503503 ctx = flb_calloc (1 , sizeof (struct log_to_metrics_ctx ));
504504 if (!ctx ) {
505505 flb_errno ();
506506 return -1 ;
507507 }
508508 ctx -> ins = f_ins ;
509+ mk_list_init (& ctx -> rules );
510+
511+ /* Set the context */
512+ flb_filter_set_context (f_ins , ctx );
509513
510514 if (flb_filter_config_map_set (f_ins , ctx ) < 0 ) {
511515 flb_errno ();
512516 flb_plg_error (f_ins , "configuration error" );
513- flb_free (ctx );
514517 return -1 ;
515518 }
516- mk_list_init (& ctx -> rules );
517519
518520 if (ctx -> metric_name == NULL ) {
519521 flb_plg_error (f_ins , "metric_name is not set" );
520- log_to_metrics_destroy (ctx );
521522 return -1 ;
522523 }
523524
524525 /* Load rules */
525526 ret = set_rules (ctx , f_ins );
526527 if (ret == -1 ) {
527- flb_free (ctx );
528528 return -1 ;
529529 }
530530
531- /* Set the context */
532- flb_filter_set_context (f_ins , ctx );
533-
534531 /* Set buckets for histogram */
535532 ctx -> buckets = NULL ;
536533 ctx -> bucket_counter = 0 ;
537534 ctx -> histogram_buckets = NULL ;
538535
539536 if (set_buckets (ctx , f_ins ) < 0 ) {
540537 flb_plg_error (f_ins , "Setting buckets failed" );
541- log_to_metrics_destroy (ctx );
542538 return -1 ;
543539 }
544540
545541 ctx -> label_accessors = NULL ;
546542 ctx -> label_accessors = (char * * ) flb_calloc (1 , MAX_LABEL_COUNT * sizeof (char * ));
547543 if (!ctx -> label_accessors ) {
548544 flb_errno ();
549- log_to_metrics_destroy (ctx );
550545 return -1 ;
551546 }
552547
553548 for (i = 0 ; i < MAX_LABEL_COUNT ; i ++ ) {
554549 ctx -> label_accessors [i ] = flb_calloc (1 , MAX_LABEL_LENGTH * sizeof (char ));
555550 if (!ctx -> label_accessors [i ]) {
556551 flb_errno ();
557- log_to_metrics_destroy (ctx );
558552 return -1 ;
559553 }
560554 }
@@ -565,22 +559,19 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
565559 ctx -> label_keys [i ] = flb_calloc (1 , MAX_LABEL_LENGTH * sizeof (char ));
566560 if (!ctx -> label_keys [i ]) {
567561 flb_errno ();
568- log_to_metrics_destroy (ctx );
569562 return -1 ;
570563 }
571564 }
572565
573566 ret = set_labels (ctx , ctx -> label_accessors , ctx -> label_keys , f_ins );
574567 if (ret < 0 ){
575- log_to_metrics_destroy (ctx );
576568 return -1 ;
577569 }
578570 ctx -> label_counter = ret ;
579571
580572 /* Check metric tag */
581573 if (ctx -> tag == NULL || strlen (ctx -> tag ) == 0 ) {
582574 flb_plg_error (f_ins , "Metric tag is not set" );
583- log_to_metrics_destroy (ctx );
584575 return -1 ;
585576 }
586577
@@ -604,13 +595,11 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
604595 "invalid 'mode' value. Only "
605596 "'counter', 'gauge' or "
606597 "'histogram' types are allowed" );
607- log_to_metrics_destroy (ctx );
608598 return -1 ;
609599 }
610600 }
611601 else {
612602 flb_plg_error (f_ins , "configuration property not set" );
613- log_to_metrics_destroy (ctx );
614603 return -1 ;
615604 }
616605
@@ -637,7 +626,6 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
637626 if (ctx -> metric_description == NULL ||
638627 strlen (ctx -> metric_description ) == 0 ) {
639628 flb_plg_error (f_ins , "metric_description is not set" );
640- log_to_metrics_destroy (ctx );
641629 return -1 ;
642630 }
643631 snprintf (metric_description , sizeof (metric_description ) - 1 , "%s" ,
@@ -647,7 +635,6 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
647635 if (ctx -> mode > 0 ) {
648636 if (ctx -> value_field == NULL || strlen (ctx -> value_field ) == 0 ) {
649637 flb_plg_error (f_ins , "value_field is not set" );
650- log_to_metrics_destroy (ctx );
651638 return -1 ;
652639 }
653640 snprintf (value_field , sizeof (value_field ) - 1 , "%s" ,
@@ -656,7 +643,6 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
656643 ctx -> value_ra = flb_ra_create (ctx -> value_field , FLB_TRUE );
657644 if (ctx -> value_ra == NULL ) {
658645 flb_plg_error (f_ins , "invalid record accessor key for value_field" );
659- log_to_metrics_destroy (ctx );
660646 return -1 ;
661647 }
662648 }
@@ -671,7 +657,7 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
671657 "0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0" );
672658 ctx -> histogram_buckets = cmt_histogram_buckets_default_create ();
673659 }
674- else {
660+ else {
675661 ctx -> histogram_buckets = cmt_histogram_buckets_create_size (ctx -> buckets , ctx -> bucket_counter );
676662 }
677663 }
@@ -701,7 +687,6 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
701687 break ;
702688 default :
703689 flb_plg_error (f_ins , "unsupported mode" );
704- log_to_metrics_destroy (ctx );
705690 return -1 ;
706691 }
707692
@@ -721,14 +706,12 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
721706 flb_plg_error (f_ins , "emitter_name '%s' already exists" ,
722707 ctx -> emitter_name );
723708 flb_sds_destroy (ctx -> emitter_name );
724- log_to_metrics_destroy (ctx );
725709 return -1 ;
726710 }
727711 input_ins = flb_input_new (config , "emitter" , NULL , FLB_FALSE );
728712 if (!input_ins ) {
729713 flb_plg_error (f_ins , "cannot create metrics emitter instance" );
730714 flb_sds_destroy (ctx -> emitter_name );
731- log_to_metrics_destroy (ctx );
732715 return -1 ;
733716 }
734717 /* Set the alias for emitter */
@@ -737,7 +720,6 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
737720 flb_plg_warn (ctx -> ins ,
738721 "cannot set emitter_name" );
739722 flb_sds_destroy (ctx -> emitter_name );
740- log_to_metrics_destroy (ctx );
741723 return -1 ;
742724 }
743725
@@ -747,7 +729,6 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
747729 ret = flb_input_set_property (input_ins , "storage.type" , "memory" );
748730 if (ret == -1 ) {
749731 flb_plg_error (f_ins , "cannot set storage type for emitter instance" );
750- log_to_metrics_destroy (ctx );
751732 return -1 ;
752733 }
753734
@@ -761,14 +742,12 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
761742 if (ret == -1 ) {
762743 flb_errno ();
763744 flb_plg_error (f_ins , "cannot initialize metrics emitter instance." );
764- log_to_metrics_destroy (ctx );
765745 return -1 ;
766746 }
767747
768748 ret = flb_storage_input_create (config -> cio , input_ins );
769749 if (ret == -1 ) {
770750 flb_plg_error (ctx -> ins , "cannot initialize storage for metrics stream" );
771- log_to_metrics_destroy (ctx );
772751 return -1 ;
773752 }
774753 ctx -> input_ins = input_ins ;
@@ -786,27 +765,25 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
786765 ctx -> timer_mode = FLB_FALSE ;
787766 return 0 ;
788767 }
789-
768+
790769 /* Initialize timer for scheduled metric updates */
791770 sched = flb_sched_ctx_get ();
792771 if (sched == 0 ) {
793772 flb_plg_error (f_ins , "could not get scheduler context" );
794- log_to_metrics_destroy (ctx );
795773 return -1 ;
796774 }
797775 /* Convert flush_interval_sec and flush_interval_nsec to milliseconds */
798- ctx -> timer_interval = (ctx -> flush_interval_sec * 1000 ) +
776+ ctx -> timer_interval = (ctx -> flush_interval_sec * 1000 ) +
799777 (ctx -> flush_interval_nsec / 1000000 );
800778 flb_plg_debug (ctx -> ins ,
801779 "Creating metric timer with frequency %d ms" ,
802780 ctx -> timer_interval );
803-
781+
804782 ret = flb_sched_timer_cb_create (sched , FLB_SCHED_TIMER_CB_PERM ,
805783 ctx -> timer_interval , cb_send_metric_chunk ,
806784 ctx , & ctx -> timer );
807785 if (ret < 0 ) {
808786 flb_plg_error (f_ins , "could not create timer callback" );
809- log_to_metrics_destroy (ctx );
810787 return -1 ;
811788 }
812789 ctx -> timer_mode = FLB_TRUE ;
@@ -1028,6 +1005,11 @@ static int cb_log_to_metrics_filter(const void *data, size_t bytes,
10281005static int cb_log_to_metrics_exit (void * data , struct flb_config * config )
10291006{
10301007 struct log_to_metrics_ctx * ctx = data ;
1008+
1009+ if (!ctx ) {
1010+ return 0 ;
1011+ }
1012+
10311013 if (ctx -> timer != NULL ) {
10321014 flb_plg_debug (ctx -> ins , "Destroying callback timer" );
10331015 flb_sched_timer_destroy (ctx -> timer );
0 commit comments