Skip to content

Support Metrics UpDownCounter instrument #63648

Closed

Description

@tarekgh

Background and motivation

In .NET 6.0 we have exposed the Metric APIs with exposing different instrument types Counter, Histogram, ObservableCounter, and ObservableGauge. We didn't expose the remaining instruments UpDownCounter and ObservableUpDownCounter. We were collecting feedback to see the demand and the scenarios needed for such instruments before exposing it. Also, we were waiting to have OpenTelemetry specs marked as stable too.

Now we have the feedback, and we are seeing the demand for such instruments and the scenarios need such instruments. The proposal here is to expose these instruments.

OpenTelemetry specification
open-telemetry/opentelemetry-dotnet#2362

APIs Proposal

UpDownCounter and ObservableUpDownCounter support only the following generic parameter types: Byte, Int16, Int32, Int64, Single, Double, and Decimal.

namespace System.Diagnostics.Metrics
{
    public sealed class UpDownCounter<T> : Instrument<T> where T : struct
    {
        public void Add(T delta) {  throw null; }
        public void Add(T delta, KeyValuePair<string, object?> tag)  {  throw null; }
        public void Add(T delta, KeyValuePair<string, object?> tag1, KeyValuePair<string, object?> tag2)  {  throw null; }
        public void Add(T delta, KeyValuePair<string, object?> tag1, KeyValuePair<string, object?> tag2, KeyValuePair<string, object?> tag3)  {  throw null; }
        public void Add(T delta, ReadOnlySpan<KeyValuePair<string, object?>> tags) {  throw null; }
        public void Add(T delta, params KeyValuePair<string, object?>[] tags) {  throw null; }
        public void Add(T delta, in TagList tagList) { throw null; }
    }

    public sealed class ObservableUpDownCounter<T> : ObservableInstrument<T> where T : struct
    {
        protected override IEnumerable<Measurement<T>> Observe() { throw null;}
    }

    public class Meter : IDisposable
    {
        public UpDownCounter<T> CreateUpDownCounter<T>(string name, string? unit = null, string? description = null) where T : struct  { throw null; }
        
        public ObservableUpDownCounter<T> CreateObservableUpDownCounter<T>(
                            string name,
                            Func<T> observeValue,
                            string? unit = null,
                            string? description = null) where T : struct { throw null; }
        public ObservableUpDownCounter<T> CreateObservableUpDownCounter<T>(
                            string name,
                            Func<Measurement<T>> observeValue,
                            string? unit = null,
                            string? description = null) where T : struct { throw null; }
        public ObservableUpDownCounter<T> CreateObservableUpDownCounter<T>(
                            string name,
                            Func<IEnumerable<Measurement<T>>> observeValues,
                            string? unit = null,
                            string? description = null) where T : struct { throw null; }        
    }
}

APIs Usage

static Meter s_meter = new Meter("MyLibrary.Queues", "1.0.0");

static UpDownCounter<int> s_queueSize = s_meter.CreateUpDownCounter<int>("Queue-Size");

static ObservableUpDownCounter<int> s_pullQueueSize = s_meter.CreateObservableUpDownCounter<int>("Queue-Size", () => s_pullQueueSize);

...

s_queueSize.Add(10);
s_queueSize.Add(-2);

Activity

  1. ghost added
    untriaged
    on Jan 11, 2022
  2. added and removed
    untriaged
    on Jan 11, 2022
  3. added this to the 7.0.0 milestone on Jan 11, 2022
  4. added
    api-needs-work
    and removed on Jan 19, 2022
  5. self-assigned this
    on Jan 19, 2022
  6. added
    api-ready-for-review
    and removed
    api-needs-work
    on Jan 24, 2022
  7. terrajobst commented on Jan 25, 2022

    @terrajobst
    Contributor

    Video

    • We considered using generic math to constrain the T, to, for example, INumber but the list of types are taken from the OpenTelemetry spec
    • UpDownCounter and ObservableUpDownCounter support only the following generic parameter types: Byte, Int16, Int32, Int64, Single, Double, and Decimal.

      • Should we consider supporting unsigned types?
      • Should we drop byte?
      • We should strive for consistency with what we shipped before for Counter (if we support byte there, we should do it here too)
      • We can consider adding support for unsigned types later too
    namespace System.Diagnostics.Metrics
    {
        public sealed class UpDownCounter<T> : Instrument<T> where T : struct
        {
            public void Add(T delta);
            public void Add(T delta, KeyValuePair<string, object?> tag);
            public void Add(T delta, KeyValuePair<string, object?> tag1, KeyValuePair<string, object?> tag2);
            public void Add(T delta, KeyValuePair<string, object?> tag1, KeyValuePair<string, object?> tag2, KeyValuePair<string, object?> tag3);
            public void Add(T delta, ReadOnlySpan<KeyValuePair<string, object?>> tags);
            public void Add(T delta, params KeyValuePair<string, object?>[] tags);
            public void Add(T delta, in TagList tagList);
        }
        public sealed class ObservableUpDownCounter<T> : ObservableInstrument<T> where T : struct
        {
            protected override IEnumerable<Measurement<T>> Observe();
        }
        public partial class Meter : IDisposable
        {
            public UpDownCounter<T> CreateUpDownCounter<T>(string name, string? unit = null, string? description = null) where T : struct ;
            public ObservableUpDownCounter<T> CreateObservableUpDownCounter<T>(
                                string name,
                                Func<T> observeValue,
                                string? unit = null,
                                string? description = null) where T : struct;
            public ObservableUpDownCounter<T> CreateObservableUpDownCounter<T>(
                                string name,
                                Func<Measurement<T>> observeValue,
                                string? unit = null,
                                string? description = null) where T : struct;
            public ObservableUpDownCounter<T> CreateObservableUpDownCounter<T>(
                                string name,
                                Func<IEnumerable<Measurement<T>>> observeValues,
                                string? unit = null,
                                string? description = null) where T : struct;
        }
    }
  8. added
    api-approved
    and removed
    api-ready-for-review
    on Jan 25, 2022
  9. ghost added
    in-pr
    on Jan 27, 2022
  10. ghost removed
    in-pr
    on Jan 28, 2022
  11. ghost locked as resolved and limited conversation to collaborators on Feb 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Diagnostics.Metric

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions