| 1 | package victoriascip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/ |
| 2 | |
| 3 | import ( |
| 4 | "net/httpscip-go gomod github.com/golang/go/src go1.21 `net/http`/" |
| 5 | "timescip-go gomod github.com/golang/go/src go1.21 time/" |
| 6 | ) |
| 7 | |
| 8 | // Option is a function that modifies Config. |
| 9 | // Used with the New() constructor for flexible configuration. |
| 10 | // Multiple options can be chained together. |
| 11 | // |
| 12 | // Example: |
| 13 | // |
| 14 | // handler, err := victoria.New( |
| 15 | // victoria.WithURL("http://logs.example.com"), |
| 16 | // victoria.WithAppName("api-server"), |
| 17 | // victoria.WithEnvironment("production"), |
| 18 | // victoria.WithBatching(100, 2*time.Second), |
| 19 | // ) |
| 20 | type Optionscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Option# func(*Configscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#) |
| 21 | |
| 22 | // WithURL sets the VictoriaLogs endpoint URL. |
| 23 | // The URL should point to VictoriaLogs' JSON ingestion endpoint. |
| 24 | // Default: "http://localhost:9428/insert/jsonline" |
| 25 | // |
| 26 | // Example: |
| 27 | // |
| 28 | // victoria.WithURL("http://victoria-logs.prod.svc:9428/insert/jsonline") |
| 29 | func WithURLscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/WithURL().(url string) Optionscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Option# { |
| 30 | return func(c *Configscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#) { |
| 31 | c.URLscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#URL. = url |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // WithAppName sets the application name for log identification. |
| 36 | // This appears in the "app" field of all log entries and is used as |
| 37 | // a stream label for efficient querying in VictoriaLogs. |
| 38 | // Default: executable name (without extension) |
| 39 | // |
| 40 | // Example: |
| 41 | // |
| 42 | // victoria.WithAppName("order-service") |
| 43 | func WithAppNamescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/WithAppName().(name string) Optionscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Option# { |
| 44 | return func(c *Configscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#) { |
| 45 | c.AppNamescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#AppName. = name |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // WithVersion sets the application version. |
| 50 | // Useful for tracking deployments and version-specific issues. |
| 51 | // Default: "unknown" |
| 52 | // |
| 53 | // Example: |
| 54 | // |
| 55 | // victoria.WithVersion("2.1.0") |
| 56 | func WithVersionscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/WithVersion().(version string) Optionscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Option# { |
| 57 | return func(c *Configscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#) { |
| 58 | c.Versionscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#Version. = version |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // WithEnvironment sets the deployment environment. |
| 63 | // Common values: "production", "staging", "development", "testing" |
| 64 | // This field is used as a stream label for environment-based filtering. |
| 65 | // Default: "production" |
| 66 | // |
| 67 | // Example: |
| 68 | // |
| 69 | // victoria.WithEnvironment("staging") |
| 70 | func WithEnvironmentscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/WithEnvironment().(env string) Optionscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Option# { |
| 71 | return func(c *Configscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#) { |
| 72 | c.Environmentscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#Environment. = env |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // WithHostname sets the hostname for log entries. |
| 77 | // Useful in distributed systems to identify which server or pod generated the log. |
| 78 | // Default: os.Hostname() result |
| 79 | // |
| 80 | // Example: |
| 81 | // |
| 82 | // victoria.WithHostname("web-server-01") |
| 83 | func WithHostnamescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/WithHostname().(hostname string) Optionscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Option# { |
| 84 | return func(c *Configscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#) { |
| 85 | c.Hostnamescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#Hostname. = hostname |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // WithStreamKeys sets the fields to use as stream labels in VictoriaLogs. |
| 90 | // Stream labels enable efficient partitioning and querying of logs. |
| 91 | // Common stream keys: ["app", "env", "level", "ns", "host"] |
| 92 | // Default: ["app", "env", "level", "ns", "host"] |
| 93 | // |
| 94 | // Example: |
| 95 | // |
| 96 | // // Use application and environment as primary stream labels |
| 97 | // victoria.WithStreamKeys("app", "env", "level") |
| 98 | func WithStreamKeysscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/WithStreamKeys().(keys ...string) Optionscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Option# { |
| 99 | return func(c *Configscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#) { |
| 100 | c.StreamKeysscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#StreamKeys. = keys |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // WithFieldMapping adds a custom field name mapping. |
| 105 | // Useful for renaming fields to match existing log schemas or conventions. |
| 106 | // The mapping is applied to all log entries processed by the handler. |
| 107 | // |
| 108 | // Example: |
| 109 | // |
| 110 | // // Rename "user_id" field to "userId" |
| 111 | // victoria.WithFieldMapping("user_id", "userId") |
| 112 | func WithFieldMappingscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/WithFieldMapping().(from, to string) Optionscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Option# { |
| 113 | return func(c *Configscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#) { |
| 114 | c.FieldMapscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#FieldMap.[from] = to |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // WithHTTPClient sets a custom HTTP client for VictoriaLogs requests. |
| 119 | // Use this to customize timeouts, authentication, TLS configuration, |
| 120 | // or to use a proxy. If not set, a default client with reasonable |
| 121 | // timeouts is created. |
| 122 | // |
| 123 | // Example: |
| 124 | // |
| 125 | // client := &http.Client{ |
| 126 | // Timeout: 30 * time.Second, |
| 127 | // Transport: &http.Transport{ |
| 128 | // TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 129 | // }, |
| 130 | // } |
| 131 | // victoria.WithHTTPClient(client) |
| 132 | func WithHTTPClientscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/WithHTTPClient().(client *httpscip-go gomod github.com/golang/go/src go1.21 `net/http`/.Clientscip-go gomod github.com/golang/go/src go1.21 `net/http`/Client#) Optionscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Option# { |
| 133 | return func(c *Configscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#) { |
| 134 | c.HTTPClientscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#HTTPClient. = client |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // WithTimeout sets the HTTP request timeout. |
| 139 | // This timeout applies to both connection establishment and request completion. |
| 140 | // Consider network latency and VictoriaLogs processing time when setting this. |
| 141 | // Default: 5 seconds |
| 142 | // |
| 143 | // Example: |
| 144 | // |
| 145 | // // Allow more time for cross-region requests |
| 146 | // victoria.WithTimeout(10 * time.Second) |
| 147 | func WithTimeoutscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/WithTimeout().(timeout timescip-go gomod github.com/golang/go/src go1.21 time/.Durationscip-go gomod github.com/golang/go/src go1.21 time/Duration#) Optionscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Option# { |
| 148 | return func(c *Configscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#) { |
| 149 | c.Timeoutscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#Timeout. = timeout |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // WithRetry sets the number of retry attempts for failed requests. |
| 154 | // Retries use exponential backoff (0ms, 100ms, 400ms, ...). |
| 155 | // Note: 4xx errors (client errors) are not retried. |
| 156 | // Default: 0 (no retry) |
| 157 | // |
| 158 | // Example: |
| 159 | // |
| 160 | // // Retry up to 3 times on network failures |
| 161 | // victoria.WithRetry(3) |
| 162 | func WithRetryscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/WithRetry().(count int) Optionscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Option# { |
| 163 | return func(c *Configscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#) { |
| 164 | c.RetryCountscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#RetryCount. = count |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // WithDevMode enables development mode features. |
| 169 | // When enabled: |
| 170 | // - Handler pings VictoriaLogs endpoint on initialization |
| 171 | // - Debug metadata (_handler, _batch) is added to log entries |
| 172 | // - May enable more verbose error reporting |
| 173 | // |
| 174 | // Default: false (disabled) |
| 175 | // |
| 176 | // Example: |
| 177 | // |
| 178 | // // Enable during development, disable in production |
| 179 | // victoria.WithDevMode(os.Getenv("ENV") == "development") |
| 180 | func WithDevModescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/WithDevMode().(enabled bool) Optionscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Option# { |
| 181 | return func(c *Configscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#) { |
| 182 | c.DevModescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/l3rd/victoria`/Config#DevMode. = enabled |
| 183 | } |
| 184 | } |