env

package
v2.693.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package env provides service identity values derived from environment variables and defaults.

This package defines small types and constructors for common identity fields that are used across go-service for consistent naming/versioning and outbound metadata, such as:

Environment variable overrides

Constructors in this package typically prefer a non-empty environment variable override and otherwise fall back to a derived default:

  • SERVICE_NAME: overrides the service name when non-empty (otherwise executable name)
  • SERVICE_VERSION: overrides the service version when non-empty (otherwise build/runtime version)
  • SERVICE_ID: overrides the service instance id when non-empty (otherwise generated)
  • SERVICE_USER_ID: overrides the service user id when non-empty (otherwise service name)

Conventions

Identity values are represented as small string wrapper types with String methods. These wrappers preserve the underlying semantics while keeping imports consistent across go-service.

Start with NewName, NewVersion, NewID, NewUserID, and NewUserAgent.

Index

Constants

This section is empty.

Variables

Module wires env-based service identity values into go.uber.org/fx/go.uber.org/dig.

It provides constructors for commonly used identity primitives derived from environment variables with sensible fallbacks:

Note: this module does not provide Name or Version directly; those are commonly constructed by callers using NewName (requires *os.FS) and NewVersion (uses runtime metadata). Consumers that depend on UserAgent typically wire Name and Version elsewhere in their module graph.

Functions

This section is empty.

Types

type Environment

type Environment string

Environment represents the runtime environment a service is running in.

This is typically used to drive environment-specific behavior (for example local/dev/stage/prod), and is commonly carried in configuration as a simple string value.

func (Environment) String

func (e Environment) String() string

String returns the environment value as a string.

This is a convenience method that preserves the underlying string value without normalization.

type ID

type ID string

ID is the service instance identifier.

func NewID

func NewID(generator id.Generator) ID

NewID returns a service instance identifier.

It prefers the SERVICE_ID environment variable when set (non-empty); otherwise it falls back to a newly generated id produced by generator.Generate().

Direct callers must pass a non-nil generator when SERVICE_ID is unset or empty. The standard module wiring supplies this generator through the id module.

This is commonly used to distinguish service instances in logs/metrics/traces when multiple replicas are running.

func (ID) String

func (id ID) String() string

String returns the id value as a string.

type Name

type Name string

Name is the service name.

func NewName

func NewName(fs *os.FS) Name

NewName returns the service name.

It prefers the SERVICE_NAME environment variable when non-empty; otherwise it falls back to the executable name as determined by [fs.ExecutableName].

The filesystem dependency exists to support consistent name derivation across environments and to enable tests to control the executable name behavior. Direct callers must pass a non-nil filesystem when SERVICE_NAME is unset or empty.

func (Name) String

func (n Name) String() string

String returns the name as a string.

type UserAgent

type UserAgent string

UserAgent is the HTTP User-Agent value for this service.

func NewUserAgent

func NewUserAgent(name Name, ver Version) UserAgent

NewUserAgent returns the service User-Agent value.

The returned value is formatted as:

"<name>/<version>"

Name and version come from the provided Name and Version values, including any normalization performed by Version.String.

This value is commonly used for outbound HTTP clients so requests can be attributed to a specific service and version by upstreams.

func (UserAgent) String

func (ua UserAgent) String() string

String returns the User-Agent value as a string.

type UserID added in v2.28.0

type UserID string

UserID is the service user identifier.

func NewUserID added in v2.28.0

func NewUserID(name Name) UserID

NewUserID returns a service user identifier.

It prefers the SERVICE_USER_ID environment variable when set (non-empty); otherwise it falls back to the service name.

This value is commonly used when a stable "user" identity is required for integrations that need a username/user-id concept, but where the service itself is the actor.

func (UserID) String added in v2.28.0

func (id UserID) String() string

String returns the user id value as a string.

type Version

type Version string

Version is the service version.

It is typically derived from SERVICE_VERSION (if set) or from runtime/build metadata.

func NewVersion

func NewVersion() Version

NewVersion returns the service version.

It prefers the SERVICE_VERSION environment variable when non-empty; otherwise it falls back to runtime.Version().

The returned Version is not normalized at construction time; normalization is applied when calling Version.String.

func (Version) String

func (v Version) String() string

String returns the version as a string.

Normalization: if the version is prefixed with "v" (for example "v1.2.3"), the prefix is stripped so callers get "1.2.3". Other values are returned unchanged.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL