Skip to content
This repository has been archived by the owner on Jul 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #45 from newrelic/DOTNET-4463-UpdateHeaderForExporter
Browse files Browse the repository at this point in the history
DOTNET-4463 Include the exporter version in the User-Agent header
  • Loading branch information
vuqtran88 authored Dec 11, 2019
2 parents 384457f + 5423ff4 commit 414cb8a
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 21 deletions.
28 changes: 28 additions & 0 deletions src/NewRelic.Telemetry.Tests/DataSenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;

namespace NewRelic.Telemetry.Tests
{
Expand Down Expand Up @@ -551,5 +552,32 @@ async public Task SendDataAsyncThrowsNonHttpException()
Assert.AreEqual(expectedNumSendBatchAsyncCall, actualCountCallsSendData, "Unexpected Number of SendDataAsync calls");
Assert.AreEqual(expectedNumHttpHandlerCall, actualCallsHttpHandler, "Unexpected Number of Http Handler calls");
}

[TestCase(null, "1.0.0")]
[TestCase("productName", null)]
[TestCase("", "")]
[TestCase(null, null)]
[TestCase("", null)]
[TestCase(null, "")]
[TestCase("productName", "1.0.0")]
public void AddVersionInfo(string productName, string productVersion)
{
var dataSender = new SpanDataSender(new TelemetryConfiguration().WithAPIKey("123456"));
var fieldInfo = dataSender.GetType().GetField("_userAgent", BindingFlags.NonPublic | BindingFlags.Instance);
var userAgentValueBefore = fieldInfo.GetValue(dataSender);

var expectedUserAgentValue = userAgentValueBefore?.ToString();

if(!string.IsNullOrEmpty(productName) && !string.IsNullOrEmpty(productVersion))
{
expectedUserAgentValue = userAgentValueBefore + " " + $@"{productName}/{productVersion}";
}

dataSender.AddVersionInfo(productName, productVersion);

var userAgentValueAfter = fieldInfo.GetValue(dataSender);

Assert.AreEqual(expectedUserAgentValue, userAgentValueAfter);
}
}
}
2 changes: 0 additions & 2 deletions src/NewRelic.Telemetry/PackageVersionAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NewRelic.Telemetry
{
Expand Down
56 changes: 37 additions & 19 deletions src/NewRelic.Telemetry/Transport/DataSender.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
Expand All @@ -14,8 +15,8 @@ namespace NewRelic.Telemetry.Transport
{
public abstract class DataSender<TData> where TData : ITelemetryDataType
{
private const string _userAgent = "NewRelic-Dotnet-TelemetrySDK";
private static readonly string _implementationVersion = Assembly.GetExecutingAssembly().GetCustomAttribute<PackageVersionAttribute>().PackageVersion;
protected static readonly string _telemetrySdkVersion = Assembly.GetExecutingAssembly().GetCustomAttribute<PackageVersionAttribute>().PackageVersion;
protected string _userAgent = "NewRelic-Dotnet-TelemetrySDK/" + _telemetrySdkVersion;

protected readonly TelemetryConfiguration _config;
protected readonly TelemetryLogging _logger;
Expand Down Expand Up @@ -154,22 +155,6 @@ private async Task<Response> RetryWithServerDelay(TData dataToSend, int retryNum
return await SendDataAsync(dataToSend, retryNum + 1);
}

/// <summary>
/// Method used to send a data to New Relic endpoint. Handles the communication with the New Relic endpoints.
/// </summary>
/// <param name="dataToSend">The data to send to New Relic</param>
/// <returns>New Relic response indicating the outcome and additional information about the interaction with the New Relic endpoint.</returns>
public async Task<Response> SendDataAsync(TData dataToSend)
{
if(string.IsNullOrWhiteSpace(_config.ApiKey))
{
_logger.Exception(new ArgumentNullException("Configuration requires API key"));
return Response.Failure("API Key was not available");
}

return await SendDataAsync(dataToSend, 0);
}

private async Task<Response> SendDataAsync(TData dataToSend, int retryNum)
{

Expand Down Expand Up @@ -240,7 +225,9 @@ private async Task<HttpResponseMessage> SendDataAsync(string serializedPayload)

var requestMessage = new HttpRequestMessage(HttpMethod.Post, EndpointUrl);
requestMessage.Content = streamContent;
requestMessage.Headers.Add("User-Agent", _userAgent + "/" + _implementationVersion);

requestMessage.Headers.Add("User-Agent", _userAgent);

requestMessage.Headers.Add("Api-Key", _config.ApiKey);
requestMessage.Method = HttpMethod.Post;

Expand All @@ -254,5 +241,36 @@ private async Task<HttpResponseMessage> SendDataAsync(string serializedPayload)
return response;
}
}

/// <summary>
/// Method used to send a data to New Relic endpoint. Handles the communication with the New Relic endpoints.
/// </summary>
/// <param name="dataToSend">The data to send to New Relic</param>
/// <returns>New Relic response indicating the outcome and additional information about the interaction with the New Relic endpoint.</returns>
public async Task<Response> SendDataAsync(TData dataToSend)
{
if (string.IsNullOrWhiteSpace(_config.ApiKey))
{
_logger.Exception(new ArgumentNullException("Configuration requires API key"));
return Response.Failure("API Key was not available");
}

return await SendDataAsync(dataToSend, 0);
}

/// <summary>
/// Method used to add product information including product name and version to the User-Agent HTTP header.
/// </summary>
/// <param name="productName">Name of the product uses the TelemetrySDK (e.g. "OpenTelemetry.Exporter.NewRelic"). This should not be null or empty.</param>
/// <param name="productVersion">Version of the product uses the TelemetrySDK (e.g. "1.0.0"). This should not be null or empty.</param>
/// <returns></returns>
public void AddVersionInfo(string productName, string productVersion)
{
if (!string.IsNullOrEmpty(productName) && !string.IsNullOrEmpty(productVersion))
{
var productIdentifier = string.Join("/", productName, productVersion);
_userAgent = string.Join(" ", _userAgent, productIdentifier);
}
}
}
}
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.NewRelic/NewRelicTraceExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using OpenTelemetry.Trace.Export;
using OpenTelemetry.Trace;
using System.Reflection;

