Skip to content

Commit

Permalink
Pull request #261: RUM-20521 Changing Bytes to long for WebRequestTracer
Browse files Browse the repository at this point in the history
Merge in OP/openkit-dotnet from feature/RUM-20521-openkit-java-.net-native-should-use-long-instead-of-int-for-bytessent-and to main

* commit '71aea275c9a2710ad185808709add7b7c34fee56':
  RUM-20521 Changing Bytes to long for WebRequestTracer

GitOrigin-RevId: ed3de4fb9845beb71d0660698c00e0f46c33940e
  • Loading branch information
TheHighriser authored and openkitdt committed Jul 17, 2024
1 parent a472f11 commit ea1b10d
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 22 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

## [Unreleased](https://github.com/Dynatrace/openkit-dotnet/compare/v3.2.0...HEAD)

### Added

- `IWebRequestTracer.SetBytesSent(long bytes)` to increase the size range
- `IWebRequestTracer.SetBytesReceived(long bytes)` to increase the size range

### Changed

- `HttpClientWebClient` for NET3.5 is now using User-Agent property directly
- Improved cancelation or disposal of childs when leaving an action
- Deprecated `IWebRequestTracer.SetBytesSent(int bytes)` due to datatype limitations
- Deprecated `IWebRequestTracer.SetBytesReceived(int bytes)` due to datatype limitations

