| 1 |
// Code generated by gotmpl. DO NOT MODIFY. |
| 2 |
// source: internal/shared/otlp/otlplog/transform/log.go.tmpl |
| 3 |
|
| 4 |
// Copyright The OpenTelemetry Authors |
| 5 |
// SPDX-License-Identifier: Apache-2.0 |
| 6 |
|
| 7 |
// Package transform provides transformation functionality from the |
| 8 |
// sdk/log data-types into OTLP data-types. |
| 9 |
package transform // import "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp/internal/transform" |
| 10 |
|
| 11 |
import ( |
| 12 |
"time" |
| 13 |
|
| 14 |
cpb "go.opentelemetry.io/proto/otlp/common/v1" |
| 15 |
lpb "go.opentelemetry.io/proto/otlp/logs/v1" |
| 16 |
rpb "go.opentelemetry.io/proto/otlp/resource/v1" |
| 17 |
|
| 18 |
"go.opentelemetry.io/otel/attribute" |
| 19 |
api "go.opentelemetry.io/otel/log" |
| 20 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 21 |
"go.opentelemetry.io/otel/sdk/log" |
| 22 |
) |
| 23 |
|
| 24 |
// ResourceLogs returns an slice of OTLP ResourceLogs generated from records. |
| 25 |
func ResourceLogs(records []log.Record) []*lpb.ResourceLogs { |
| 26 |
if len(records) == 0 { |
| 27 |
return nil |
| 28 |
} |
| 29 |
|
| 30 |
resMap := make(map[attribute.Distinct]*lpb.ResourceLogs) |
| 31 |
|
| 32 |
type key struct { |
| 33 |
r attribute.Distinct |
| 34 |
is instrumentation.Scope |
| 35 |
} |
| 36 |
scopeMap := make(map[key]*lpb.ScopeLogs) |
| 37 |
|
| 38 |
var resources int |
| 39 |
for _, r := range records { |
| 40 |
res := r.Resource() |
| 41 |
rKey := res.Equivalent() |
| 42 |
scope := r.InstrumentationScope() |
| 43 |
k := key{ |
| 44 |
r: rKey, |
| 45 |
is: scope, |
| 46 |
} |
| 47 |
sl, iOk := scopeMap[k] |
| 48 |
if !iOk { |
| 49 |
sl = new(lpb.ScopeLogs) |
| 50 |
var emptyScope instrumentation.Scope |
| 51 |
if scope != emptyScope { |
| 52 |
sl.Scope = &cpb.InstrumentationScope{ |
| 53 |
Name: scope.Name, |
| 54 |
Version: scope.Version, |
| 55 |
Attributes: AttrIter(scope.Attributes.Iter()), |
| 56 |
} |
| 57 |
sl.SchemaUrl = scope.SchemaURL |
| 58 |
} |
| 59 |
scopeMap[k] = sl |
| 60 |
} |
| 61 |
|
| 62 |
sl.LogRecords = append(sl.LogRecords, LogRecord(r)) |
| 63 |
rl, rOk := resMap[rKey] |
| 64 |
if !rOk { |
| 65 |
resources++ |
| 66 |
rl = new(lpb.ResourceLogs) |
| 67 |
if res.Len() > 0 { |
| 68 |
rl.Resource = &rpb.Resource{ |
| 69 |
Attributes: AttrIter(res.Iter()), |
| 70 |
} |
| 71 |
} |
| 72 |
rl.SchemaUrl = res.SchemaURL() |
| 73 |
resMap[rKey] = rl |
| 74 |
} |
| 75 |
if !iOk { |
| 76 |
rl.ScopeLogs = append(rl.ScopeLogs, sl) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// Transform the categorized map into a slice |
| 81 |
resLogs := make([]*lpb.ResourceLogs, 0, resources) |
| 82 |
for _, rl := range resMap { |
| 83 |
resLogs = append(resLogs, rl) |
| 84 |
} |
| 85 |
|
| 86 |
return resLogs |
| 87 |
} |
| 88 |
|
| 89 |
// LogRecord returns an OTLP LogRecord generated from record. |
| 90 |
func LogRecord(record log.Record) *lpb.LogRecord { |
| 91 |
r := &lpb.LogRecord{ |
| 92 |
TimeUnixNano: timeUnixNano(record.Timestamp()), |
| 93 |
ObservedTimeUnixNano: timeUnixNano(record.ObservedTimestamp()), |
| 94 |
EventName: record.EventName(), |
| 95 |
SeverityNumber: SeverityNumber(record.Severity()), |
| 96 |
SeverityText: record.SeverityText(), |
| 97 |
Body: LogAttrValue(record.Body()), |
| 98 |
Attributes: make([]*cpb.KeyValue, 0, record.AttributesLen()), |
| 99 |
Flags: uint32(record.TraceFlags()), |
| 100 |
// TODO: DroppedAttributesCount: /* ... */, |
| 101 |
} |
| 102 |
record.WalkAttributes(func(kv api.KeyValue) bool { |
| 103 |
r.Attributes = append(r.Attributes, LogAttr(kv)) |
| 104 |
return true |
| 105 |
}) |
| 106 |
if tID := record.TraceID(); tID.IsValid() { |
| 107 |
r.TraceId = tID[:] |
| 108 |
} |
| 109 |
if sID := record.SpanID(); sID.IsValid() { |
| 110 |
r.SpanId = sID[:] |
| 111 |
} |
| 112 |
return r |
| 113 |
} |
| 114 |
|
| 115 |
// timeUnixNano returns t as a Unix time, the number of nanoseconds elapsed |
| 116 |
// since January 1, 1970 UTC as uint64. The result is undefined if the Unix |
| 117 |
// time in nanoseconds cannot be represented by an int64 (a date before the |
| 118 |
// year 1678 or after 2262). timeUnixNano on the zero Time returns 0. The |
| 119 |
// result does not depend on the location associated with t. |
| 120 |
func timeUnixNano(t time.Time) uint64 { |
| 121 |
nano := t.UnixNano() |
| 122 |
if nano < 0 { |
| 123 |
return 0 |
| 124 |
} |
| 125 |
return uint64(nano) // nolint:gosec // Overflow checked. |
| 126 |
} |
| 127 |
|
| 128 |
// AttrIter transforms an [attribute.Iterator] into OTLP key-values. |
| 129 |
func AttrIter(iter attribute.Iterator) []*cpb.KeyValue { |
| 130 |
l := iter.Len() |
| 131 |
if l == 0 { |
| 132 |
return nil |
| 133 |
} |
| 134 |
|
| 135 |
out := make([]*cpb.KeyValue, 0, l) |
| 136 |
for iter.Next() { |
| 137 |
out = append(out, Attr(iter.Attribute())) |
| 138 |
} |
| 139 |
return out |
| 140 |
} |
| 141 |
|
| 142 |
// Attrs transforms a slice of [attribute.KeyValue] into OTLP key-values. |
| 143 |
func Attrs(attrs []attribute.KeyValue) []*cpb.KeyValue { |
| 144 |
if len(attrs) == 0 { |
| 145 |
return nil |
| 146 |
} |
| 147 |
|
| 148 |
out := make([]*cpb.KeyValue, 0, len(attrs)) |
| 149 |
for _, kv := range attrs { |
| 150 |
out = append(out, Attr(kv)) |
| 151 |
} |
| 152 |
return out |
| 153 |
} |
| 154 |
|
| 155 |
// Attr transforms an [attribute.KeyValue] into an OTLP key-value. |
| 156 |
func Attr(kv attribute.KeyValue) *cpb.KeyValue { |
| 157 |
return &cpb.KeyValue{Key: string(kv.Key), Value: AttrValue(kv.Value)} |
| 158 |
} |
| 159 |
|
| 160 |
// AttrValue transforms an [attribute.Value] into an OTLP AnyValue. |
| 161 |
func AttrValue(v attribute.Value) *cpb.AnyValue { |
| 162 |
av := new(cpb.AnyValue) |
| 163 |
switch v.Type() { |
| 164 |
case attribute.BOOL: |
| 165 |
av.Value = &cpb.AnyValue_BoolValue{ |
| 166 |
BoolValue: v.AsBool(), |
| 167 |
} |
| 168 |
case attribute.BOOLSLICE: |
| 169 |
av.Value = &cpb.AnyValue_ArrayValue{ |
| 170 |
ArrayValue: &cpb.ArrayValue{ |
| 171 |
Values: boolSliceValues(v.AsBoolSlice()), |
| 172 |
}, |
| 173 |
} |
| 174 |
case attribute.INT64: |
| 175 |
av.Value = &cpb.AnyValue_IntValue{ |
| 176 |
IntValue: v.AsInt64(), |
| 177 |
} |
| 178 |
case attribute.INT64SLICE: |
| 179 |
av.Value = &cpb.AnyValue_ArrayValue{ |
| 180 |
ArrayValue: &cpb.ArrayValue{ |
| 181 |
Values: int64SliceValues(v.AsInt64Slice()), |
| 182 |
}, |
| 183 |
} |
| 184 |
case attribute.FLOAT64: |
| 185 |
av.Value = &cpb.AnyValue_DoubleValue{ |
| 186 |
DoubleValue: v.AsFloat64(), |
| 187 |
} |
| 188 |
case attribute.FLOAT64SLICE: |
| 189 |
av.Value = &cpb.AnyValue_ArrayValue{ |
| 190 |
ArrayValue: &cpb.ArrayValue{ |
| 191 |
Values: float64SliceValues(v.AsFloat64Slice()), |
| 192 |
}, |
| 193 |
} |
| 194 |
case attribute.STRING: |
| 195 |
av.Value = &cpb.AnyValue_StringValue{ |
| 196 |
StringValue: v.AsString(), |
| 197 |
} |
| 198 |
case attribute.STRINGSLICE: |
| 199 |
av.Value = &cpb.AnyValue_ArrayValue{ |
| 200 |
ArrayValue: &cpb.ArrayValue{ |
| 201 |
Values: stringSliceValues(v.AsStringSlice()), |
| 202 |
}, |
| 203 |
} |
| 204 |
case attribute.EMPTY: |
| 205 |
default: |
| 206 |
av.Value = &cpb.AnyValue_StringValue{ |
| 207 |
StringValue: "INVALID", |
| 208 |
} |
| 209 |
} |
| 210 |
return av |
| 211 |
} |
| 212 |
|
| 213 |
func boolSliceValues(vals []bool) []*cpb.AnyValue { |
| 214 |
converted := make([]*cpb.AnyValue, len(vals)) |
| 215 |
for i, v := range vals { |
| 216 |
converted[i] = &cpb.AnyValue{ |
| 217 |
Value: &cpb.AnyValue_BoolValue{ |
| 218 |
BoolValue: v, |
| 219 |
}, |
| 220 |
} |
| 221 |
} |
| 222 |
return converted |
| 223 |
} |
| 224 |
|
| 225 |
func int64SliceValues(vals []int64) []*cpb.AnyValue { |
| 226 |
converted := make([]*cpb.AnyValue, len(vals)) |
| 227 |
for i, v := range vals { |
| 228 |
converted[i] = &cpb.AnyValue{ |
| 229 |
Value: &cpb.AnyValue_IntValue{ |
| 230 |
IntValue: v, |
| 231 |
}, |
| 232 |
} |
| 233 |
} |
| 234 |
return converted |
| 235 |
} |
| 236 |
|
| 237 |
func float64SliceValues(vals []float64) []*cpb.AnyValue { |
| 238 |
converted := make([]*cpb.AnyValue, len(vals)) |
| 239 |
for i, v := range vals { |
| 240 |
converted[i] = &cpb.AnyValue{ |
| 241 |
Value: &cpb.AnyValue_DoubleValue{ |
| 242 |
DoubleValue: v, |
| 243 |
}, |
| 244 |
} |
| 245 |
} |
| 246 |
return converted |
| 247 |
} |
| 248 |
|
| 249 |
func stringSliceValues(vals []string) []*cpb.AnyValue { |
| 250 |
converted := make([]*cpb.AnyValue, len(vals)) |
| 251 |
for i, v := range vals { |
| 252 |
converted[i] = &cpb.AnyValue{ |
| 253 |
Value: &cpb.AnyValue_StringValue{ |
| 254 |
StringValue: v, |
| 255 |
}, |
| 256 |
} |
| 257 |
} |
| 258 |
return converted |
| 259 |
} |
| 260 |
|
| 261 |
// LogAttrs transforms a slice of [api.KeyValue] into OTLP key-values. |
| 262 |
func LogAttrs(attrs []api.KeyValue) []*cpb.KeyValue { |
| 263 |
if len(attrs) == 0 { |
| 264 |
return nil |
| 265 |
} |
| 266 |
|
| 267 |
out := make([]*cpb.KeyValue, 0, len(attrs)) |
| 268 |
for _, kv := range attrs { |
| 269 |
out = append(out, LogAttr(kv)) |
| 270 |
} |
| 271 |
return out |
| 272 |
} |
| 273 |
|
| 274 |
// LogAttr transforms an [api.KeyValue] into an OTLP key-value. |
| 275 |
func LogAttr(attr api.KeyValue) *cpb.KeyValue { |
| 276 |
return &cpb.KeyValue{ |
| 277 |
Key: attr.Key, |
| 278 |
Value: LogAttrValue(attr.Value), |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
// LogAttrValues transforms a slice of [api.Value] into an OTLP []AnyValue. |
| 283 |
func LogAttrValues(vals []api.Value) []*cpb.AnyValue { |
| 284 |
if len(vals) == 0 { |
| 285 |
return nil |
| 286 |
} |
| 287 |
|
| 288 |
out := make([]*cpb.AnyValue, 0, len(vals)) |
| 289 |
for _, v := range vals { |
| 290 |
out = append(out, LogAttrValue(v)) |
| 291 |
} |
| 292 |
return out |
| 293 |
} |
| 294 |
|
| 295 |
// LogAttrValue transforms an [api.Value] into an OTLP AnyValue. |
| 296 |
func LogAttrValue(v api.Value) *cpb.AnyValue { |
| 297 |
av := new(cpb.AnyValue) |
| 298 |
switch v.Kind() { |
| 299 |
case api.KindBool: |
| 300 |
av.Value = &cpb.AnyValue_BoolValue{ |
| 301 |
BoolValue: v.AsBool(), |
| 302 |
} |
| 303 |
case api.KindInt64: |
| 304 |
av.Value = &cpb.AnyValue_IntValue{ |
| 305 |
IntValue: v.AsInt64(), |
| 306 |
} |
| 307 |
case api.KindFloat64: |
| 308 |
av.Value = &cpb.AnyValue_DoubleValue{ |
| 309 |
DoubleValue: v.AsFloat64(), |
| 310 |
} |
| 311 |
case api.KindString: |
| 312 |
av.Value = &cpb.AnyValue_StringValue{ |
| 313 |
StringValue: v.AsString(), |
| 314 |
} |
| 315 |
case api.KindBytes: |
| 316 |
av.Value = &cpb.AnyValue_BytesValue{ |
| 317 |
BytesValue: v.AsBytes(), |
| 318 |
} |
| 319 |
case api.KindSlice: |
| 320 |
av.Value = &cpb.AnyValue_ArrayValue{ |
| 321 |
ArrayValue: &cpb.ArrayValue{ |
| 322 |
Values: LogAttrValues(v.AsSlice()), |
| 323 |
}, |
| 324 |
} |
| 325 |
case api.KindMap: |
| 326 |
av.Value = &cpb.AnyValue_KvlistValue{ |
| 327 |
KvlistValue: &cpb.KeyValueList{ |
| 328 |
Values: LogAttrs(v.AsMap()), |
| 329 |
}, |
| 330 |
} |
| 331 |
case api.KindEmpty: |
| 332 |
default: |
| 333 |
av.Value = &cpb.AnyValue_StringValue{ |
| 334 |
StringValue: "INVALID", |
| 335 |
} |
| 336 |
} |
| 337 |
return av |
| 338 |
} |
| 339 |
|
| 340 |
// SeverityNumber transforms a [log.Severity] into an OTLP SeverityNumber. |
| 341 |
func SeverityNumber(s api.Severity) lpb.SeverityNumber { |
| 342 |
switch s { |
| 343 |
case api.SeverityTrace: |
| 344 |
return lpb.SeverityNumber_SEVERITY_NUMBER_TRACE |
| 345 |
case api.SeverityTrace2: |
| 346 |
return lpb.SeverityNumber_SEVERITY_NUMBER_TRACE2 |
| 347 |
case api.SeverityTrace3: |
| 348 |
return lpb.SeverityNumber_SEVERITY_NUMBER_TRACE3 |
| 349 |
case api.SeverityTrace4: |
| 350 |
return lpb.SeverityNumber_SEVERITY_NUMBER_TRACE4 |
| 351 |
case api.SeverityDebug: |
| 352 |
return lpb.SeverityNumber_SEVERITY_NUMBER_DEBUG |
| 353 |
case api.SeverityDebug2: |
| 354 |
return lpb.SeverityNumber_SEVERITY_NUMBER_DEBUG2 |
| 355 |
case api.SeverityDebug3: |
| 356 |
return lpb.SeverityNumber_SEVERITY_NUMBER_DEBUG3 |
| 357 |
case api.SeverityDebug4: |
| 358 |
return lpb.SeverityNumber_SEVERITY_NUMBER_DEBUG4 |
| 359 |
case api.SeverityInfo: |
| 360 |
return lpb.SeverityNumber_SEVERITY_NUMBER_INFO |
| 361 |
case api.SeverityInfo2: |
| 362 |
return lpb.SeverityNumber_SEVERITY_NUMBER_INFO2 |
| 363 |
case api.SeverityInfo3: |
| 364 |
return lpb.SeverityNumber_SEVERITY_NUMBER_INFO3 |
| 365 |
case api.SeverityInfo4: |
| 366 |
return lpb.SeverityNumber_SEVERITY_NUMBER_INFO4 |
| 367 |
case api.SeverityWarn: |
| 368 |
return lpb.SeverityNumber_SEVERITY_NUMBER_WARN |
| 369 |
case api.SeverityWarn2: |
| 370 |
return lpb.SeverityNumber_SEVERITY_NUMBER_WARN2 |
| 371 |
case api.SeverityWarn3: |
| 372 |
return lpb.SeverityNumber_SEVERITY_NUMBER_WARN3 |
| 373 |
case api.SeverityWarn4: |
| 374 |
return lpb.SeverityNumber_SEVERITY_NUMBER_WARN4 |
| 375 |
case api.SeverityError: |
| 376 |
return lpb.SeverityNumber_SEVERITY_NUMBER_ERROR |
| 377 |
case api.SeverityError2: |
| 378 |
return lpb.SeverityNumber_SEVERITY_NUMBER_ERROR2 |
| 379 |
case api.SeverityError3: |
| 380 |
return lpb.SeverityNumber_SEVERITY_NUMBER_ERROR3 |
| 381 |
case api.SeverityError4: |
| 382 |
return lpb.SeverityNumber_SEVERITY_NUMBER_ERROR4 |
| 383 |
case api.SeverityFatal: |
| 384 |
return lpb.SeverityNumber_SEVERITY_NUMBER_FATAL |
| 385 |
case api.SeverityFatal2: |
| 386 |
return lpb.SeverityNumber_SEVERITY_NUMBER_FATAL2 |
| 387 |
case api.SeverityFatal3: |
| 388 |
return lpb.SeverityNumber_SEVERITY_NUMBER_FATAL3 |
| 389 |
case api.SeverityFatal4: |
| 390 |
return lpb.SeverityNumber_SEVERITY_NUMBER_FATAL4 |
| 391 |
} |
| 392 |
return lpb.SeverityNumber_SEVERITY_NUMBER_UNSPECIFIED |
| 393 |
} |