Skip to content

Commit

Permalink
Apply design feedback on Metrics and DistributedContext (#340)
Browse files Browse the repository at this point in the history
* Apply design feedback on Metrics and DistributedContext

- Reduce the number od abstract methods on Meter and allow overriding protected methods to avoid make it public.
- Rename the word “Long” in the APIs to “Int64”. This is the design guidelines recommendation.
- Add convenient properties NoPropagationEntry and UnlimitedPropagationEntry in EntryMetadata.
- Avoid using the 3 uppercase letters acronym of TTL and just use the full words TimeToLive.

* Address the feedback
  • Loading branch information
tarekgh authored and Liudmila Molkova committed Nov 20, 2019
1 parent cb5eebd commit d44cea3
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public sealed class DistributedContextEntry
/// <param name="key">Key name for the entry.</param>
/// <param name="value">Value associated with the key name.</param>
public DistributedContextEntry(string key, string value)
: this(key, value, new EntryMetadata(EntryMetadata.NoPropagation))
: this(key, value, EntryMetadata.NoPropagationEntry)
{
}

Expand Down
25 changes: 16 additions & 9 deletions src/OpenTelemetry.Api/DistributedContext/EntryMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,34 @@ namespace OpenTelemetry.Context
public readonly struct EntryMetadata
{
/// <summary>
/// TTL indicating in-process only propagation of an entry.
/// TimeToLive (TTL) indicating in-process only propagation of an entry.
/// </summary>
public const int NoPropagation = 0;

/// <summary>
/// TTL indicating unlimited propagation of an entry.
/// TimeToLive (TTL) indicating unlimited propagation of an entry.
/// </summary>
public const int UnlimitedPropagation = -1;

/// <summary>
/// Initializes a new instance of the <see cref="EntryMetadata"/> struct.
/// </summary>
/// <param name="entryTTL">TTL for the distributed context entry.</param>
public EntryMetadata(int entryTTL)
{
this.EntryTTL = EntryMetadata.NoPropagation;
}
/// <param name="timeToLive">TTL for the distributed context entry.</param>
private EntryMetadata(int timeToLive) => this.TimeToLive = timeToLive;

/// <summary>
/// Gets the EntryTTL is either NO_PROPAGATION (0) or UNLIMITED_PROPAGATION (-1).
/// Gets a new instance of the <see cref="EntryMetadata"/> struct with NoPropagation value.
/// </summary>
public int EntryTTL { get; }
public static EntryMetadata NoPropagationEntry => new EntryMetadata(NoPropagation);

/// <summary>
/// Gets a new instance of the <see cref="EntryMetadata"/> struct with UnlimitedPropagation value.
/// </summary>
public static EntryMetadata UnlimitedPropagationEntry => new EntryMetadata(UnlimitedPropagation);

/// <summary>
/// Gets the TimeToLive which is either NO_PROPAGATION (0) or UNLIMITED_PROPAGATION (-1).
/// </summary>
public int TimeToLive { get; }
}
}
66 changes: 48 additions & 18 deletions src/OpenTelemetry.Api/Metrics/Meter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,58 +24,88 @@ namespace OpenTelemetry.Metrics
public abstract class Meter
{
/// <summary>
/// Creates a counter for long with given name.
/// Creates Int64 counter with given name.
/// </summary>
/// <param name="name">The name of the counter.</param>
/// <param name="monotonic">indicates if only positive values are expected.</param>
/// <returns>The counter instance.</returns>
public abstract Counter<long> CreateLongCounter(string name, bool monotonic = true);
public Counter<long> CreateInt64Counter(string name, bool monotonic = true) => this.CreateCounter<long>(name, monotonic);

/// <summary>
/// Creates a counter for double with given name.
/// Creates double counter with given name.
/// </summary>
/// <param name="name">indicates if only positive values are expected.</param>
/// <param name="monotonic">The name of the counter.</param>
/// <returns>The counter instance.</returns>
public abstract Counter<double> CreateDoubleCounter(string name, bool monotonic = true);
public Counter<double> CreateDoubleCounter(string name, bool monotonic = true) => this.CreateCounter<double>(name, monotonic);

/// <summary>
/// Creates a Gauge for long with given name.
/// Creates Int64 Gauge with given name.
/// </summary>
/// <param name="name">The name of the counter.</param>
/// <param name="monotonic">indicates if only positive values are expected.</param>
/// <returns>The Gauge instance.</returns>
public abstract Gauge<long> CreateLongGauge(string name, bool monotonic = false);
public Gauge<long> CreateInt64Gauge(string name, bool monotonic = false) => this.CreateGauge<long>(name, monotonic);

/// <summary>
/// Creates a Gauge for long with given name.
/// </summary>
/// <param name="name">The name of the counter.</param>
/// <param name="monotonic">indicates if only positive values are expected.</param>
/// <returns>The Gauge instance.</returns>
public abstract Gauge<double> CreateDoubleGauge(string name, bool monotonic = false);

/// <summary>
/// Creates a measure for long with given name.
/// Creates Int64 Measure with given name.
/// </summary>
/// <param name="name">The name of the measure.</param>
/// <param name="absolute">indicates if only positive values are expected.</param>
/// <returns>The measure instance.</returns>
public abstract Measure<long> CreateLongMeasure(string name, bool absolute = true);
public Measure<long> CreateInt64Measure(string name, bool absolute = true) => this.CreateMeasure<long>(name, absolute);

/// <summary>
/// Creates a measure for long with given name.
/// Creates double Measure with given name.
/// </summary>
/// <param name="name">The name of the measure.</param>
/// <param name="absolute">indicates if only positive values are expected.</param>
/// <returns>The measure instance.</returns>
public abstract Measure<double> CreateDoubleMeasure(string name, bool absolute = true);
public Measure<double> CreateDoubleMeasure(string name, bool absolute = true) => this.CreateMeasure<double>(name, absolute);

/// <summary>
/// Creates double Gauge with given name.
/// </summary>
/// <param name="name">The name of the counter.</param>
/// <param name="monotonic">indicates if only positive values are expected.</param>
/// <returns>The Gauge instance.</returns>
public Gauge<double> CreateDoubleGauge(string name, bool monotonic = false) => this.CreateGauge<double>(name, monotonic);

/// <summary>
/// Constructs or retrieves the <see cref="LabelSet"/> from the given label key-value pairs.
/// </summary>
/// <param name="labels">Label key value pairs.</param>
/// <returns>The <see cref="LabelSet"/> with given label key value pairs.</returns>
public abstract LabelSet GetLabelSet(IEnumerable<KeyValuePair<string, string>> labels);

/// <summary>
/// Creates double or Int64 counter with given name.
/// </summary>
/// <param name="name">indicates if only positive values are expected.</param>
/// <param name="monotonic">The name of the counter.</param>
/// <typeparam name="T">The element type of the counter. Should be either long or double.</typeparam>
/// <returns>The counter instance.</returns>
protected abstract Counter<T> CreateCounter<T>(string name, bool monotonic = true)
where T : struct;

/// <summary>
/// Creates double or Int64 Gauge with given name.
/// </summary>
/// <param name="name">The name of the counter.</param>
/// <param name="monotonic">indicates if only positive values are expected.</param>
/// <typeparam name="T">The element type of the Gauge. Should be either long or double.</typeparam>
/// <returns>The Gauge instance.</returns>
protected abstract Gauge<T> CreateGauge<T>(string name, bool monotonic = false)
where T : struct;

/// <summary>
/// Creates double or Int64 Measure with given name.
/// </summary>
/// <param name="name">The name of the measure.</param>
/// <param name="absolute">indicates if only positive values are expected.</param>
/// <typeparam name="T">The element type of the Measure. Should be either long or double.</typeparam>
/// <returns>The measure instance.</returns>
protected abstract Measure<T> CreateMeasure<T>(string name, bool absolute = true)
where T : struct;
}
}
36 changes: 18 additions & 18 deletions src/OpenTelemetry.Api/Metrics/NoOpMeter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Collections.Generic;