## 3.2.0 [Release date: 2023-12-06]
[GitHub Releases](https://github.com/Dynatrace/openkit-dotnet/releases/tag/v3.2.0)
Expand Down
16 changes: 16 additions & 0 deletions src/Dynatrace.OpenKit/API/IWebRequestTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,30 @@ public interface IWebRequestTracer : IDisposable
/// </summary>
/// <param name="bytesSent">number of bytes sent by the web request</param>
/// <returns></returns>
[Obsolete("Use SetBytesSent(long) instead")]
IWebRequestTracer SetBytesSent(int bytesSent);

/// <summary>
/// Sets the amount of sent bytes of the web request
/// </summary>
/// <param name="bytesSent">number of bytes sent by the web request</param>
/// <returns></returns>
IWebRequestTracer SetBytesSent(long bytesSent);

/// <summary>
/// Sets the amount of received bytes of the web request
/// </summary>
/// <param name="bytesReceived">number of bytes received by the web request</param>
/// <returns></returns>
[Obsolete("Use SetBytesReceived(long) instead")]
IWebRequestTracer SetBytesReceived(int bytesReceived);

/// <summary>
/// Sets the amount of received bytes of the web request
/// </summary>
/// <param name="bytesReceived">number of bytes received by the web request</param>
/// <returns></returns>
IWebRequestTracer SetBytesReceived(long bytesReceived);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ internal interface IWebRequestTracerInternals : IWebRequestTracer, ICancelableOp
/// <summary>
/// The number of bytes sent
/// </summary>
int BytesSent { get; }
long BytesSent { get; }

/// <summary>
/// The number of received bytes
/// </summary>
int BytesReceived { get; }
long BytesReceived { get; }

/// <summary>
/// Indicates whether this <see cref="IWebRequestTracer"/> was <see cref="IWebRequestTracer.Stop(int)">stopped</see>
Expand Down
9 changes: 9 additions & 0 deletions src/Dynatrace.OpenKit/Core/Objects/NullWebRequestTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,21 @@ public IWebRequestTracer SetBytesReceived(int bytesReceived)
{
return this;
}
public IWebRequestTracer SetBytesReceived(long bytesReceived)
{
return this;
}

public IWebRequestTracer SetBytesSent(int bytesSent)
{
return this;
}

public IWebRequestTracer SetBytesSent(long bytesSent)
{
return this;
}

public IWebRequestTracer Start()
{
return this;
Expand Down
14 changes: 12 additions & 2 deletions src/Dynatrace.OpenKit/Core/Objects/WebRequestTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ internal static bool IsValidUrlScheme(string url)

public int ResponseCode { get; private set; } = -1;

public int BytesSent { get; private set; } = -1;
public long BytesSent { get; private set; } = -1;

public int BytesReceived { get; private set; } = -1;
public long BytesReceived { get; private set; } = -1;

bool IWebRequestTracerInternals.IsStopped => EndTime != -1;

Expand Down Expand Up @@ -215,6 +215,11 @@ private void DoStop(int responseCode, bool discardData)
}

public IWebRequestTracer SetBytesSent(int bytesSent)
{
return SetBytesSent((long) bytesSent);
}

public IWebRequestTracer SetBytesSent(long bytesSent)
{
lock (lockObject)
{
Expand All @@ -228,6 +233,11 @@ public IWebRequestTracer SetBytesSent(int bytesSent)
}

public IWebRequestTracer SetBytesReceived(int bytesReceived)
{
return SetBytesReceived((long) bytesReceived);
}

public IWebRequestTracer SetBytesReceived(long bytesReceived)
{
lock (lockObject)
{
Expand Down
18 changes: 18 additions & 0 deletions src/Dynatrace.OpenKit/Protocol/Beacon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,24 @@ private static void AddKeyValuePairIfNotNegative(StringBuilder builder, string k
}
}

/// <summary>
/// Serialization helper method for adding key/value pairs with int values.
///
/// <para>
/// The key value pair is only added if the given <paramref name="intValue"/> is not negative.
/// </para>
/// </summary>
/// <param name="builder"></param>
/// <param name="key"></param>
/// <param name="intValue"></param>
private static void AddKeyValuePairIfNotNegative(StringBuilder builder, string key, long longValue)
{
if (longValue >= 0)
{
AddKeyValuePair(builder, key, longValue);
}
}

// helper method for adding key/value pairs with double values
private static void AddKeyValuePair(StringBuilder builder, string key, double doubleValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ public void SetBytesSentReturnsSelf()
Assert.That(obtained, Is.SameAs(target));
}

[Test]
public void SetBytesSentLongReturnsSelf()
{
// given
var target = NullWebRequestTracer.Instance;

// when
var obtained = target.SetBytesSent(37L);

// then
Assert.That(obtained, Is.SameAs(target));
}

[Test]
public void SetBytesReceivedReturnsSelf()
{
Expand All @@ -59,6 +72,19 @@ public void SetBytesReceivedReturnsSelf()
Assert.That(obtained, Is.SameAs(target));
}

[Test]
public void SetBytesReceivedLongReturnsSelf()
{
// given
var target = NullWebRequestTracer.Instance;

// when
var obtained = target.SetBytesReceived(73L);

// then
Assert.That(obtained, Is.SameAs(target));
}

[Test]
public void StartReturnsSelf()
{
Expand Down
66 changes: 62 additions & 4 deletions tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void SetBytesSentSetsTheNumberOfSentBytes()
var obtained = target.SetBytesSent(1234);

Check warning on line 137 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / build

'IWebRequestTracer.SetBytesSent(int)' is obsolete: 'Use SetBytesSent(long) instead'

Check warning on line 137 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / build

'IWebRequestTracer.SetBytesSent(int)' is obsolete: 'Use SetBytesSent(long) instead'

Check warning on line 137 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / build

'IWebRequestTracer.SetBytesSent(int)' is obsolete: 'Use SetBytesSent(long) instead'

Check warning on line 137 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / coverage

'IWebRequestTracer.SetBytesSent(int)' is obsolete: 'Use SetBytesSent(long) instead'

Check warning on line 137 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / coverage

'IWebRequestTracer.SetBytesSent(int)' is obsolete: 'Use SetBytesSent(long) instead'

Check warning on line 137 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / coverage

'IWebRequestTracer.SetBytesSent(int)' is obsolete: 'Use SetBytesSent(long) instead'

// then
Assert.That(target.BytesSent, Is.EqualTo(1234));
Assert.That(target.BytesSent, Is.EqualTo(1234L));
Assert.That(obtained, Is.SameAs(target));
}

Expand All @@ -152,7 +152,36 @@ public void SetBytesSentDoesNotSetAnythingIfStoppedWithResponseCode()
var obtained = target.SetBytesSent(1234);

Check warning on line 152 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / build

'IWebRequestTracer.SetBytesSent(int)' is obsolete: 'Use SetBytesSent(long) instead'

Check warning on line 152 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / build

'IWebRequestTracer.SetBytesSent(int)' is obsolete: 'Use SetBytesSent(long) instead'

Check warning on line 152 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / build

'IWebRequestTracer.SetBytesSent(int)' is obsolete: 'Use SetBytesSent(long) instead'

Check warning on line 152 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / coverage

'IWebRequestTracer.SetBytesSent(int)' is obsolete: 'Use SetBytesSent(long) instead'

Check warning on line 152 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / coverage

'IWebRequestTracer.SetBytesSent(int)' is obsolete: 'Use SetBytesSent(long) instead'

Check warning on line 152 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / coverage

'IWebRequestTracer.SetBytesSent(int)' is obsolete: 'Use SetBytesSent(long) instead'

// then
Assert.That(target.BytesSent, Is.EqualTo(-1));
Assert.That(target.BytesSent, Is.EqualTo(-1L));
Assert.That(obtained, Is.SameAs(target));
}

[Test]
public void SetBytesSentLongSetsTheNumberOfSentBytes()
{
// given
var target = CreateWebRequestTracer().Build();

// when setting the sent bytes
var obtained = target.SetBytesSent(1234L);

// then
Assert.That(target.BytesSent, Is.EqualTo(1234L));
Assert.That(obtained, Is.SameAs(target));
}

[Test]
public void SetBytesSentLongDoesNotSetAnythingIfStoppedWithResponseCode()
{
// given
var target = CreateWebRequestTracer().Build();
target.Stop(200);

// when setting the sent bytes
var obtained = target.SetBytesSent(1234L);

// then
Assert.That(target.BytesSent, Is.EqualTo(-1L));
Assert.That(obtained, Is.SameAs(target));
}

Expand All @@ -166,7 +195,7 @@ public void SetBytesReceivedSetsTheNumberOfReceivedBytes()
var obtained = target.SetBytesReceived(4321);

Check warning on line 195 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / build

'IWebRequestTracer.SetBytesReceived(int)' is obsolete: 'Use SetBytesReceived(long) instead'

Check warning on line 195 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / build

'IWebRequestTracer.SetBytesReceived(int)' is obsolete: 'Use SetBytesReceived(long) instead'

Check warning on line 195 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / coverage

'IWebRequestTracer.SetBytesReceived(int)' is obsolete: 'Use SetBytesReceived(long) instead'

Check warning on line 195 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / coverage

'IWebRequestTracer.SetBytesReceived(int)' is obsolete: 'Use SetBytesReceived(long) instead'

// then
Assert.That(target.BytesReceived, Is.EqualTo(4321));
Assert.That(target.BytesReceived, Is.EqualTo(4321L));
Assert.That(obtained, Is.SameAs(target));
}

Expand All @@ -181,7 +210,36 @@ public void SetBytesReceivedDoesNotSetAnythingIfStoppedWithResponseCode()
var obtained = target.SetBytesReceived(4321);

Check warning on line 210 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / build

'IWebRequestTracer.SetBytesReceived(int)' is obsolete: 'Use SetBytesReceived(long) instead'

Check warning on line 210 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / build

'IWebRequestTracer.SetBytesReceived(int)' is obsolete: 'Use SetBytesReceived(long) instead'

Check warning on line 210 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / coverage

'IWebRequestTracer.SetBytesReceived(int)' is obsolete: 'Use SetBytesReceived(long) instead'

Check warning on line 210 in tests/Dynatrace.OpenKit.Tests/Core/Objects/WebRequestTracerTest.cs

View workflow job for this annotation

GitHub Actions / coverage

'IWebRequestTracer.SetBytesReceived(int)' is obsolete: 'Use SetBytesReceived(long) instead'

// then
Assert.That(target.BytesReceived, Is.EqualTo(-1));
Assert.That(target.BytesReceived, Is.EqualTo(-1L));
Assert.That(obtained, Is.SameAs(target));
}

