| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package observ // import "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp/internal/observ" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"net" |
| 11 |
"net/http" |
| 12 |
"net/netip" |
| 13 |
"strconv" |
| 14 |
"strings" |
| 15 |
"sync" |
| 16 |
"time" |
| 17 |
|
| 18 |
"go.opentelemetry.io/otel" |
| 19 |
"go.opentelemetry.io/otel/attribute" |
| 20 |
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp/internal" |
| 21 |
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp/internal/x" |
| 22 |
"go.opentelemetry.io/otel/internal/global" |
| 23 |
"go.opentelemetry.io/otel/metric" |
| 24 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 25 |
"go.opentelemetry.io/otel/semconv/v1.40.0/otelconv" |
| 26 |
) |
| 27 |
|
| 28 |
const ( |
| 29 |
// ScopeName is the unique name of the meter used for instrumentation. |
| 30 |
ScopeName = "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp/internal/observ" |
| 31 |
|
| 32 |
// Version is the current version of this instrumentation |
| 33 |
// |
| 34 |
// This matches the version of the exporter. |
| 35 |
Version = internal.Version |
| 36 |
) |
| 37 |
|
| 38 |
var ( |
| 39 |
attrsPool = &sync.Pool{ |
| 40 |
New: func() any { |
| 41 |
const n = 1 + // component.name |
| 42 |
1 + // component.type |
| 43 |
1 + // server.addr |
| 44 |
1 + // server.port |
| 45 |
1 + // error.port |
| 46 |
1 // http.response.status.code |
| 47 |
s := make([]attribute.KeyValue, 0, n) |
| 48 |
return &s |
| 49 |
}, |
| 50 |
} |
| 51 |
addOptPool = &sync.Pool{ |
| 52 |
New: func() any { |
| 53 |
const n = 1 // WithAttributeSet |
| 54 |
s := make([]metric.AddOption, 0, n) |
| 55 |
return &s |
| 56 |
}, |
| 57 |
} |
| 58 |
recordPool = &sync.Pool{ |
| 59 |
New: func() any { |
| 60 |
const n = 1 // WithAttributeSet |
| 61 |
s := make([]metric.RecordOption, 0, n) |
| 62 |
return &s |
| 63 |
}, |
| 64 |
} |
| 65 |
) |
| 66 |
|
| 67 |
func get[T any](pool *sync.Pool) *[]T { |
| 68 |
return pool.Get().(*[]T) |
| 69 |
} |
| 70 |
|
| 71 |
func put[T any](pool *sync.Pool, value *[]T) { |
| 72 |
*value = (*value)[:0] |
| 73 |
pool.Put(value) |
| 74 |
} |
| 75 |
|
| 76 |
// GetComponentName returns the constant name for the exporter with the |
| 77 |
// provided id. |
| 78 |
func GetComponentName(id int64) string { |
| 79 |
return fmt.Sprintf("%s/%d", otelconv.ComponentTypeOtlpHTTPLogExporter, id) |
| 80 |
} |
| 81 |
|
| 82 |
// Instrumentation is experimental instrumentation for the exporter. |
| 83 |
type Instrumentation struct { |
| 84 |
inflightMetric metric.Int64UpDownCounter |
| 85 |
exportedMetric metric.Int64Counter |
| 86 |
operationDuration metric.Float64Histogram |
| 87 |
|
| 88 |
presetAttrs []attribute.KeyValue |
| 89 |
addOpt metric.AddOption |
| 90 |
recordOpt metric.RecordOption |
| 91 |
} |
| 92 |
|
| 93 |
// NewInstrumentation returns instrumentation for otlplog http exporter. |
| 94 |
func NewInstrumentation(id int64, target string) (*Instrumentation, error) { |
| 95 |
if !x.Observability.Enabled() { |
| 96 |
return nil, nil |
| 97 |
} |
| 98 |
|
| 99 |
inst := &Instrumentation{} |
| 100 |
|
| 101 |
provider := otel.GetMeterProvider() |
| 102 |
m := provider.Meter( |
| 103 |
ScopeName, |
| 104 |
metric.WithSchemaURL(semconv.SchemaURL), |
| 105 |
metric.WithInstrumentationVersion(Version), |
| 106 |
) |
| 107 |
|
| 108 |
var e, err error |
| 109 |
logInflight, e := otelconv.NewSDKExporterLogInflight(m) |
| 110 |
if e != nil { |
| 111 |
e = fmt.Errorf("failed to create the inflight metric %w", e) |
| 112 |
err = errors.Join(err, e) |
| 113 |
} |
| 114 |
inst.inflightMetric = logInflight.Inst() |
| 115 |
|
| 116 |
exported, e := otelconv.NewSDKExporterLogExported(m) |
| 117 |
if e != nil { |
| 118 |
e = fmt.Errorf("failed to create the exported metric %w", e) |
| 119 |
err = errors.Join(err, e) |
| 120 |
} |
| 121 |
inst.exportedMetric = exported.Inst() |
| 122 |
|
| 123 |
operation, e := otelconv.NewSDKExporterOperationDuration(m) |
| 124 |
if e != nil { |
| 125 |
e = fmt.Errorf("failed to create the operation duration metric %w", e) |
| 126 |
err = errors.Join(err, e) |
| 127 |
} |
| 128 |
inst.operationDuration = operation.Inst() |
| 129 |
|
| 130 |
if err != nil { |
| 131 |
return nil, err |
| 132 |
} |
| 133 |
|
| 134 |
inst.presetAttrs = setPresetAttrs(GetComponentName(id), target) |
| 135 |
|
| 136 |
inst.addOpt = metric.WithAttributeSet(attribute.NewSet(inst.presetAttrs...)) |
| 137 |
inst.recordOpt = metric.WithAttributeSet(attribute.NewSet(append( |
| 138 |
[]attribute.KeyValue{semconv.HTTPResponseStatusCode(http.StatusOK)}, |
| 139 |
inst.presetAttrs..., |
| 140 |
)...)) |
| 141 |
|
| 142 |
return inst, nil |
| 143 |
} |
| 144 |
|
| 145 |
func setPresetAttrs(name, target string) []attribute.KeyValue { |
| 146 |
addrAttrs := ServerAddrAttrs(target) |
| 147 |
|
| 148 |
attrs := make([]attribute.KeyValue, 0, 2+len(addrAttrs)) |
| 149 |
attrs = append( |
| 150 |
attrs, |
| 151 |
semconv.OTelComponentName(name), |
| 152 |
semconv.OTelComponentTypeOtlpHTTPLogExporter, |
| 153 |
) |
| 154 |
attrs = append(attrs, addrAttrs...) |
| 155 |
return attrs |
| 156 |
} |
| 157 |
|
| 158 |
// ServerAddrAttrs is a function that extracts server address and port attributes |
| 159 |
// from a target string. |
| 160 |
func ServerAddrAttrs(target string) []attribute.KeyValue { |
| 161 |
host, port, err := parseTarget(target) |
| 162 |
if err != nil || (host == "" && port < 0) { |
| 163 |
if err != nil { |
| 164 |
global.Debug("failed to parse target", "target", target, "error", err) |
| 165 |
} |
| 166 |
return nil |
| 167 |
} |
| 168 |
|
| 169 |
if port < 0 { |
| 170 |
return []attribute.KeyValue{semconv.ServerAddress(host)} |
| 171 |
} |
| 172 |
|
| 173 |
if host == "" { |
| 174 |
return []attribute.KeyValue{ |
| 175 |
semconv.ServerPort(port), |
| 176 |
} |
| 177 |
} |
| 178 |
return []attribute.KeyValue{ |
| 179 |
semconv.ServerAddress(host), |
| 180 |
semconv.ServerPort(port), |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
func (i *Instrumentation) ExportLogs(ctx context.Context, count int64) ExportOp { |
| 185 |
start := time.Now() |
| 186 |
|
| 187 |
if i.inflightMetric.Enabled(ctx) { |
| 188 |
addOpt := get[metric.AddOption](addOptPool) |
| 189 |
defer put(addOptPool, addOpt) |
| 190 |
*addOpt = append(*addOpt, i.addOpt) |
| 191 |
i.inflightMetric.Add(ctx, count, *addOpt...) |
| 192 |
} |
| 193 |
|
| 194 |
return ExportOp{ |
| 195 |
ctx: ctx, |
| 196 |
inst: i, |
| 197 |
count: count, |
| 198 |
start: start, |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
// ExportOp tracks the operationDuration being observed by [Instrumentation.ExportLogs]. |
| 203 |
type ExportOp struct { |
| 204 |
ctx context.Context |
| 205 |
start time.Time |
| 206 |
inst *Instrumentation |
| 207 |
count int64 |
| 208 |
} |
| 209 |
|
| 210 |
// End completes the observation of the operationDuration being observed by a call to |
| 211 |
// [Instrumentation.ExportLogs]. |
| 212 |
// Any error that is encountered is provided as err. |
| 213 |
// |
| 214 |
// If err is not nil, all logs will be recorded as failures unless error is of |
| 215 |
// type [internal.PartialSuccess]. In the case of a PartialSuccess, the number |
| 216 |
// of successfully exported logs will be determined by inspecting the |
| 217 |
// RejectedItems field of the PartialSuccess. |
| 218 |
func (e ExportOp) End(err error, code int) { |
| 219 |
inflightEnabled := e.inst.inflightMetric.Enabled(e.ctx) |
| 220 |
exportedEnabled := e.inst.exportedMetric.Enabled(e.ctx) |
| 221 |
durationEnabled := e.inst.operationDuration.Enabled(e.ctx) |
| 222 |
|
| 223 |
if !inflightEnabled && !exportedEnabled && !durationEnabled { |
| 224 |
return |
| 225 |
} |
| 226 |
|
| 227 |
var success int64 |
| 228 |
if inflightEnabled || exportedEnabled { |
| 229 |
addOpt := get[metric.AddOption](addOptPool) |
| 230 |
defer put(addOptPool, addOpt) |
| 231 |
*addOpt = append(*addOpt, e.inst.addOpt) |
| 232 |
|
| 233 |
if inflightEnabled { |
| 234 |
e.inst.inflightMetric.Add(e.ctx, -e.count, *addOpt...) |
| 235 |
} |
| 236 |
if exportedEnabled { |
| 237 |
success = successful(e.count, err) |
| 238 |
e.inst.exportedMetric.Add(e.ctx, success, *addOpt...) |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
if err != nil && exportedEnabled { |
| 243 |
attrs := get[attribute.KeyValue](attrsPool) |
| 244 |
defer put(attrsPool, attrs) |
| 245 |
|
| 246 |
*attrs = append(*attrs, e.inst.presetAttrs...) |
| 247 |
*attrs = append(*attrs, semconv.ErrorType(err)) |
| 248 |
|
| 249 |
a := metric.WithAttributeSet(attribute.NewSet(*attrs...)) |
| 250 |
e.inst.exportedMetric.Add(e.ctx, e.count-success, a) |
| 251 |
} |
| 252 |
|
| 253 |
if durationEnabled { |
| 254 |
record := get[metric.RecordOption](recordPool) |
| 255 |
defer put(recordPool, record) |
| 256 |
*record = append(*record, e.recordOption(err, code)) |
| 257 |
duration := time.Since(e.start).Seconds() |
| 258 |
e.inst.operationDuration.Record(e.ctx, duration, *record...) |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
func (e ExportOp) recordOption(err error, code int) metric.RecordOption { |
| 263 |
if err == nil { |
| 264 |
return e.inst.recordOpt |
| 265 |
} |
| 266 |
|
| 267 |
attrs := get[attribute.KeyValue](attrsPool) |
| 268 |
defer put(attrsPool, attrs) |
| 269 |
|
| 270 |
*attrs = append(*attrs, e.inst.presetAttrs...) |
| 271 |
*attrs = append( |
| 272 |
*attrs, |
| 273 |
semconv.HTTPResponseStatusCode(code), |
| 274 |
semconv.ErrorType(err), |
| 275 |
) |
| 276 |
return metric.WithAttributeSet(attribute.NewSet(*attrs...)) |
| 277 |
} |
| 278 |
|
| 279 |
// successful returns the number of successfully exported logs out of the n |
| 280 |
// that were exported based on the provided error. |
| 281 |
// |
| 282 |
// If err is nil, n is returned. All logs were successfully exported. |
| 283 |
// |
| 284 |
// If err is not nil and not an [internal.PartialSuccess] error, 0 is returned. |
| 285 |
// It is assumed all logs failed to be exported. |
| 286 |
// |
| 287 |
// If err is an [internal.PartialSuccess] error, the number of successfully |
| 288 |
// exported logs is computed by subtracting the RejectedItems field from n. If |
| 289 |
// RejectedItems is negative, n is returned. If RejectedItems is greater than |
| 290 |
// n, 0 is returned. |
| 291 |
func successful(count int64, err error) int64 { |
| 292 |
if err == nil { |
| 293 |
return count |
| 294 |
} |
| 295 |
return count - rejected(count, err) |
| 296 |
} |
| 297 |
|
| 298 |
var errPool = sync.Pool{ |
| 299 |
New: func() any { |
| 300 |
return new(internal.PartialSuccess) |
| 301 |
}, |
| 302 |
} |
| 303 |
|
| 304 |
// rejected returns how many out of the n logs exporter were rejected based on |
| 305 |
// the provided non-nil err. |
| 306 |
func rejected(n int64, err error) int64 { |
| 307 |
ps := errPool.Get().(*internal.PartialSuccess) |
| 308 |
defer errPool.Put(ps) |
| 309 |
|
| 310 |
if errors.As(err, ps) { |
| 311 |
// Bound RejectedItems to [0, n]. This should not be needed, |
| 312 |
// but be defensive as this is from an external source. |
| 313 |
return min(max(ps.RejectedItems, 0), n) |
| 314 |
} |
| 315 |
// all logs exported |
| 316 |
return n |
| 317 |
} |
| 318 |
|
| 319 |
// parseEndpoint parses the host and port from target that has the form |
| 320 |
// "host[:port]", or it returns an error if the target is not parsable. |
| 321 |
// |
| 322 |
// If no port is specified, -1 is returned. |
| 323 |
// |
| 324 |
// If no host is specified, an empty string is returned. |
| 325 |
func parseTarget(endpoint string) (string, int, error) { |
| 326 |
if ip := parseIP(endpoint); ip != "" { |
| 327 |
return ip, -1, nil |
| 328 |
} |
| 329 |
|
| 330 |
// If there's no colon, there is no port (IPv6 with no port checked above). |
| 331 |
if !strings.Contains(endpoint, ":") { |
| 332 |
return endpoint, -1, nil |
| 333 |
} |
| 334 |
|
| 335 |
// Otherwise, parse as host:port. |
| 336 |
host, portStr, err := net.SplitHostPort(endpoint) |
| 337 |
if err != nil { |
| 338 |
return "", -1, fmt.Errorf("invalid host:port %q: %w", endpoint, err) |
| 339 |
} |
| 340 |
|
| 341 |
const base, bitSize = 10, 16 |
| 342 |
port16, err := strconv.ParseUint(portStr, base, bitSize) |
| 343 |
if err != nil { |
| 344 |
return "", -1, fmt.Errorf("invalid port %q: %w", portStr, err) |
| 345 |
} |
| 346 |
port := int(port16) |
| 347 |
|
| 348 |
return host, port, nil |
| 349 |
} |
| 350 |
|
| 351 |
// parseIP attempts to parse the entire target as an IP address. |
| 352 |
// It returns the normalized string form of the IP if successful, |
| 353 |
// or an empty string if parsing fails. |
| 354 |
func parseIP(ip string) string { |
| 355 |
// Strip leading and trailing brackets for IPv6 addresses. |
| 356 |
if len(ip) >= 2 && ip[0] == '[' && ip[len(ip)-1] == ']' { |
| 357 |
ip = ip[1 : len(ip)-1] |
| 358 |
} |
| 359 |
addr, err := netip.ParseAddr(ip) |
| 360 |
if err != nil { |
| 361 |
return "" |
| 362 |
} |
| 363 |
// Return the normalized string form of the IP. |
| 364 |
return addr.String() |
| 365 |
} |