// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.Text; using System.Text.Json; namespace EpicGames.Horde.Utilities; #pragma warning disable CA2227 // Collection properties should be read only /// /// OpenTelemetry configuration for collection and sending of traces and metrics. /// public class OpenTelemetrySettings { /// /// Whether OpenTelemetry exporting is enabled /// public bool Enabled { get; set; } = false; /// /// Service name /// public string ServiceName { get; set; } = "HordeServer"; /// /// Service namespace /// public string ServiceNamespace { get; set; } = "Horde"; /// /// Service version /// public string? ServiceVersion { get; set; } /// /// Whether to enrich and format telemetry to fit presentation in Datadog /// public bool EnableDatadogCompatibility { get; set; } = false; /// /// Extra attributes to set /// public Dictionary Attributes { get; set; } = new(); /// /// Whether to enable the console exporter (for debugging purposes) /// public bool EnableConsoleExporter { get; set; } = false; /// /// Protocol exporters (key is a unique and arbitrary name) /// public Dictionary ProtocolExporters { get; set; } = new(); } /// /// Configuration for an OpenTelemetry exporter /// public class OpenTelemetryProtocolExporterSettings { /// /// Endpoint URL. Usually differs depending on protocol used. /// public Uri? Endpoint { get; set; } /// /// Protocol for the exporter ('grpc' or 'httpprotobuf') /// public string Protocol { get; set; } = "grpc"; } /// /// Provides extension methods for serializing and deserializing OpenTelemetrySettings /// public static class OpenTelemetrySettingsExtensions { /// /// Serializes OpenTelemetrySettings to a JSON string, with an option to encode as base64 /// /// OpenTelemetrySettings to serialize. /// If true, the resulting JSON string is encoded as base64 public static string Serialize(OpenTelemetrySettings settings, bool asBase64 = false) { string data = JsonSerializer.Serialize(settings); return asBase64 ? Convert.ToBase64String(Encoding.UTF8.GetBytes(data)) : data; } /// /// Deserializes a JSON string of OpenTelemetrySettings /// /// The string to deserialize, which can be either a JSON string or a Base64 encoded JSON string. /// If true, the input string is treated as base64 encoded /// The deserialized OpenTelemetrySettings /// Thrown when deserialization fails or results in a null object. public static OpenTelemetrySettings Deserialize(string data, bool asBase64 = false) { byte[] temp = asBase64 ? Convert.FromBase64String(data) : Encoding.UTF8.GetBytes(data); OpenTelemetrySettings? settings = JsonSerializer.Deserialize(temp); if (settings == null) { throw new JsonException($"Unable to deserialize {nameof(OpenTelemetrySettings)} from JSON"); } return settings; } }