@@ -62,35 +62,38 @@ func MetricsModule(cfg metrics.ModuleConfig) fx.Option {
6262
6363 return ret
6464 }, fx .ParamTags (metricsProviderOptionKey ))),
65- fx .Invoke (func (lc fx.Lifecycle , metricProvider * sdkmetric.MeterProvider , options ... runtime.Option ) {
66- otel .SetTextMapPropagator (propagation .NewCompositeTextMapPropagator (
67- b3 .New (), propagation.TraceContext {}))
68- lc .Append (fx.Hook {
69- OnStart : func (ctx context.Context ) error {
70- if cfg .RuntimeMetrics {
71- if err := runtime .Start (options ... ); err != nil {
72- return err
65+ fx .Invoke (fx .Annotate (
66+ func (lc fx.Lifecycle , metricProvider * sdkmetric.MeterProvider , options ... runtime.Option ) {
67+ otel .SetTextMapPropagator (propagation .NewCompositeTextMapPropagator (
68+ b3 .New (), propagation.TraceContext {}))
69+ lc .Append (fx.Hook {
70+ OnStart : func (ctx context.Context ) error {
71+ if cfg .RuntimeMetrics {
72+ if err := runtime .Start (options ... ); err != nil {
73+ return err
74+ }
75+ if err := host .Start (); err != nil {
76+ return err
77+ }
7378 }
74- if err := host .Start (); err != nil {
75- return err
79+ return nil
80+ },
81+ OnStop : func (ctx context.Context ) error {
82+ logging .FromContext (ctx ).Infof ("Flush metrics" )
83+ if err := metricProvider .ForceFlush (ctx ); err != nil {
84+ logging .FromContext (ctx ).Errorf ("unable to flush metrics: %s" , err )
7685 }
77- }
78- return nil
79- },
80- OnStop : func (ctx context.Context ) error {
81- logging .FromContext (ctx ).Infof ("Flush metrics" )
82- if err := metricProvider .ForceFlush (ctx ); err != nil {
83- logging .FromContext (ctx ).Errorf ("unable to flush metrics: %s" , err )
84- }
85- logging .FromContext (ctx ).Infof ("Shutting down metrics provider" )
86- if err := metricProvider .Shutdown (ctx ); err != nil {
87- logging .FromContext (ctx ).Errorf ("unable to shutdown metrics provider: %s" , err )
88- }
89- logging .FromContext (ctx ).Infof ("Metrics provider stopped" )
90- return nil
91- },
92- })
93- }),
86+ logging .FromContext (ctx ).Infof ("Shutting down metrics provider" )
87+ if err := metricProvider .Shutdown (ctx ); err != nil {
88+ logging .FromContext (ctx ).Errorf ("unable to shutdown metrics provider: %s" , err )
89+ }
90+ logging .FromContext (ctx ).Infof ("Metrics provider stopped" )
91+ return nil
92+ },
93+ })
94+ },
95+ fx .ParamTags (`` , `` , metricsRuntimeOptionKey ),
96+ )),
9497 ProvideMetricsProviderOption (sdkmetric .WithResource ),
9598 ProvideMetricsProviderOption (sdkmetric .WithReader ),
9699 fx .Provide (
@@ -99,10 +102,12 @@ func MetricsModule(cfg metrics.ModuleConfig) fx.Option {
99102 ProvideOTLPMetricsPeriodicReaderOption (func () sdkmetric.PeriodicReaderOption {
100103 return sdkmetric .WithInterval (cfg .PushInterval )
101104 }),
102- ProvideRuntimeMetricsOption (func () runtime.Option {
103- return runtime .WithMinimumReadMemStatsInterval (cfg .MinimumReadMemStatsInterval )
104- }),
105105 )
106+ if cfg .MinimumReadMemStatsInterval > 0 {
107+ options = append (options , ProvideRuntimeMetricsOption (func () runtime.Option {
108+ return runtime .WithMinimumReadMemStatsInterval (cfg .MinimumReadMemStatsInterval )
109+ }))
110+ }
106111
107112 switch cfg .Exporter {
108113 case metrics .StdoutExporter :
0 commit comments