Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion internal/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ func TestVerifySupportedConfiguration(t *testing.T) {
cfg, err := readSupportedConfigurations(getConfigFilePath())
require.NoError(t, err)
require.Contains(t, cfg.SupportedConfigurations, "DD_UNKNOWN_CONFIGURATION_KEY")
require.Equal(t, []string{"A"}, cfg.SupportedConfigurations["DD_UNKNOWN_CONFIGURATION_KEY"])
require.Equal(t, []configurationImplementation{{
Implementation: "A",
Type: "FIX_ME",
Default: "FIX_ME",
}}, cfg.SupportedConfigurations["DD_UNKNOWN_CONFIGURATION_KEY"])

// Remove the env var from the supported configurations file
delete(cfg.SupportedConfigurations, "DD_UNKNOWN_CONFIGURATION_KEY")
Expand Down
41 changes: 30 additions & 11 deletions internal/env/supported_configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package env

import (
"bytes"
"encoding/json"
"fmt"
"os"
Expand All @@ -16,17 +17,23 @@ import (
"github.com/DataDog/dd-trace-go/v2/internal/log"
)

type configurationImplementation struct {
Implementation string `json:"implementation"`
Type string `json:"type"`
Default string `json:"default"`
Aliases []string `json:"aliases,omitempty"`
}

// SupportedConfiguration represents the content of the supported_configurations.json file.
type SupportedConfiguration struct {
SupportedConfigurations map[string][]string `json:"supportedConfigurations"`
Aliases map[string][]string `json:"aliases"`
type supportedConfiguration struct {
Version string `json:"version"`
SupportedConfigurations map[string][]configurationImplementation `json:"supportedConfigurations"`
}

var (
configFilePath string
once sync.Once
mu sync.Mutex
skipLock bool
)

// getConfigFilePath returns the path to the supported_configurations.json file
Expand Down Expand Up @@ -65,34 +72,46 @@ func addSupportedConfigurationToFile(name string) {
}

if _, ok := cfg.SupportedConfigurations[name]; !ok {
cfg.SupportedConfigurations[name] = []string{"A"}
cfg.SupportedConfigurations[name] = []configurationImplementation{
{
Implementation: "A",
Type: "FIX_ME",
Default: "FIX_ME",
},
}
}

if err := writeSupportedConfigurations(filePath, cfg); err != nil {
log.Error("config: failed to write supported configurations: %s", err.Error())
}
}

func readSupportedConfigurations(filePath string) (*SupportedConfiguration, error) {
func readSupportedConfigurations(filePath string) (*supportedConfiguration, error) {
// read the json file
jsonFile, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("failed to open supported_configurations.json: %w", err)
}

var cfg SupportedConfiguration
var cfg supportedConfiguration
if err := json.Unmarshal(jsonFile, &cfg); err != nil {
return nil, fmt.Errorf("failed to unmarshal SupportedConfiguration: %w", err)
}
return &cfg, nil
}

func writeSupportedConfigurations(filePath string, cfg *SupportedConfiguration) error {
// write the json file - Go's json.MarshalIndent automatically sorts map keys
jsonFile, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
func writeSupportedConfigurations(filePath string, cfg *supportedConfiguration) error {
// Write the JSON file. We explicitly disable HTML escaping so strings like "&"
// remain readable (and stable across test runs) instead of being rendered as
// "\u0026". Map keys are still deterministically sorted by encoding/json.
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
if err := enc.Encode(cfg); err != nil {
return fmt.Errorf("failed to marshal SupportedConfiguration: %w", err)
}
jsonFile := bytes.TrimRight(buf.Bytes(), "\n")

if err := os.WriteFile(filePath, jsonFile, 0644); err != nil {
return fmt.Errorf("failed to write supported_configurations.json: %w", err)
Expand Down
Loading
Loading