Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
sourabh1007 committed Sep 26, 2024
1 parent f4692fa commit c16a97a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,62 +28,73 @@ public static void Initialize()
{
cosmosMeter ??= new Meter(OpenTelemetryMetricsConstant.OperationMetricName, OpenTelemetryMetricsConstant.MetricVersion100);

NumberOfOperationCallCounter = cosmosMeter.CreateCounter<int>(
CosmosOperationMeter.NumberOfOperationCallCounter = cosmosMeter.CreateCounter<int>(
name: OpenTelemetryMetricsConstant.OperationMetrics.NumberOfCallsName,
unit: OpenTelemetryMetricsConstant.OperationMetrics.Count,
description: OpenTelemetryMetricsConstant.OperationMetrics.NumberOfCallsDesc);

RequestLatencyHistogram = cosmosMeter.CreateHistogram<double>(name: OpenTelemetryMetricsConstant.OperationMetrics.LatencyName,
unit: OpenTelemetryMetricsConstant.OperationMetrics.Count,
CosmosOperationMeter.RequestLatencyHistogram = cosmosMeter.CreateHistogram<double>(name: OpenTelemetryMetricsConstant.OperationMetrics.LatencyName,
unit: OpenTelemetryMetricsConstant.OperationMetrics.Ms,
description: OpenTelemetryMetricsConstant.OperationMetrics.LatencyDesc);

RequestUnitsHistogram = cosmosMeter.CreateHistogram<double>(name: OpenTelemetryMetricsConstant.OperationMetrics.RUName,
CosmosOperationMeter.RequestUnitsHistogram = cosmosMeter.CreateHistogram<double>(name: OpenTelemetryMetricsConstant.OperationMetrics.RUName,
unit: OpenTelemetryMetricsConstant.OperationMetrics.RUUnit,
description: OpenTelemetryMetricsConstant.OperationMetrics.RUDesc);

MaxItemGauge = cosmosMeter.CreateObservableGauge<int>(name: "cosmos.client.op.maxItemCount", observeValues: () => CosmosOperationMeter.PullMaxItemCount(), unit: "#", description: "For feed operations (query, readAll, readMany, change feed) and batch operations this meter capture the requested maxItemCount per page/request");
ActualItemGauge = cosmosMeter.CreateObservableGauge<int>(name: "cosmos.client.op.actualItemCount", observeValues: () => CosmosOperationMeter.PullActualItemCount(), unit: "#", description: "For feed operations (query, readAll, readMany, change feed) batch operations this meter capture the actual item count in responses from the service");
RegionsContactedGauge = cosmosMeter.CreateObservableGauge<int>(name: "cosmos.client.op.regionsContacted", observeValues: () => CosmosOperationMeter.PullRegionContactedCount(), unit: "# regions", description: "Number of regions contacted when executing an operation");
CosmosOperationMeter.MaxItemGauge = cosmosMeter.CreateObservableGauge<int>(name: OpenTelemetryMetricsConstant.OperationMetrics.MaxItemName,
observeValues: () => CosmosOperationMeter.PullMaxItemCount(),
unit: OpenTelemetryMetricsConstant.OperationMetrics.Count,
description: OpenTelemetryMetricsConstant.OperationMetrics.MaxItemDesc );

CosmosOperationMeter.ActualItemGauge = cosmosMeter.CreateObservableGauge<int>(name: OpenTelemetryMetricsConstant.OperationMetrics.ActualItemName,
observeValues: () => CosmosOperationMeter.PullActualItemCount(),
unit: OpenTelemetryMetricsConstant.OperationMetrics.Count,
description: OpenTelemetryMetricsConstant.OperationMetrics.ActualItemDesc);

CosmosOperationMeter.RegionsContactedGauge = cosmosMeter.CreateObservableGauge<int>(name: OpenTelemetryMetricsConstant.OperationMetrics.RegionContactedName,
observeValues: () => CosmosOperationMeter.PullRegionContactedCount(),
unit: OpenTelemetryMetricsConstant.OperationMetrics.Count,
description: OpenTelemetryMetricsConstant.OperationMetrics.RegionContactedDesc);
}

public static void RecordMaxItemCount(int maxItemCount, Func<KeyValuePair<string, object>[]> dimensionsFunc)
{
if (!MaxItemGauge.Enabled)
if (!CosmosOperationMeter.MaxItemGauge.Enabled)
{
return;
}

maxItemCounts ??= new ConcurrentBag<Tuple<int, KeyValuePair<string, object>[]>>();
maxItemCounts.Add(new Tuple<int, KeyValuePair<string, object>[]>(maxItemCount, dimensionsFunc()));
CosmosOperationMeter.maxItemCounts ??= new ConcurrentBag<Tuple<int, KeyValuePair<string, object>[]>>();
CosmosOperationMeter.maxItemCounts.Add(new Tuple<int, KeyValuePair<string, object>[]>(maxItemCount, dimensionsFunc()));
}

public static void RecordActualItemCount(int actualItemCount, Func<KeyValuePair<string, object>[]> dimensionsFunc)
{
if (!ActualItemGauge.Enabled)
if (!CosmosOperationMeter.ActualItemGauge.Enabled)
{
return;
}

actualItemCounts ??= new ConcurrentBag<Tuple<int, KeyValuePair<string, object>[]>>();
actualItemCounts.Add(new Tuple<int, KeyValuePair<string, object>[]>(actualItemCount, dimensionsFunc()));
CosmosOperationMeter.actualItemCounts ??= new ConcurrentBag<Tuple<int, KeyValuePair<string, object>[]>>();
CosmosOperationMeter.actualItemCounts.Add(new Tuple<int, KeyValuePair<string, object>[]>(actualItemCount, dimensionsFunc()));
}

public static void RecordRegionContactedCount(int regionsContactedCount, Func<KeyValuePair<string, object>[]> dimensionsFunc)
{
if (!RegionsContactedGauge.Enabled)
if (!CosmosOperationMeter.RegionsContactedGauge.Enabled)
{
return;
}

regionsContactedCounts ??= new ConcurrentBag<Tuple<int, KeyValuePair<string, object>[]>>();
regionsContactedCounts.Add(new Tuple<int, KeyValuePair<string, object>[]>(regionsContactedCount, dimensionsFunc()));
CosmosOperationMeter.regionsContactedCounts ??= new ConcurrentBag<Tuple<int, KeyValuePair<string, object>[]>>();
CosmosOperationMeter.regionsContactedCounts.Add(new Tuple<int, KeyValuePair<string, object>[]>(regionsContactedCount, dimensionsFunc()));
}

public static IEnumerable<Measurement<int>> PullMaxItemCount()
{
while (maxItemCounts.Count > 0)
while (CosmosOperationMeter.maxItemCounts.Count > 0)
{
if (maxItemCounts.TryTake(out Tuple<int, KeyValuePair<string, object>[]> maxItemCount))
if (CosmosOperationMeter.maxItemCounts.TryTake(out Tuple<int, KeyValuePair<string, object>[]> maxItemCount))
{
yield return new Measurement<int>(maxItemCount.Item1, maxItemCount.Item2);
}
Expand All @@ -92,9 +103,9 @@ public static IEnumerable<Measurement<int>> PullMaxItemCount()

public static IEnumerable<Measurement<int>> PullActualItemCount()
{
while (actualItemCounts.Count > 0)
while (CosmosOperationMeter.actualItemCounts.Count > 0)
{
if (actualItemCounts.TryTake(out Tuple<int, KeyValuePair<string, object>[]> actualItemCount))
if (CosmosOperationMeter.actualItemCounts.TryTake(out Tuple<int, KeyValuePair<string, object>[]> actualItemCount))
{
yield return new Measurement<int>(actualItemCount.Item1, actualItemCount.Item2);
}
Expand All @@ -103,9 +114,9 @@ public static IEnumerable<Measurement<int>> PullActualItemCount()

public static IEnumerable<Measurement<int>> PullRegionContactedCount()
{
while (regionsContactedCounts.Count > 0)
while (CosmosOperationMeter.regionsContactedCounts.Count > 0)
{
if (regionsContactedCounts.TryTake(out Tuple<int, KeyValuePair<string, object>[]> regionsContactedCount))
if (CosmosOperationMeter.regionsContactedCounts.TryTake(out Tuple<int, KeyValuePair<string, object>[]> regionsContactedCount))
{
yield return new Measurement<int>(regionsContactedCount.Item1, regionsContactedCount.Item2);
}
Expand All @@ -114,32 +125,32 @@ public static IEnumerable<Measurement<int>> PullRegionContactedCount()

internal static void RecordRequestUnit(double requestCharge, Func<KeyValuePair<string, object>[]> dimensionsFunc)
{
if (!RequestUnitsHistogram.Enabled)
if (!CosmosOperationMeter.RequestUnitsHistogram.Enabled)
{
return;
}

RequestUnitsHistogram?.Record(requestCharge, dimensionsFunc());
CosmosOperationMeter.RequestUnitsHistogram?.Record(requestCharge, dimensionsFunc());
}

internal static void RecordRequestLatency(TimeSpan? requestLatency, Func<KeyValuePair<string, object>[]> dimensionsFunc)
{
if (!RequestLatencyHistogram.Enabled || !requestLatency.HasValue)
if (!CosmosOperationMeter.RequestLatencyHistogram.Enabled || !requestLatency.HasValue)
{
return;
}

RequestLatencyHistogram.Record(requestLatency.Value.Milliseconds, dimensionsFunc());
CosmosOperationMeter.RequestLatencyHistogram.Record(requestLatency.Value.Milliseconds, dimensionsFunc());
}

internal static void RecordOperationCallCount(Func<KeyValuePair<string, object>[]> dimensionsFunc)
{
if (!NumberOfOperationCallCounter.Enabled)
if (!CosmosOperationMeter.NumberOfOperationCallCounter.Enabled)
{
return;
}

NumberOfOperationCallCounter.Add(1, dimensionsFunc());
CosmosOperationMeter.NumberOfOperationCallCounter.Add(1, dimensionsFunc());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ public static class OperationMetrics
/// NumberOfCalls
/// </summary>
public const string NumberOfCallsName = "cosmos.client.op.calls";

/// <summary>
/// Count
/// </summary>
public const string Count = "#";

/// <summary>
/// NumberOfCallsDesc
/// </summary>
Expand All @@ -55,14 +49,55 @@ public static class OperationMetrics
public const string RUName = "cosmos.client.op.RUs";

/// <summary>
/// RUUnit
/// RUDesc
/// </summary>
public const string RUUnit = "# RU";
public const string RUDesc = "Total request units per operation (sum of RUs for all requested needed when processing an operation)";

/// <summary>
/// RUName
/// </summary>
public const string MaxItemName = "cosmos.client.op.maxItemCount";

/// <summary>
/// RUDesc
/// </summary>
public const string RUDesc = "Total request units per operation (sum of RUs for all requested needed when processing an operation)";
public const string MaxItemDesc = "For feed operations (query, readAll, readMany, change feed) and batch operations this meter capture the requested maxItemCount per page/request";

/// <summary>
/// ActualItemName
/// </summary>
public const string ActualItemName = "cosmos.client.op.actualItemCount";

/// <summary>
/// ActualItemDesc
/// </summary>
public const string ActualItemDesc = "For feed operations (query, readAll, readMany, change feed) batch operations this meter capture the actual item count in responses from the service";

/// <summary>
/// RegionContactedName
/// </summary>
public const string RegionContactedName = "cosmos.client.op.regionsContacted";

/// <summary>
/// RegionContactedDesc
/// </summary>
public const string RegionContactedDesc = "Number of regions contacted when executing an operation";

/// <summary>
/// Count
/// </summary>
public const string Count = "#";

/// <summary>
/// Milliseconds
/// </summary>
public const string Ms = "ms";

/// <summary>
/// RUUnit
/// </summary>
public const string RUUnit = "# RU";

}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public override ExportResult Export(in Batch<Metric> batch)
{
Console.WriteLine("\n[Custom Exporter] Exporting metrics:");

int gaugeCount = 0;

Dictionary<string, MetricType> actualMetrics = new Dictionary<string, MetricType>();
foreach (Metric metric in batch)
{
Expand All @@ -45,7 +43,6 @@ public override ExportResult Export(in Batch<Metric> batch)
break;
case MetricType.LongGauge:
Console.WriteLine($"Value (Gauge): {metricPoint.GetGaugeLastValueLong()}");
gaugeCount++;
break;
case MetricType.Histogram:
Console.WriteLine($"Value (Histogram): {metricPoint.GetHistogramCount()}");
Expand All @@ -61,9 +58,6 @@ public override ExportResult Export(in Batch<Metric> batch)

Assert.IsTrue(expectedMetrics.Count == actualMetrics.Count && !expectedMetrics.Except(actualMetrics).Any());

Console.WriteLine($"Total gauge values exported: {gaugeCount}");
Console.WriteLine();

return ExportResult.Success;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public async Task OperationLevelMetrics()
await feedIterator.ReadNextAsync();
}

if (sw.ElapsedMilliseconds > TimeSpan.FromMinutes(.5).TotalMilliseconds)
if (sw.ElapsedMilliseconds > TimeSpan.FromMinutes(1.5).TotalMilliseconds)
{
break;
}
Expand Down

0 comments on commit c16a97a

Please sign in to comment.