Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Commit

Permalink
IList -> IEnumerable (few of them) (#84)
Browse files Browse the repository at this point in the history
* few IList replaced with IEnumerable

* parent links from IList to IEnumerable

* a few more of IList -> IEnumerable

* one more

* stats event with IEnumerable now

* smal utils method should not use IList

* another utility method
  • Loading branch information
SergeyKanzhelev authored Jan 2, 2019
1 parent 81e1cc1 commit f811a11
Show file tree
Hide file tree
Showing 28 changed files with 91 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/OpenCensus.Abstractions/Trace/Export/IHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public interface IHandler
/// Exports the list of spans to the backend.
/// </summary>
/// <param name="spanDataList">Collection of spans to export.</param>
void Export(IList<ISpanData> spanDataList);
void Export(IEnumerable<ISpanData> spanDataList);
}
}
2 changes: 1 addition & 1 deletion src/OpenCensus.Abstractions/Trace/ISampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public interface ISampler
/// </param>
/// <param name="parentLinks">Links associated with the parent span.</param>
/// <returns>True of span needs to be created. False otherwise.</returns>
bool ShouldSample(ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IList<ISpan> parentLinks);
bool ShouldSample(ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IEnumerable<ISpan> parentLinks);
}
}
2 changes: 1 addition & 1 deletion src/OpenCensus.Abstractions/Trace/ISpanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface ISpanBuilder
/// </summary>
/// <param name="parentLinks">Parent links to set on span.</param>
/// <returns>This span builder for chaining.</returns>
ISpanBuilder SetParentLinks(IList<ISpan> parentLinks);
ISpanBuilder SetParentLinks(IEnumerable<ISpan> parentLinks);

/// <summary>
/// Set the record events value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public string Description
}
}

public bool ShouldSample(ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IList<ISpan> parentLinks)
public bool ShouldSample(ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IEnumerable<ISpan> parentLinks)
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public string Description
}
}

public bool ShouldSample(ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IList<ISpan> parentLinks)
public bool ShouldSample(ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IEnumerable<ISpan> parentLinks)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public string Description

public long IdUpperBound { get; }

public bool ShouldSample(ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IList<ISpan> parentLinks)
public bool ShouldSample(ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IEnumerable<ISpan> parentLinks)
{
// If the parent is sampled keep the sampling decision.
if (parentContext != null && parentContext.TraceOptions.IsSampled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public TraceExporterHandler(TelemetryConfiguration telemetryConfiguration)
this.telemetryClient = new TelemetryClient(telemetryConfiguration);
}

public void Export(IList<ISpanData> spanDataList)
public void Export(IEnumerable<ISpanData> spanDataList)
{
foreach (var span in spanDataList)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public TraceExporterHandler(string agentEndpoint, string hostName, string servic
this.Start();
}

public void Export(IList<ISpanData> spanDataList)
public void Export(IEnumerable<ISpanData> spanDataList)
{
if (this.cts == null || this.cts.IsCancellationRequested)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ static StackdriverTraceExporter()
}
}

public void Export(IList<ISpanData> spanDataList)
public void Export(IEnumerable<ISpanData> spanDataList)
{
TraceServiceClient traceWriter = TraceServiceClient.Create(settings: traceServiceSettings);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public TraceExporterHandler(ZipkinTraceExporterOptions options, HttpClient clien
this.httpClient = client ?? new HttpClient();
}

public void Export(IList<ISpanData> spanDataList)
public void Export(IEnumerable<ISpanData> spanDataList)
{
List<ZipkinSpan> zipkinSpans = new List<ZipkinSpan>();

Expand Down
2 changes: 1 addition & 1 deletion src/OpenCensus/Stats/MeasureMapBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal MeasureMapBuilder Put(IMeasureLong measure, long value)
return this;
}

internal IList<IMeasurement> Build()
internal IEnumerable<IMeasurement> Build()
{
// Note: this makes adding measurements quadratic but is fastest for the sizes of
// MeasureMapInternals that we should see. We may want to go to a strategy of sort/eliminate
Expand Down
4 changes: 2 additions & 2 deletions src/OpenCensus/Stats/MeasureToViewMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ internal void RegisterView(IView view, IClock clock)
}

// Records stats with a set of tags.
internal void Record(ITagContext tags, IList<IMeasurement> stats, ITimestamp timestamp)
internal void Record(ITagContext tags, IEnumerable<IMeasurement> stats, ITimestamp timestamp)
{
lock (this.lck)
{
Expand All @@ -117,7 +117,7 @@ internal void Record(ITagContext tags, IList<IMeasurement> stats, ITimestamp tim
continue;
}

IList<MutableViewData> views = this.mutableMap[measure.Name];
var views = this.mutableMap[measure.Name];
foreach (MutableViewData view in views)
{
measurement.Match<object>(
Expand Down
19 changes: 11 additions & 8 deletions src/OpenCensus/Stats/StatsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace OpenCensus.Stats
{
using System;
using System.Collections.Generic;
using System.Linq;
using OpenCensus.Tags;

public static class StatsExtensions
Expand Down Expand Up @@ -67,30 +68,32 @@ public static IAggregationData Sum(this IDictionary<TagValues, IAggregationData>
return MutableViewData.CreateAggregationData(sum, view.Measure);
}

private static bool TagValuesMatch(IList<ITagValue> aggValues, IList<ITagValue> values)
private static bool TagValuesMatch(IEnumerable<ITagValue> aggValues, IEnumerable<ITagValue> values)
{
if (values == null)
{
return true;
}

if (aggValues.Count != values.Count)
if (aggValues.Count() != values.Count())
{
return false;
}

for (int i = 0; i < aggValues.Count; i++)
{
var v1 = aggValues[i];
var v2 = values[i];
var first = aggValues.GetEnumerator();
var second = values.GetEnumerator();

while (first.MoveNext())
{
second.MoveNext();

// Null matches any aggValue
if (v2 == null)
if (second.Current == null)
{
continue;
}

if (!v2.Equals(v1))
if (first.Current != second.Current)
{
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/OpenCensus/Stats/StatsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal IViewData GetView(IViewName viewName)
return this.measureToViewMap.GetView(viewName, this.clock, this.state.Internal);
}

internal void Record(ITagContext tags, IList<IMeasurement> measurementValues)
internal void Record(ITagContext tags, IEnumerable<IMeasurement> measurementValues)
{
// TODO(songya): consider exposing No-op MeasureMap and use it when stats state is DISABLED, so
// that we don't need to create actual MeasureMapImpl.
Expand All @@ -80,10 +80,10 @@ internal void ResumeStatsCollection()
private class StatsEvent : IEventQueueEntry
{
private readonly ITagContext tags;
private readonly IList<IMeasurement> stats;
private readonly IEnumerable<IMeasurement> stats;
private readonly StatsManager statsManager;

public StatsEvent(StatsManager statsManager, ITagContext tags, IList<IMeasurement> stats)
public StatsEvent(StatsManager statsManager, ITagContext tags, IEnumerable<IMeasurement> stats)
{
this.statsManager = statsManager;
this.tags = tags;
Expand Down
2 changes: 1 addition & 1 deletion src/OpenCensus/Trace/Export/LinkList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override string ToString()
+ "}";
}

/// <inheritdoc/>
/// <inheritdoc/>
public override bool Equals(object o)
{
if (o == this)
Expand Down
2 changes: 1 addition & 1 deletion src/OpenCensus/Trace/Export/SpanData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static ISpanData Create(
throw new ArgumentNullException(nameof(messageOrNetworkEvents));
}

IList<ITimedEvent<IMessageEvent>> messageEventsList = new List<ITimedEvent<IMessageEvent>>();
var messageEventsList = new List<ITimedEvent<IMessageEvent>>();
foreach (ITimedEvent<IMessageEvent> timedEvent in messageOrNetworkEvents.Events)
{
messageEventsList.Add(timedEvent);
Expand Down
2 changes: 1 addition & 1 deletion src/OpenCensus/Trace/Export/SpanExporterWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private void BuildList(ISpan item, List<ISpanData> toExport)
}
}

private void Export(IList<ISpanData> export)
private void Export(IEnumerable<ISpanData> export)
{
var handlers = this.serviceHandlers.Values;
foreach (var handler in handlers)
Expand Down
2 changes: 1 addition & 1 deletion src/OpenCensus/Trace/Export/TimedEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal TimedEvents(IList<ITimedEvent<T>> events, int droppedEventsCount)

public int DroppedEventsCount { get; }

public static ITimedEvents<T> Create(IList<ITimedEvent<T>> events, int droppedEventsCount)
public static ITimedEvents<T> Create(IEnumerable<ITimedEvent<T>> events, int droppedEventsCount)
{
if (events == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/OpenCensus/Trace/NoopSpanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override ISpanBuilder SetSampler(ISampler sampler)
return this;
}

public override ISpanBuilder SetParentLinks(IList<ISpan> parentLinks)
public override ISpanBuilder SetParentLinks(IEnumerable<ISpan> parentLinks)
{
return this;
}
Expand Down
5 changes: 2 additions & 3 deletions src/OpenCensus/Trace/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,16 +466,15 @@ internal static ISpan StartSpan(
return span;
}

// public abstract void AddLink(LinkBase link);
private static ITimedEvents<T> CreateTimedEvents<T>(TraceEvents<EventWithNanoTime<T>> events, ITimestampConverter timestampConverter)
{
if (events == null)
{
IList<ITimedEvent<T>> empty = new List<ITimedEvent<T>>();
IEnumerable<ITimedEvent<T>> empty = new ITimedEvent<T>[0];
return TimedEvents<T>.Create(empty, 0);
}

IList<ITimedEvent<T>> eventsList = new List<ITimedEvent<T>>(events.Events.Count);
var eventsList = new List<ITimedEvent<T>>(events.Events.Count);
foreach (EventWithNanoTime<T> networkEvent in events.Events)
{
eventsList.Add(networkEvent.ToSpanDataTimedEvent(timestampConverter));
Expand Down
15 changes: 8 additions & 7 deletions src/OpenCensus/Trace/SpanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace OpenCensus.Trace
{
using System;
using System.Collections.Generic;
using System.Linq;
using OpenCensus.Internal;
using OpenCensus.Trace.Config;
using OpenCensus.Trace.Internal;
Expand Down Expand Up @@ -46,7 +47,7 @@ private SpanBuilder(string name, SpanBuilderOptions options, ISpanContext remote

private ISampler Sampler { get; set; }

private IList<ISpan> ParentLinks { get; set; } = new List<ISpan>();
private IEnumerable<ISpan> ParentLinks { get; set; } = new List<ISpan>();

private bool RecordEvents { get; set; }

Expand Down Expand Up @@ -94,7 +95,7 @@ public override ISpanBuilder SetSampler(ISampler sampler)
return this;
}

public override ISpanBuilder SetParentLinks(IList<ISpan> parentLinks)
public override ISpanBuilder SetParentLinks(IEnumerable<ISpan> parentLinks)
{
this.ParentLinks = parentLinks ?? throw new ArgumentNullException(nameof(parentLinks));
return this;
Expand All @@ -116,7 +117,7 @@ internal static ISpanBuilder CreateWithRemoteParent(string spanName, ISpanContex
return new SpanBuilder(spanName, options, remoteParentSpanContext, null);
}

private static bool IsAnyParentLinkSampled(IList<ISpan> parentLinks)
private static bool IsAnyParentLinkSampled(IEnumerable<ISpan> parentLinks)
{
foreach (ISpan parentLink in parentLinks)
{
Expand All @@ -129,9 +130,9 @@ private static bool IsAnyParentLinkSampled(IList<ISpan> parentLinks)
return false;
}

private static void LinkSpans(ISpan span, IList<ISpan> parentLinks)
private static void LinkSpans(ISpan span, IEnumerable<ISpan> parentLinks)
{
if (parentLinks.Count > 0)
if (parentLinks.Any())
{
ILink childLink = Link.FromSpanContext(span.Context, LinkType.ChildLinkedSpan);
foreach (ISpan linkedSpan in parentLinks)
Expand All @@ -147,7 +148,7 @@ private static bool MakeSamplingDecision(
bool hasRemoteParent,
string name,
ISampler sampler,
IList<ISpan> parentLinks,
IEnumerable<ISpan> parentLinks,
ITraceId traceId,
ISpanId spanId,
ITraceParams activeTraceParams)
Expand Down Expand Up @@ -176,7 +177,7 @@ private ISpan StartSpanInternal(
bool hasRemoteParent,
string name,
ISampler sampler,
IList<ISpan> parentLinks,
IEnumerable<ISpan> parentLinks,
bool recordEvents,
ITimestampConverter timestampConverter)
{
Expand Down
13 changes: 1 addition & 12 deletions src/OpenCensus/Trace/SpanBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public abstract class SpanBuilderBase : ISpanBuilder
{
public abstract ISpanBuilder SetSampler(ISampler sampler);

public abstract ISpanBuilder SetParentLinks(IList<ISpan> parentLinks);
public abstract ISpanBuilder SetParentLinks(IEnumerable<ISpan> parentLinks);

public abstract ISpanBuilder SetRecordEvents(bool recordEvents);

Expand All @@ -33,16 +33,5 @@ public IScope StartScopedSpan()
{
return CurrentSpanUtils.WithSpan(this.StartSpan(), true);
}

// public void StartSpanAndRun(Runnable runnable)
// {
// Span span = startSpan();
// CurrentSpanUtils.withSpan(span, /* endSpan= */ true, runnable).run();
// }
// public final<V> V startSpanAndCall(Callable<V> callable) throws Exception
// {
// final Span span = startSpan();
// return CurrentSpanUtils.withSpan(span, /* endSpan= */ true, callable).call();
// }
}
}
6 changes: 3 additions & 3 deletions src/OpenCensus/Utils/Collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static string ToString<TKey, TValue>(IDictionary<TKey, TValue> dict)
return sb.ToString();
}

public static string ToString<T>(IList<T> list)
public static string ToString<T>(IEnumerable<T> list)
{
if (list == null)
{
Expand All @@ -61,11 +61,11 @@ public static string ToString<T>(IList<T> list)
return sb.ToString();
}

public static bool AreEquivalent<T>(IList<T> c1, IList<T> c2)
public static bool AreEquivalent<T>(IEnumerable<T> c1, IEnumerable<T> c2)
{
var c1Dist = c1.Distinct();
var c2Dist = c2.Distinct();
return c1.Count == c2.Count && c1Dist.Count() == c2Dist.Count() && c1Dist.Intersect(c2Dist).Count() == c1Dist.Count();
return c1.Count() == c2.Count() && c1Dist.Count() == c2Dist.Count() && c1Dist.Intersect(c2Dist).Count() == c1Dist.Count();
}
}
}
Loading

0 comments on commit f811a11

Please sign in to comment.