Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append traces path to http endpoint #279

Merged
merged 18 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion smoke-tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ x-env-base: &env_base
HONEYCOMB_METRICS_DATASET: bogus_dataset
OTEL_METRIC_EXPORT_INTERVAL: 1000
OTEL_SERVICE_NAME: "aspnetcore-example"
DEBUG: "true"

x-app-base: &app_base
build:
Expand Down Expand Up @@ -35,7 +36,7 @@ services:
<<: *app_base
environment:
<<: *env_base
HONEYCOMB_API_ENDPOINT: http://collector:4318/v1/traces
HONEYCOMB_API_ENDPOINT: http://collector:4318
OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf
ports:
- "127.0.0.1:5001:5001"
Expand Down
97 changes: 14 additions & 83 deletions src/Honeycomb.OpenTelemetry/EnvironmentOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal class EnvironmentOptions
private const string ServiceVersionKey = "SERVICE_VERSION";
private const string EnableLocalVisualizationsKey = "HONEYCOMB_ENABLE_LOCAL_VISUALIZATIONS";
private const string DebugKey = "DEBUG";
private const string OtelExporterOtlpProtocolKey = "OTEL_EXPORTER_OTLP_PROTOCOL";
private const uint DefaultSampleRate = 1;
private const string DefaultApiEndpoint = "https://api.honeycomb.io:443";
private readonly IDictionary _environmentService;
Expand All @@ -30,93 +31,23 @@ internal EnvironmentOptions(IDictionary service)
}

internal string ApiKey => GetEnvironmentVariable(ApiKeyKey);
internal string TracesApiKey => GetEnvironmentVariable(TracesApiKeyKey, ApiKey);
internal string MetricsApiKey => GetEnvironmentVariable(MetricsApiKeyKey, ApiKey);
internal string TracesApiKey => GetEnvironmentVariable(TracesApiKeyKey);
internal string MetricsApiKey => GetEnvironmentVariable(MetricsApiKeyKey);
internal string Dataset => GetEnvironmentVariable(DatasetKey);
internal string TracesDataset => GetEnvironmentVariable(TracesDatasetKey, Dataset);
internal string TracesDataset => GetEnvironmentVariable(TracesDatasetKey);
internal string MetricsDataset => GetEnvironmentVariable(MetricsDatasetKey);
internal string ApiEndpoint => GetEnvironmentVariable(ApiEndpointKey, DefaultApiEndpoint);
internal string TracesEndpoint => GetEnvironmentVariable(TracesEndpointKey, ApiEndpoint);
internal string MetricsEndpoint => GetEnvironmentVariable(MetricsEndpointKey, ApiEndpoint);
internal string ApiEndpoint => GetEnvironmentVariable(ApiEndpointKey);
internal string TracesEndpoint => GetEnvironmentVariable(TracesEndpointKey);
internal string MetricsEndpoint => GetEnvironmentVariable(MetricsEndpointKey);
internal string ServiceName => GetEnvironmentVariable(ServiceNameKey);
internal string ServiceVersion => GetEnvironmentVariable(ServiceVersionKey);
internal bool EnableLocalVisualizations => bool.TryParse(GetEnvironmentVariable(EnableLocalVisualizationsKey), out var enableLocalVisualizations) ? enableLocalVisualizations : false;
internal bool Debug => bool.TryParse(GetEnvironmentVariable(DebugKey), out var debug) ? debug : false;
internal uint SampleRate => uint.TryParse(GetEnvironmentVariable(SampleRateKey), out var sampleRate) ? sampleRate : DefaultSampleRate;

internal void SetOptionsFromEnvironmentIfTheyExist(HoneycombOptions options)
{
if (!string.IsNullOrWhiteSpace(ApiKey))
{
options.ApiKey = ApiKey;
}

if (!string.IsNullOrWhiteSpace(TracesApiKey))
{
options.TracesApiKey = TracesApiKey;
}

if (!string.IsNullOrWhiteSpace(MetricsApiKey))
{
options.MetricsApiKey = MetricsApiKey;
}

if (!string.IsNullOrWhiteSpace(Dataset))
{
options.Dataset = Dataset;
}

if (!string.IsNullOrWhiteSpace(TracesDataset))
{
options.TracesDataset = TracesDataset;
}

if (!string.IsNullOrWhiteSpace(MetricsDataset))
{
options.MetricsDataset = MetricsDataset;
}

if (!string.IsNullOrWhiteSpace(ApiEndpoint))
{
options.Endpoint = ApiEndpoint;
}

if (!string.IsNullOrWhiteSpace(TracesEndpoint))
{
options.TracesEndpoint = TracesEndpoint;
}

if (!string.IsNullOrWhiteSpace(MetricsEndpoint))
{
options.MetricsEndpoint = MetricsEndpoint;
}

if (!string.IsNullOrWhiteSpace(ServiceName))
{
options.ServiceName = ServiceName;
}

if (!string.IsNullOrWhiteSpace(ServiceVersion))
{
options.ServiceVersion = ServiceVersion;
}

if (bool.TryParse(GetEnvironmentVariable(EnableLocalVisualizationsKey), out var enableLocalVisualizations))
{
options.EnableLocalVisualizations = enableLocalVisualizations;
}

if (bool.TryParse(GetEnvironmentVariable(DebugKey), out var debug))
{
options.Debug = debug;
}

if (uint.TryParse(GetEnvironmentVariable(SampleRateKey), out var sampleRate))
{
options.SampleRate = sampleRate;
}
}