namespace OpenTelemetry.Exporter.NewRelic
{
Expand All @@ -19,6 +20,8 @@ namespace OpenTelemetry.Exporter.NewRelic
public class NewRelicTraceExporter : SpanExporter
{
private readonly NRSpans.SpanDataSender _spanDataSender;
private const string _productName = "OpenTelemetry.Exporter.NewRelic";
private static readonly string _productVersion = Assembly.GetExecutingAssembly().GetCustomAttribute<PackageVersionAttribute>().PackageVersion;

private const string _attribName_url = "http.url";

Expand Down Expand Up @@ -65,6 +68,7 @@ public NewRelicTraceExporter(TelemetryConfiguration config, ILoggerFactory logge
internal NewRelicTraceExporter(NRSpans.SpanDataSender spanDataSender, TelemetryConfiguration config, ILoggerFactory loggerFactory)
{
_spanDataSender = spanDataSender;
spanDataSender.AddVersionInfo(_productName, _productVersion);

_config = config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@
<PackageTags>newrelic</PackageTags>
<Authors>New Relic</Authors>
<Company>New Relic</Company>

<PackageId>OpenTelemetry.Exporter.NewRelic</PackageId>
<PackageTags>newrelic</PackageTags>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>

<Major>1</Major>
<Minor>0</Minor>
<Build>0</Build>
<Revision>0</Revision>
<Version>$(Major).$(Minor).$(Build).$(Revision)</Version>
<PackageVersion>$(Major).$(Minor).$(Build)-beta</PackageVersion>
<AssemblyVersion>$(Major).0.0.0</AssemblyVersion>
<FileVersion>$(Major).$(Minor).$(Build).$(Revision)</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -30,6 +44,10 @@
<_Parameter1>OpenTelemetry.Exporter.NewRelic.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100198f2915b649f8774e7937c4e37e39918db1ad4e83109623c1895e386e964f6aa344aeb61d87ac9bd1f086a7be8a97d90f9ad9994532e5fb4038d9f867eb5ed02066ae24086cf8a82718564ebac61d757c9cbc0cc80f69cc4738f48f7fc2859adfdc15f5dde3e05de785f0ed6b6e020df738242656b02c5c596a11e628752bd0</_Parameter1>
</AssemblyAttribute>

<AssemblyAttribute Include="OpenTelemetry.Exporter.NewRelic.PackageVersion">
<_Parameter1>$(PackageVersion)</_Parameter1>
</AssemblyAttribute>

</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions src/OpenTelemetry.Exporter.NewRelic/PackageVersionAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace OpenTelemetry.Exporter.NewRelic
{
[AttributeUsage(AttributeTargets.Assembly)]
public class PackageVersionAttribute : Attribute
{
public string PackageVersion { get; }
public PackageVersionAttribute(string version)
{
PackageVersion = version;
}
}
}

0 comments on commit 414cb8a

Please sign in to comment.