[Test]
public void SetBytesReceivedLongSetsTheNumberOfReceivedBytes()
{
// given
var target = CreateWebRequestTracer().Build();

// when setting the received bytes
var obtained = target.SetBytesReceived(4321L);

// then
Assert.That(target.BytesReceived, Is.EqualTo(4321L));
Assert.That(obtained, Is.SameAs(target));
}

[Test]
public void SetBytesReceivedLongDoesNotSetAnythingIfStoppedWithResponseCode()
{
// given
var target = CreateWebRequestTracer().Build();
target.Stop(200);

// when setting the received bytes
var obtained = target.SetBytesReceived(4321L);

// then
Assert.That(target.BytesReceived, Is.EqualTo(-1L));
Assert.That(obtained, Is.SameAs(target));
}

Expand Down
28 changes: 14 additions & 14 deletions tests/Dynatrace.OpenKit.Tests/Protocol/BeaconTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3193,8 +3193,8 @@ public void AddWebRequest()
// given
var target = CreateBeacon().Build();

const int sentBytes = 1337;
const int receivedBytes = 1447;
const long sentBytes = 1337;
const long receivedBytes = 1447;
const int responseCode = 418;
var tracer = Substitute.For<IWebRequestTracerInternals>();
tracer.Url.Returns(Url);
Expand Down Expand Up @@ -3268,8 +3268,8 @@ public void CanAddSentBytesEqualToZeroToWebRequestTracer()
// given
var target = CreateBeacon().Build();

