Documentation
¶
Overview ¶
Package profiles manages named Dash0 configuration profiles.
Profiles are stored in ~/.dash0/profiles.json as named sets of connection parameters (API URL, auth token, OTLP URL, dataset). One profile is marked as active in ~/.dash0/activeProfile and used as the default configuration. Environment variables (DASH0_API_URL, DASH0_AUTH_TOKEN, DASH0_OTLP_URL, DASH0_DATASET) override the active profile.
This package imports the root dash0 package for dash0.ClientOption and dash0.DatasetPtr -- never the reverse.
Index ¶
- Constants
- Variables
- func WithConfiguration(ctx context.Context, cfg *Configuration) context.Context
- type Configuration
- type Profile
- type ProfilesFile
- type Store
- func (s *Store) AddProfile(profile Profile) error
- func (s *Store) GetActiveConfiguration() (*Configuration, error)
- func (s *Store) GetActiveProfile() (*Profile, error)
- func (s *Store) GetProfiles() ([]Profile, error)
- func (s *Store) RemoveProfile(profileName string) error
- func (s *Store) SetActiveProfile(profileName string) error
- func (s *Store) UpdateProfile(name string, updateFn func(*Configuration)) error
- type StoreOption
Examples ¶
Constants ¶
const ( // ConfigDirName is the name of the directory containing Dash0 configuration. ConfigDirName = ".dash0" // ProfilesFileName is the name of the file containing profile configurations. ProfilesFileName = "profiles.json" // ActiveProfileFileName is the name of the file containing the active profile name. ActiveProfileFileName = "activeProfile" // EnvApiUrl is the environment variable for the Dash0 API URL. EnvApiUrl = "DASH0_API_URL" // EnvAuthToken is the environment variable for the Dash0 auth token. EnvAuthToken = "DASH0_AUTH_TOKEN" // EnvOtlpUrl is the environment variable for the Dash0 OTLP URL. EnvOtlpUrl = "DASH0_OTLP_URL" // EnvDataset is the environment variable for the Dash0 dataset. EnvDataset = "DASH0_DATASET" // EnvConfigDir is the environment variable that overrides the default // configuration directory. EnvConfigDir = "DASH0_CONFIG_DIR" )
Variables ¶
var ( // ErrNoActiveProfile is returned when there is no active profile. ErrNoActiveProfile = errors.New("no active profile configured") // ErrProfileNotFound is returned when a requested profile is not found. ErrProfileNotFound = errors.New("profile not found") )
Functions ¶
func WithConfiguration ¶
func WithConfiguration(ctx context.Context, cfg *Configuration) context.Context
WithConfiguration returns a new context with the given configuration stored.
Example ¶
package main
import (
"context"
"fmt"
"github.com/dash0hq/dash0-api-client-go/profiles"
)
func main() {
cfg := &profiles.Configuration{
ApiUrl: "https://api.eu-west-1.aws.dash0.com",
AuthToken: "auth_example-token",
Dataset: "production",
}
ctx := profiles.WithConfiguration(context.Background(), cfg)
retrieved := profiles.FromContext(ctx)
fmt.Println(retrieved.ApiUrl)
}
Output: https://api.eu-west-1.aws.dash0.com
Types ¶
type Configuration ¶
type Configuration struct {
ApiUrl string `json:"apiUrl"`
AuthToken string `json:"authToken"`
OtlpUrl string `json:"otlpUrl,omitempty"`
Dataset string `json:"dataset,omitempty"`
}
Configuration represents a Dash0 configuration with connection parameters.
func FromContext ¶
func FromContext(ctx context.Context) *Configuration
FromContext retrieves the configuration from ctx, or nil if not present.
func ResolveConfiguration ¶
func ResolveConfiguration(apiUrl, authToken string, opts ...StoreOption) (*Configuration, error)
ResolveConfiguration loads the active profile, applies environment variable overrides, then applies the given parameter overrides on top. Non-empty parameters take highest precedence.
This is a convenience function that creates a temporary Store internally. Pass StoreOption values (e.g. WithConfigDir) to control how the profile store is constructed.
Example (ParameterOverride) ¶
package main
import (
"fmt"
"os"
"github.com/dash0hq/dash0-api-client-go/profiles"
)
func main() {
// Parameters take highest precedence, overriding both profiles and env vars.
configDir, _ := os.MkdirTemp("", "dash0-example-*")
defer func() { _ = os.RemoveAll(configDir) }()
store, _ := profiles.NewStore(profiles.WithConfigDir(configDir))
_ = store.AddProfile(profiles.Profile{
Name: "dev",
Configuration: profiles.Configuration{
ApiUrl: "https://api.eu-west-1.aws.dash0.com",
AuthToken: "auth_dev-token",
},
})
cfg, err := profiles.ResolveConfiguration(
"https://api.us-west-2.aws.dash0.com", // overrides profile's API URL
"", // falls back to profile's auth token
profiles.WithConfigDir(configDir),
)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(cfg.ApiUrl)
fmt.Println(cfg.AuthToken)
}
Output: https://api.us-west-2.aws.dash0.com auth_dev-token
func ResolveConfigurationWithOtlp ¶
func ResolveConfigurationWithOtlp(apiUrl, authToken, otlpUrl, dataset string, opts ...StoreOption) (*Configuration, error)
ResolveConfigurationWithOtlp is like ResolveConfiguration but also accepts OTLP URL and dataset overrides.
func (*Configuration) ClientOptions ¶
func (cfg *Configuration) ClientOptions() []dash0.ClientOption
ClientOptions returns dash0.ClientOption values that configure a client from this Configuration. Non-empty fields are mapped as follows:
- ApiUrl -> dash0.WithApiUrl
- AuthToken -> dash0.WithAuthToken
- OtlpUrl -> dash0.WithOtlpEndpoint with dash0.OtlpEncodingJson
The Dataset field is not mapped because it is a per-request parameter, not a client-level setting. Use Configuration.DatasetPtr to convert it for API calls.
Callers can append additional options to override or supplement the returned slice:
opts := cfg.ClientOptions()
opts = append(opts, dash0.WithUserAgent("my-tool/1.0"))
client, err := dash0.NewClient(opts...)
Example ¶
package main
import (
"fmt"
"github.com/dash0hq/dash0-api-client-go/profiles"
)
func main() {
cfg := &profiles.Configuration{
ApiUrl: "https://api.eu-west-1.aws.dash0.com",
AuthToken: "auth_example-token",
}
opts := cfg.ClientOptions()
fmt.Printf("produced %d client options\n", len(opts))
}
Output: produced 2 client options
func (*Configuration) DatasetPtr ¶
func (cfg *Configuration) DatasetPtr() *string
DatasetPtr returns the dataset as a *string suitable for Dash0 API calls. It returns nil for empty strings and "default", matching dash0.DatasetPtr.
Example ¶
package main
import (
"fmt"
"github.com/dash0hq/dash0-api-client-go/profiles"
)
func main() {
cfg := &profiles.Configuration{Dataset: "production"}
ptr := cfg.DatasetPtr()
fmt.Println(*ptr)
cfgDefault := &profiles.Configuration{Dataset: "default"}
ptrDefault := cfgDefault.DatasetPtr()
fmt.Println(ptrDefault)
}
Output: production <nil>
type Profile ¶
type Profile struct {
Name string `json:"name"`
Configuration Configuration `json:"configuration"`
}
Profile represents a named configuration profile.
type ProfilesFile ¶
type ProfilesFile struct {
Profiles []Profile `json:"profiles"`
}
ProfilesFile represents the file storing multiple profiles.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store handles profile storage and retrieval.
func NewStore ¶
func NewStore(opts ...StoreOption) (*Store, error)
NewStore creates a new profile store.
The configuration directory is resolved in this order:
- Explicit WithConfigDir option (if provided).
- The DASH0_CONFIG_DIR environment variable (if set).
- ~/.dash0/ (default).
Example ¶
package main
import (
"fmt"
"github.com/dash0hq/dash0-api-client-go/profiles"
)
func main() {
svc, err := profiles.NewStore()
if err != nil {
fmt.Println("error:", err)
return
}
allProfiles, err := svc.GetProfiles()
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Printf("found %d profiles\n", len(allProfiles))
}
Output:
Example (WithConfigDir) ¶
package main
import (
"fmt"
"github.com/dash0hq/dash0-api-client-go/profiles"
)
func main() {
svc, err := profiles.NewStore(profiles.WithConfigDir("/tmp/dash0-test"))
if err != nil {
fmt.Println("error:", err)
return
}
allProfiles, err := svc.GetProfiles()
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Printf("found %d profiles\n", len(allProfiles))
}
Output: found 0 profiles
func (*Store) AddProfile ¶
AddProfile adds a new profile to the configuration. If a profile with the same name already exists, it is replaced. When adding the first profile, it is automatically set as active.
func (*Store) GetActiveConfiguration ¶
func (s *Store) GetActiveConfiguration() (*Configuration, error)
GetActiveConfiguration returns the currently active configuration. Environment variables take precedence over the active profile.
Example ¶
package main
import (
"fmt"
"os"
"github.com/dash0hq/dash0-api-client-go/profiles"
)
func main() {
// Create a temporary config directory with a profile.
configDir, _ := os.MkdirTemp("", "dash0-example-*")
defer func() { _ = os.RemoveAll(configDir) }()
store, _ := profiles.NewStore(profiles.WithConfigDir(configDir))
_ = store.AddProfile(profiles.Profile{
Name: "dev",
Configuration: profiles.Configuration{
ApiUrl: "https://api.eu-west-1.aws.dash0.com",
AuthToken: "auth_dev-token",
Dataset: "staging",
},
})
cfg, err := store.GetActiveConfiguration()
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(cfg.ApiUrl)
fmt.Println(cfg.Dataset)
}
Output: https://api.eu-west-1.aws.dash0.com staging
Example (EnvVarOverride) ¶
package main
import (
"fmt"
"os"
"github.com/dash0hq/dash0-api-client-go/profiles"
)
func main() {
// Environment variables override values from the active profile.
configDir, _ := os.MkdirTemp("", "dash0-example-*")
defer func() { _ = os.RemoveAll(configDir) }()
store, _ := profiles.NewStore(profiles.WithConfigDir(configDir))
_ = store.AddProfile(profiles.Profile{
Name: "dev",
Configuration: profiles.Configuration{
ApiUrl: "https://api.eu-west-1.aws.dash0.com",
AuthToken: "auth_dev-token",
Dataset: "staging",
},
})
// DASH0_DATASET overrides the profile's dataset.
_ = os.Setenv(profiles.EnvDataset, "production")
defer func() { _ = os.Unsetenv(profiles.EnvDataset) }()
cfg, err := store.GetActiveConfiguration()
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(cfg.ApiUrl)
fmt.Println(cfg.Dataset)
}
Output: https://api.eu-west-1.aws.dash0.com production
func (*Store) GetActiveProfile ¶
GetActiveProfile returns the currently active profile.
func (*Store) GetProfiles ¶
GetProfiles returns all available profiles.
func (*Store) RemoveProfile ¶
RemoveProfile removes a profile from the configuration. If the removed profile was the active profile, the first remaining profile becomes active.
func (*Store) SetActiveProfile ¶
SetActiveProfile sets the active profile by name. Returns ErrProfileNotFound if no profile with the given name exists.
func (*Store) UpdateProfile ¶
func (s *Store) UpdateProfile(name string, updateFn func(*Configuration)) error
UpdateProfile finds a profile by name and applies updateFn to its configuration, then saves. Returns an error if no profile with the given name exists.
type StoreOption ¶
type StoreOption func(*storeConfig)
StoreOption configures a Store.
func WithConfigDir ¶
func WithConfigDir(dir string) StoreOption
WithConfigDir overrides the default configuration directory (~/.dash0/). This is useful for testing or for applications that store profiles in a non-standard location.