internal string EnableLocalVisualizationsValue => GetEnvironmentVariable(EnableLocalVisualizationsKey);
internal bool EnableLocalVisualizations => bool.TryParse(EnableLocalVisualizationsValue, out var enableLocalVisualizations) ? enableLocalVisualizations : false;
internal string DebugValue => GetEnvironmentVariable(DebugKey);
internal bool Debug => bool.TryParse(DebugValue, out var debug) ? debug : false;
internal string SampleRateValue => GetEnvironmentVariable(SampleRateKey);
internal uint SampleRate => uint.TryParse(SampleRateValue, out var sampleRate) ? sampleRate : DefaultSampleRate;
internal string OtelExporterOtlpProtocol => GetEnvironmentVariable(OtelExporterOtlpProtocolKey);
private string GetEnvironmentVariable(string key, string defaultValue = "")
{
var value = _environmentService[key];
Expand Down
179 changes: 130 additions & 49 deletions src/Honeycomb.OpenTelemetry/HoneycombOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ namespace Honeycomb.OpenTelemetry
public class HoneycombOptions
{
private const string OtlpVersion = "0.16.0";
private const string OtelExporterOtlpProtcolHttp = "http/protobuf";
pkanal marked this conversation as resolved.
Show resolved Hide resolved
pkanal marked this conversation as resolved.
Show resolved Hide resolved
private const string OtelExporterOtlpProtcolGrpc = "grpc";
pkanal marked this conversation as resolved.
Show resolved Hide resolved
private const string OtelExporterHttpTracesPath = "/v1/traces";

private bool isHttp = false;

/// <summary>
/// Default service name if service name is not provided.
/// </summary>
internal static readonly string SDefaultServiceName = $"unknown_service:{System.Diagnostics.Process.GetCurrentProcess().ProcessName}";
private static readonly string SDefaultServiceVersion = "{unknown_service_version}";

private string _tracesApiKey;
private string _metricsApiKey;
private string _tracesDataset;
private string _tracesEndpoint;
private string _metricsEndpoint;
internal static readonly string SDefaultServiceVersion = "{unknown_service_version}";
pkanal marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Name of the Honeycomb section of IConfiguration
Expand All @@ -47,22 +46,6 @@ public class HoneycombOptions
/// </summary>
public string ApiKey { get; set; }

/// <summary>
/// Returns whether API key used to send trace telemetry is a legacy key.
/// </summary>
/// <remarks>
/// Legacy keys have 32 characters.
/// </remarks>
internal bool IsTracesLegacyKey() => IsClassicKey(TracesApiKey);

/// <summary>
/// Returns whether API key used to send metrics telemetry is a legacy key.
/// </summary>
/// <remarks>
/// Legacy keys have 32 characters.
/// </remarks>
internal bool IsMetricsLegacyKey() => IsClassicKey(MetricsApiKey);

/// <summary>
/// Returns whether the provided API key is a legacy key.
/// </summary>
Expand All @@ -79,20 +62,12 @@ public class HoneycombOptions
/// <summary>
/// API key used to send trace telemetry data to Honeycomb. Defaults to <see cref="ApiKey"/>.
/// </summary>
public string TracesApiKey
{
get { return _tracesApiKey ?? ApiKey; }
set { _tracesApiKey = value; }
}
public string TracesApiKey { get; set; }

/// <summary>
/// API key used to send metrics telemetry data to Honeycomb. Defaults to <see cref="ApiKey"/>.
/// </summary>
public string MetricsApiKey
{
get { return _metricsApiKey ?? ApiKey; }
set { _metricsApiKey = value; }
}
public string MetricsApiKey { get; set; }

/// <summary>
/// Honeycomb dataset to store telemetry data.
Expand All @@ -103,11 +78,7 @@ public string MetricsApiKey
/// <summary>
/// Honeycomb dataset to store trace telemetry data. Defaults to <see cref="Dataset"/>.
/// </summary>
public string TracesDataset
{
get { return _tracesDataset ?? Dataset; }
set { _tracesDataset = value; }
}
public string TracesDataset { get; set; }

/// <summary>
/// Honeycomb dataset to store metrics telemetry data. Defaults to "null".
Expand All @@ -121,24 +92,15 @@ public string TracesDataset
/// </summary>
public string Endpoint { get; set; } = DefaultEndpoint;


/// <summary>
/// API endpoint to send telemetry data. Defaults to <see cref="Endpoint"/>.
/// </summary>
public string TracesEndpoint
{
get { return _tracesEndpoint ?? Endpoint; }
set { _tracesEndpoint = value; }
}
public string TracesEndpoint { get; set; }

/// <summary>
/// API endpoint to send telemetry data. Defaults to <see cref="Endpoint"/>.
/// </summary>
public string MetricsEndpoint
{
get { return _metricsEndpoint ?? Endpoint; }
set { _metricsEndpoint = value; }
}
public string MetricsEndpoint { get; set; }

/// <summary>
/// Sample rate for sending telemetry data. Defaults to <see cref="DefaultSampleRate"/>.
Expand Down Expand Up @@ -185,6 +147,125 @@ public string MetricsEndpoint
/// </summary>
public bool Debug { get; set; } = false;

/// <summary>
/// Applies environment variable option overrides.
/// </summary>
internal void ApplyEnvironmentOptions(EnvironmentOptions environmentOptions)
{
if (!string.IsNullOrWhiteSpace(environmentOptions.ApiKey))
{
ApiKey = environmentOptions.ApiKey;
}

if (!string.IsNullOrWhiteSpace(environmentOptions.TracesApiKey))
{
TracesApiKey = environmentOptions.TracesApiKey;
}

if (!string.IsNullOrWhiteSpace(environmentOptions.MetricsApiKey))
{
MetricsApiKey = environmentOptions.MetricsApiKey;
}

if (!string.IsNullOrWhiteSpace(environmentOptions.ApiEndpoint))
{
Endpoint = environmentOptions.ApiEndpoint;
}

if (!string.IsNullOrWhiteSpace(environmentOptions.Dataset))
{
Dataset = environmentOptions.Dataset;
}

if (!string.IsNullOrWhiteSpace(environmentOptions.TracesDataset))
{
TracesDataset = environmentOptions.TracesDataset;
}

if (!string.IsNullOrWhiteSpace(environmentOptions.MetricsDataset))
{
MetricsDataset = environmentOptions.MetricsDataset;
}

if (!string.IsNullOrWhiteSpace(environmentOptions.TracesEndpoint))
{
TracesEndpoint = environmentOptions.TracesEndpoint;
}

if (!string.IsNullOrWhiteSpace(environmentOptions.MetricsEndpoint))
{
MetricsEndpoint = environmentOptions.MetricsEndpoint;
}

if (!string.IsNullOrWhiteSpace(environmentOptions.ServiceName))
{
ServiceName = environmentOptions.ServiceName;
}

if (!string.IsNullOrWhiteSpace(environmentOptions.ServiceVersion))
{
ServiceVersion = environmentOptions.ServiceVersion;
}

if (!string.IsNullOrWhiteSpace(environmentOptions.EnableLocalVisualizationsValue))
{
EnableLocalVisualizations = environmentOptions.EnableLocalVisualizations;
}

if (!string.IsNullOrWhiteSpace(environmentOptions.DebugValue))
{
Debug = environmentOptions.Debug;
}

if (!string.IsNullOrWhiteSpace(environmentOptions.SampleRateValue))
{
SampleRate = environmentOptions.SampleRate;
}

if (!string.IsNullOrWhiteSpace(environmentOptions.OtelExporterOtlpProtocol))
{
isHttp = true;
}
pkanal marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Computes the final traces endpoint.
/// </summary>
internal string GetTracesEndpoint()
{
var endpoint = new UriBuilder(Endpoint);
if (isHttp)
{
endpoint.Path = OtelExporterHttpTracesPath;
}
return TracesEndpoint ?? endpoint.ToString();
pkanal marked this conversation as resolved.
Show resolved Hide resolved
}

internal string GetTracesApiKey()
{
return TracesApiKey ?? ApiKey;
}

internal string GetTracesDataset()
{
return TracesDataset ?? Dataset;
}

/// <summary>
/// Gets the <see cref="MetricsEndpoint" /> or falls back to the generic <see cref="Endpoint" />.
/// </summary>
internal string GetMetricsEndpoint()
{
return new UriBuilder(MetricsEndpoint ?? Endpoint).ToString();
}

/// <summary>
/// Gets the <see cref="MetricsApiKey" /> or falls back to the generic <see cref="ApiKey" />.
/// </summary>
internal string GetMetricsApiKey()
{
return MetricsApiKey ?? ApiKey;
}
pkanal marked this conversation as resolved.
Show resolved Hide resolved
internal string GetTraceHeaders() => GetTraceHeaders(TracesApiKey, TracesDataset);

internal static string GetTraceHeaders(string apikey, string dataset)
Expand Down
Loading