Skip to content

Commit

Permalink
remove smoke tests and start HoneycombOptions refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pkanal committed Sep 27, 2022
1 parent 264ee69 commit bac26f1
Show file tree
Hide file tree
Showing 8 changed files with 477 additions and 138 deletions.
1 change: 0 additions & 1 deletion smoke-tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ services:
environment:
<<: *env_base
HONEYCOMB_API_ENDPOINT: http://collector:4318
HONEYCOMB_TRACES_ENDPOINT: ${HONEYCOMB_TRACES_ENDPOINT}
OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf
DEBUG: "true"
ports:
Expand Down
14 changes: 0 additions & 14 deletions smoke-tests/smoke-sdk-http.bats
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,3 @@ teardown_file() {
result=$(span_attributes_for ${OTEL_SERVICE_NAME} | jq "select(.key == \"delay_ms\").value.intValue")
assert_equal "$result" '"100"'
}

@test "Traces path is appended to endpoint" {
expected_endpoint="http://collector:4318/v1/traces"
result=$(docker-compose logs ${CONTAINER_NAME} | grep -o ${expected_endpoint} | head -1)
assert_equal "$result" "$expected_endpoint"
}

@test "Traces path is not appended to traces endpoint" {
docker-compose stop ${CONTAINER_NAME}
expected_endpoint="http://collector:4318/special-traces-endpoint"
HONEYCOMB_TRACES_ENDPOINT=$expected_endpoint docker-compose up --detach ${CONTAINER_NAME}
result=$(docker-compose logs ${CONTAINER_NAME} | grep -o ${expected_endpoint} | head -1)
assert_equal "$result" "$expected_endpoint"
}
9 changes: 1 addition & 8 deletions smoke-tests/test_helpers/utilities.bash
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,9 @@ wait_for_flush() {
wait_for_ready_app() {
CONTAINER=${1:?container name is a required parameter}
MAX_RETRIES=10
READY_APP_LOG="Now listening on:"
echo -n "# 🍿 Setting up ${CONTAINER}" >&3
NEXT_WAIT_TIME=0
until [ $NEXT_WAIT_TIME -eq $MAX_RETRIES ] || [[ $(search_logs_for ${CONTAINER} ${READY_APP_LOG}) ]]
until [ $NEXT_WAIT_TIME -eq $MAX_RETRIES ] || [[ $(docker-compose logs ${CONTAINER} | grep "Now listening on:") ]]
do
echo -n " ... $(( NEXT_WAIT_TIME++ ))s" >&3
sleep $NEXT_WAIT_TIME
Expand All @@ -110,12 +109,6 @@ wait_for_ready_app() {
[ $NEXT_WAIT_TIME -lt $MAX_RETRIES ]
}

search_logs_for() {
CONTAINER=${1:?container name is a required parameter}
SEARCH_TERM=${2:?search term is a required parameter}
docker-compose logs ${CONTAINER} | grep -o ${SEARCH_TERM} | head -1
}

# Fail and display details if the expected and actual values do not
# equal. Details include both values.
#
Expand Down
154 changes: 79 additions & 75 deletions src/Honeycomb.OpenTelemetry/EnvironmentOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,83 +43,87 @@ internal EnvironmentOptions(IDictionary service)
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 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);

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;
}
}
// TODO: Delete this once the other thing is working
// 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;
// }
// }

private string GetEnvironmentVariable(string key, string defaultValue = "")
{
Expand Down
128 changes: 110 additions & 18 deletions src/Honeycomb.OpenTelemetry/HoneycombOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ namespace Honeycomb.OpenTelemetry
public class HoneycombOptions
{
private const string OtlpVersion = "0.16.0";
private const string OtelExporterOtlpProtcolHttp = "http/protobuf";
private const string OtelExporterOtlpProtcolGrpc = "grpc";
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}";
internal static readonly string SDefaultServiceVersion = "{unknown_service_version}";

private string _metricsApiKey;
private string _metricsEndpoint;
Expand All @@ -44,22 +49,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 Down Expand Up @@ -106,7 +95,6 @@ public class HoneycombOptions
/// </summary>
public string Endpoint { get; set; } = DefaultEndpoint;


/// <summary>
/// API endpoint to send telemetry data. Defaults to <see cref="Endpoint"/>.
/// </summary>
Expand Down Expand Up @@ -162,6 +150,110 @@ public class HoneycombOptions
/// </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;
}
}

/// <summary>
/// Computes the final traces endpoint.
/// </summary>
internal string GetTracesEndpoint()
{
var endpoint = new UriBuilder(Endpoint);
if (isHttp)
{
endpoint.Path = OtelExporterHttpTracesPath;
}
return TracesEndpoint ?? endpoint.ToString();
}

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

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

private static readonly Dictionary<string, string> CommandLineSwitchMap = new Dictionary<string, string>
{
{ "--honeycomb-apikey", "apikey" },
Expand Down
8 changes: 6 additions & 2 deletions src/Honeycomb.OpenTelemetry/MeterProviderBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace Honeycomb.OpenTelemetry
public static class MeterProviderBuilderExtensions
{

private static EnvironmentOptions _environmentOptions = new EnvironmentOptions(Environment.GetEnvironmentVariables());

/// <summary>
/// Configures the <see cref="MeterProviderBuilder"/> to send metrics telemetry data to Honeycomb using options created from command line arguments.
Expand All @@ -36,7 +35,12 @@ public static MeterProviderBuilder AddHoneycomb(this MeterProviderBuilder builde
/// </summary>
public static MeterProviderBuilder AddHoneycomb(this MeterProviderBuilder builder, HoneycombOptions options)
{
_environmentOptions.SetOptionsFromEnvironmentIfTheyExist(options);
options.ApplyEnvironmentOptions(new EnvironmentOptions(Environment.GetEnvironmentVariables()));

if (options is null)
{
throw new ArgumentNullException(nameof(options), "No Honeycomb options have been set in appsettings.json, environment variables, or the command line.");
}

// only enable metrics if a metrics dataset is set
if (!string.IsNullOrWhiteSpace(options.MetricsDataset))
Expand Down
Loading

0 comments on commit bac26f1

Please sign in to comment.