const int sentBytes = 0;
const int receivedBytes = 1447;
const long sentBytes = 0;
const long receivedBytes = 1447;
const int responseCode = 418;
var tracer = Substitute.For<IWebRequestTracerInternals>();
tracer.Url.Returns(Url);
Expand Down Expand Up @@ -3304,8 +3304,8 @@ public void CannnotAddSentBytesLessThanZeroToWebRequestTracer()
// given
var target = CreateBeacon().Build();

const int sentBytes = -1;
const int receivedBytes = 1447;
const long sentBytes = -1;
const long receivedBytes = 1447;
const int responseCode = 418;
var tracer = Substitute.For<IWebRequestTracerInternals>();
tracer.Url.Returns(Url);
Expand Down Expand Up @@ -3339,8 +3339,8 @@ public void CanAddReceivedBytesEqualToZeroToWebRequestTracer()
// given
var target = CreateBeacon().Build();

const int sentBytes = 1337;
const int receivedBytes = 0;
const long sentBytes = 1337;
const long receivedBytes = 0;
const int responseCode = 418;
var tracer = Substitute.For<IWebRequestTracerInternals>();
tracer.Url.Returns(Url);
Expand Down Expand Up @@ -3375,8 +3375,8 @@ public void CannnotAddReceivedBytesLessThanZeroToWebRequestTracer()
// given
var target = CreateBeacon().Build();

const int sentBytes = 1337;
const int receivedBytes = -1;
const long sentBytes = 1337;
const long receivedBytes = -1;
const int responseCode = 418;
var tracer = Substitute.For<IWebRequestTracerInternals>();
tracer.Url.Returns(Url);
Expand Down Expand Up @@ -3410,8 +3410,8 @@ public void CanAddResponseCodeEqualToZeroToWebRequestTracer()
// given
var target = CreateBeacon().Build();

const int sentBytes = 1337;
const int receivedBytes = 1447;
const long sentBytes = 1337;
const long receivedBytes = 1447;
const int responseCode = 0;
var tracer = Substitute.For<IWebRequestTracerInternals>();
tracer.Url.Returns(Url);
Expand Down Expand Up @@ -3446,8 +3446,8 @@ public void CannnotAddResponseCodeLessThanZeroToWebRequestTracer()
// given
var target = CreateBeacon().Build();

const int sentBytes = 1337;
const int receivedBytes = 1447;
const long sentBytes = 1337;
const long receivedBytes = 1447;
const int responseCode = -1;
var tracer = Substitute.For<IWebRequestTracerInternals>();
tracer.Url.Returns(Url);
Expand Down

0 comments on commit ea1b10d

Please sign in to comment.