namespace OpenTelemetry.Metrics
Expand All @@ -23,42 +24,41 @@ public NoOpMeter()
{
}

public override Counter<double> CreateDoubleCounter(string name, bool monotonic = true)
{
// return no op
throw new System.NotImplementedException();
}

public override Gauge<double> CreateDoubleGauge(string name, bool monotonic = false)
public override LabelSet GetLabelSet(IEnumerable<KeyValuePair<string, string>> labels)
{
// return no op
throw new System.NotImplementedException();
}

public override Measure<double> CreateDoubleMeasure(string name, bool absolute = true)
protected override Counter<T> CreateCounter<T>(string name, bool monotonic = true)
{
throw new System.NotImplementedException();
}
if (typeof(T) != typeof(long) || typeof(T) != typeof(double))
{
throw new InvalidOperationException();
}

public override Counter<long> CreateLongCounter(string name, bool monotonic = true)
{
// return no op
throw new System.NotImplementedException();
}

public override Gauge<long> CreateLongGauge(string name, bool monotonic = false)
protected override Gauge<T> CreateGauge<T>(string name, bool monotonic = true)
{
if (typeof(T) != typeof(long) || typeof(T) != typeof(double))
{
throw new InvalidOperationException();
}

// return no op
throw new System.NotImplementedException();
}

public override Measure<long> CreateLongMeasure(string name, bool absolute = true)
protected override Measure<T> CreateMeasure<T>(string name, bool monotonic = true)
{
throw new System.NotImplementedException();
}
if (typeof(T) != typeof(long) || typeof(T) != typeof(double))
{
throw new InvalidOperationException();
}

public override LabelSet GetLabelSet(IEnumerable<KeyValuePair<string, string>> labels)
{
// return no op
throw new System.NotImplementedException();
}
Expand Down

0 comments on commit d44cea3

Please sign in to comment.