Skip to content

ObservableGauge holds disappeared metrics #72099

Closed
dotnet/diagnostics
#3221

Description

@Yustos

Description

Gauges can be distincted by tags, but process holds old gauge with old tags, after this removed from data source.
This behaviour is fine for counters, but make unuseful metrics values for gauges.

Reproduction Steps

Example source code:

using System.Diagnostics.Metrics;

namespace Test
{
	public class Program
	{
		static Meter s_meter = new Meter("HatCo.HatStore", "1.0.0");

		static IEnumerable<Measurement<int>> m;

		static void Main(string[] args)
		{
			s_meter.CreateObservableGauge<int>("orders-pending", () => m);
			m = new Measurement<int>[]
			{
				new Measurement<int>(1, new KeyValuePair<string,object>("Country", "Italy")),
			};
			Console.WriteLine("Change to Spain");
			Console.ReadLine();

			m = new Measurement<int>[]
			{
				new Measurement<int>(1, new KeyValuePair<string,object>("Country", "Spain")),
			};

			Console.WriteLine("Remove Spain, Update Italy");
			Console.ReadLine();

			m = new Measurement<int>[]
			{
				new Measurement<int>(2, new KeyValuePair<string,object>("Country", "Italy")),
			};

			Console.WriteLine("Press any key to exit");
			Console.ReadLine();
		}
	}
}

Expected behavior

Run example and watch its metrics:
dotnet-counters monitor --name ConsoleApp HatCo.HatStore

After process started metrics is:

[HatCo.HatStore]
    orders-pending
        Country=Italy                                  1

Press enter in example console, this will change metrics from Italy to Spain:

[HatCo.HatStore]
    orders-pending
        Country=Spain                                  1

Here is added Spain and removed Italy.

Press enter in example console, this will change metrics to Italy:

[HatCo.HatStore]
    orders-pending
        Country=Italy                                  2

Now we have updated Italy and removed Spain.

Actual behavior

Run example and watch its metrics:
dotnet-counters monitor --name ConsoleApp HatCo.HatStore

After process started metrics is:

[HatCo.HatStore]
    orders-pending
        Country=Italy                                  1

Press enter in example console, this will change metrics from Italy to Spain:

[HatCo.HatStore]
    orders-pending
        Country=Italy                                  1
        Country=Spain                                  1

Here is added Spain, but removed Italy is remainded.

Press enter in example console, this will change metrics to Italy:

[HatCo.HatStore]
    orders-pending
        Country=Italy                                  2
        Country=Spain                                  1

Now we have update Italy, but remainded Spain.

Regression?

No response

Known Workarounds

Track metrics+tags in own Measurement collection. Return this collection to CreateObservableGauge.
In own collection track missing metrics during update (with respect to metric name, tags, and tags combinations) - set its value to zero.

Configuration

No response

Other information

No response

Activity

  1. ghost added
    untriaged
    on Jul 13, 2022
  2. Yustos commented on Jul 14, 2022

    @Yustos
    Author

    I see that area is area-System.Diagnostics.Metric, but i have no idea how to add label for issue.

  3. tommcdon commented on Jul 18, 2022

    @tommcdon
    Member
  4. added this to the 7.0.0 milestone on Jul 18, 2022
  5. ghost removed
    untriaged
    on Jul 18, 2022
  6. carlossanlop commented on Jul 18, 2022

    @carlossanlop
    Contributor

    @noahfalk @tarekgh should there be a @dotnet/area-system-diagnostics-metric group created? There isn't one yet.

  7. tarekgh commented on Jul 18, 2022

    @tarekgh
    Member

    It is ok to create such a group and add me and Noah to it. This will apply to System.Diagnostics.Activity too.

  8. noahfalk commented on Jul 22, 2022

    @noahfalk
    Member

    I reproduced the problem and it looks like an issue with the dotnet-counters console UI logic. I have PR open that hopefully resolves it.

  9. Yustos commented on Aug 1, 2022

    @Yustos
    Author

    Hi, @noahfalk. Thank you!
    But i'm not sure that problem in client tools (like dotnet-conters or somewhat other).
    Looks like there some cache in the monitored process. Some array with tags permutations (somewhere in https://github.com/open-telemetry/opentelemetry-dotnet/blob/8511e92f062f1a8dbef6e248addf3eeb3417c34b/src/OpenTelemetry/Metrics/AggregatorStore.cs - not sure).

    Proof. Add prometheus exporter to first example after Main:

    using MeterProvider meterProvider = Sdk.CreateMeterProviderBuilder()
    	.AddMeter("HatCo.HatStore")
    	.AddPrometheusExporter(opt =>
    	{
    		opt.StartHttpListener = true;
    		opt.HttpListenerPrefixes = new string[] { $"http://localhost:9184/" };
    	})
    	.Build();
    

    And use http://localhost:9184/metrics endpoint to observe metrics (instead of dotnet-counters). Behavior will be identical to dotnet-counters - stale metrics retains.

  10. noahfalk commented on Aug 1, 2022

    @noahfalk
    Member

    This sounds like a separate issue we should file with the OpenTelemetry team. Roughly the flow of metrics data looks like this:

         --------------- Meter APIs -------- --------------------------------
        |                                        |                          |
        V                                        V                          V
     MetricsEventSource              OpenTelemetry SDK                  Other 3rd party MeterListeners
        |                                        | 
        V                                        V                                            
    dotnet-counters                    OpenTelemetry Exporters             
    

    The runtime team maintains the Meter APIs the stuff in the left column, the OpenTelemetry team owns the OTel SDK. Do you want me to open an issue on your behalf or you can open it?

  11. Yustos commented on Aug 18, 2022

    @Yustos
    Author

    Hi @noahfalk, you are right. I did some debugging and saw that OpenTelemetry AggregatorStore class retains metrics.
    Their repository have multiple issues about this (like open-telemetry/opentelemetry-dotnet#3508).
    Thank you!

  12. noahfalk commented on Aug 19, 2022

    @noahfalk
    Member

    Thanks for letting us know and I'm glad you were able to track it down!

  13. ghost locked as resolved and limited conversation to collaborators on Sep 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions