| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otlptext |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"fmt" |
| 9 |
"math" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 13 |
"go.opentelemetry.io/collector/pdata/pmetric" |
| 14 |
"go.opentelemetry.io/collector/pdata/pprofile" |
| 15 |
"go.opentelemetry.io/collector/pdata/ptrace" |
| 16 |
"go.opentelemetry.io/collector/pdata/xpdata/entity" |
| 17 |
) |
| 18 |
|
| 19 |
type dataBuffer struct { |
| 20 |
buf bytes.Buffer |
| 21 |
} |
| 22 |
|
| 23 |
func (b *dataBuffer) logEntry(format string, a ...any) { |
| 24 |
b.buf.WriteString(fmt.Sprintf(format, a...)) |
| 25 |
b.buf.WriteString("\n") |
| 26 |
} |
| 27 |
|
| 28 |
func (b *dataBuffer) logAttr(attr string, value any) { |
| 29 |
b.logEntry(" %-15s: %s", attr, value) |
| 30 |
} |
| 31 |
|
| 32 |
func (b *dataBuffer) logAttributes(header string, m pcommon.Map) { |
| 33 |
if m.Len() == 0 { |
| 34 |
return |
| 35 |
} |
| 36 |
|
| 37 |
b.logEntry("%s:", header) |
| 38 |
attrPrefix := " ->" |
| 39 |
|
| 40 |
|
| 41 |
headerParts := strings.Split(header, "->") |
| 42 |
if len(headerParts) > 1 { |
| 43 |
attrPrefix = headerParts[0] + attrPrefix |
| 44 |
} |
| 45 |
|
| 46 |
for k, v := range m.All() { |
| 47 |
b.logEntry("%s %s: %s", attrPrefix, k, valueToString(v)) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
func (b *dataBuffer) logEntityRefs(resource pcommon.Resource) { |
| 52 |
entityRefs := entity.ResourceEntityRefs(resource) |
| 53 |
if entityRefs.Len() == 0 { |
| 54 |
return |
| 55 |
} |
| 56 |
|
| 57 |
b.logEntry("Resource entity refs:") |
| 58 |
for i := 0; i < entityRefs.Len(); i++ { |
| 59 |
entityRef := entityRefs.At(i) |
| 60 |
b.logEntry(" -> Entity ref #%d:", i) |
| 61 |
b.logEntry(" -> Type: %s", entityRef.Type()) |
| 62 |
if entityRef.SchemaUrl() != "" { |
| 63 |
b.logEntry(" -> Schema URL: %s", entityRef.SchemaUrl()) |
| 64 |
} |
| 65 |
|
| 66 |
idKeys := entityRef.IdKeys() |
| 67 |
if idKeys.Len() > 0 { |
| 68 |
b.logEntry(" -> ID keys:") |
| 69 |
for j := 0; j < idKeys.Len(); j++ { |
| 70 |
b.logEntry(" -> %s", idKeys.At(j)) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
descKeys := entityRef.DescriptionKeys() |
| 75 |
if descKeys.Len() > 0 { |
| 76 |
b.logEntry(" -> Description keys:") |
| 77 |
for j := 0; j < descKeys.Len(); j++ { |
| 78 |
b.logEntry(" -> %s", descKeys.At(j)) |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func (b *dataBuffer) logAttributesWithIndentation(header string, m pcommon.Map, indentVal int) { |
| 85 |
if m.Len() == 0 { |
| 86 |
return |
| 87 |
} |
| 88 |
|
| 89 |
indent := strings.Repeat(" ", indentVal) |
| 90 |
|
| 91 |
b.logEntry("%s%s:", indent, header) |
| 92 |
attrPrefix := indent + " ->" |
| 93 |
|
| 94 |
|
| 95 |
headerParts := strings.Split(header, "->") |
| 96 |
if len(headerParts) > 1 { |
| 97 |
attrPrefix = headerParts[0] + attrPrefix |
| 98 |
} |
| 99 |
|
| 100 |
for k, v := range m.All() { |
| 101 |
b.logEntry("%s %s: %s", attrPrefix, k, valueToString(v)) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func (b *dataBuffer) logInstrumentationScope(il pcommon.InstrumentationScope) { |
| 106 |
b.logEntry( |
| 107 |
"InstrumentationScope %s %s", |
| 108 |
il.Name(), |
| 109 |
il.Version()) |
| 110 |
b.logAttributes("InstrumentationScope attributes", il.Attributes()) |
| 111 |
} |
| 112 |
|
| 113 |
func (b *dataBuffer) logMetricDescriptor(md pmetric.Metric) { |
| 114 |
b.logEntry("Descriptor:") |
| 115 |
b.logEntry(" -> Name: %s", md.Name()) |
| 116 |
b.logEntry(" -> Description: %s", md.Description()) |
| 117 |
b.logEntry(" -> Unit: %s", md.Unit()) |
| 118 |
b.logEntry(" -> DataType: %s", md.Type().String()) |
| 119 |
} |
| 120 |
|
| 121 |
func (b *dataBuffer) logMetricDataPoints(m pmetric.Metric) { |
| 122 |
switch m.Type() { |
| 123 |
case pmetric.MetricTypeEmpty: |
| 124 |
return |
| 125 |
case pmetric.MetricTypeGauge: |
| 126 |
b.logNumberDataPoints(m.Gauge().DataPoints()) |
| 127 |
case pmetric.MetricTypeSum: |
| 128 |
data := m.Sum() |
| 129 |
b.logEntry(" -> IsMonotonic: %t", data.IsMonotonic()) |
| 130 |
b.logEntry(" -> AggregationTemporality: %s", data.AggregationTemporality().String()) |
| 131 |
b.logNumberDataPoints(data.DataPoints()) |
| 132 |
case pmetric.MetricTypeHistogram: |
| 133 |
data := m.Histogram() |
| 134 |
b.logEntry(" -> AggregationTemporality: %s", data.AggregationTemporality().String()) |
| 135 |
b.logHistogramDataPoints(data.DataPoints()) |
| 136 |
case pmetric.MetricTypeExponentialHistogram: |
| 137 |
data := m.ExponentialHistogram() |
| 138 |
b.logEntry(" -> AggregationTemporality: %s", data.AggregationTemporality().String()) |
| 139 |
b.logExponentialHistogramDataPoints(data.DataPoints()) |
| 140 |
case pmetric.MetricTypeSummary: |
| 141 |
data := m.Summary() |
| 142 |
b.logDoubleSummaryDataPoints(data.DataPoints()) |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func (b *dataBuffer) logNumberDataPoints(ps pmetric.NumberDataPointSlice) { |
| 147 |
for i := 0; i < ps.Len(); i++ { |
| 148 |
p := ps.At(i) |
| 149 |
b.logEntry("NumberDataPoints #%d", i) |
| 150 |
b.logDataPointAttributes(p.Attributes()) |
| 151 |
|
| 152 |
b.logEntry("StartTimestamp: %s", p.StartTimestamp()) |
| 153 |
b.logEntry("Timestamp: %s", p.Timestamp()) |
| 154 |
switch p.ValueType() { |
| 155 |
case pmetric.NumberDataPointValueTypeInt: |
| 156 |
b.logEntry("Value: %d", p.IntValue()) |
| 157 |
case pmetric.NumberDataPointValueTypeDouble: |
| 158 |
b.logEntry("Value: %f", p.DoubleValue()) |
| 159 |
} |
| 160 |
|
| 161 |
b.logExemplars("Exemplars", p.Exemplars()) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
func (b *dataBuffer) logHistogramDataPoints(ps pmetric.HistogramDataPointSlice) { |
| 166 |
for i := 0; i < ps.Len(); i++ { |
| 167 |
p := ps.At(i) |
| 168 |
b.logEntry("HistogramDataPoints #%d", i) |
| 169 |
b.logDataPointAttributes(p.Attributes()) |
| 170 |
|
| 171 |
b.logEntry("StartTimestamp: %s", p.StartTimestamp()) |
| 172 |
b.logEntry("Timestamp: %s", p.Timestamp()) |
| 173 |
b.logEntry("Count: %d", p.Count()) |
| 174 |
|
| 175 |
if p.HasSum() { |
| 176 |
b.logEntry("Sum: %f", p.Sum()) |
| 177 |
} |
| 178 |
|
| 179 |
if p.HasMin() { |
| 180 |
b.logEntry("Min: %f", p.Min()) |
| 181 |
} |
| 182 |
|
| 183 |
if p.HasMax() { |
| 184 |
b.logEntry("Max: %f", p.Max()) |
| 185 |
} |
| 186 |
|
| 187 |
for i := 0; i < p.ExplicitBounds().Len(); i++ { |
| 188 |
b.logEntry("ExplicitBounds #%d: %f", i, p.ExplicitBounds().At(i)) |
| 189 |
} |
| 190 |
|
| 191 |
for j := 0; j < p.BucketCounts().Len(); j++ { |
| 192 |
b.logEntry("Buckets #%d, Count: %d", j, p.BucketCounts().At(j)) |
| 193 |
} |
| 194 |
|
| 195 |
b.logExemplars("Exemplars", p.Exemplars()) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func (b *dataBuffer) logExponentialHistogramDataPoints(ps pmetric.ExponentialHistogramDataPointSlice) { |
| 200 |
for i := 0; i < ps.Len(); i++ { |
| 201 |
p := ps.At(i) |
| 202 |
b.logEntry("ExponentialHistogramDataPoints #%d", i) |
| 203 |
b.logDataPointAttributes(p.Attributes()) |
| 204 |
|
| 205 |
b.logEntry("StartTimestamp: %s", p.StartTimestamp()) |
| 206 |
b.logEntry("Timestamp: %s", p.Timestamp()) |
| 207 |
b.logEntry("Count: %d", p.Count()) |
| 208 |
|
| 209 |
if p.HasSum() { |
| 210 |
b.logEntry("Sum: %f", p.Sum()) |
| 211 |
} |
| 212 |
|
| 213 |
if p.HasMin() { |
| 214 |
b.logEntry("Min: %f", p.Min()) |
| 215 |
} |
| 216 |
|
| 217 |
if p.HasMax() { |
| 218 |
b.logEntry("Max: %f", p.Max()) |
| 219 |
} |
| 220 |
|
| 221 |
scale := int(p.Scale()) |
| 222 |
factor := math.Ldexp(math.Ln2, -scale) |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
negB := p.Negative().BucketCounts() |
| 233 |
posB := p.Positive().BucketCounts() |
| 234 |
|
| 235 |
for i := 0; i < negB.Len(); i++ { |
| 236 |
pos := negB.Len() - i - 1 |
| 237 |
index := float64(p.Negative().Offset()) + float64(pos) |
| 238 |
lower := math.Exp(index * factor) |
| 239 |
upper := math.Exp((index + 1) * factor) |
| 240 |
b.logEntry("Bucket [%f, %f), Count: %d", -upper, -lower, negB.At(pos)) |
| 241 |
} |
| 242 |
|
| 243 |
if p.ZeroCount() != 0 { |
| 244 |
b.logEntry("Bucket [0, 0], Count: %d", p.ZeroCount()) |
| 245 |
} |
| 246 |
|
| 247 |
for pos := 0; pos < posB.Len(); pos++ { |
| 248 |
index := float64(p.Positive().Offset()) + float64(pos) |
| 249 |
lower := math.Exp(index * factor) |
| 250 |
upper := math.Exp((index + 1) * factor) |
| 251 |
b.logEntry("Bucket (%f, %f], Count: %d", lower, upper, posB.At(pos)) |
| 252 |
} |
| 253 |
|
| 254 |
b.logExemplars("Exemplars", p.Exemplars()) |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
func (b *dataBuffer) logDoubleSummaryDataPoints(ps pmetric.SummaryDataPointSlice) { |
| 259 |
for i := 0; i < ps.Len(); i++ { |
| 260 |
p := ps.At(i) |
| 261 |
b.logEntry("SummaryDataPoints #%d", i) |
| 262 |
b.logDataPointAttributes(p.Attributes()) |
| 263 |
|
| 264 |
b.logEntry("StartTimestamp: %s", p.StartTimestamp()) |
| 265 |
b.logEntry("Timestamp: %s", p.Timestamp()) |
| 266 |
b.logEntry("Count: %d", p.Count()) |
| 267 |
b.logEntry("Sum: %f", p.Sum()) |
| 268 |
|
| 269 |
quantiles := p.QuantileValues() |
| 270 |
for i := 0; i < quantiles.Len(); i++ { |
| 271 |
quantile := quantiles.At(i) |
| 272 |
b.logEntry("QuantileValue #%d: Quantile %f, Value %f", i, quantile.Quantile(), quantile.Value()) |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
func (b *dataBuffer) logDataPointAttributes(attributes pcommon.Map) { |
| 278 |
b.logAttributes("Data point attributes", attributes) |
| 279 |
} |
| 280 |
|
| 281 |
func (b *dataBuffer) logEvents(description string, se ptrace.SpanEventSlice) { |
| 282 |
if se.Len() == 0 { |
| 283 |
return |
| 284 |
} |
| 285 |
|
| 286 |
b.logEntry("%s:", description) |
| 287 |
for i := 0; i < se.Len(); i++ { |
| 288 |
e := se.At(i) |
| 289 |
b.logEntry("SpanEvent #%d", i) |
| 290 |
b.logEntry(" -> Name: %s", e.Name()) |
| 291 |
b.logEntry(" -> Timestamp: %s", e.Timestamp()) |
| 292 |
b.logEntry(" -> DroppedAttributesCount: %d", e.DroppedAttributesCount()) |
| 293 |
b.logAttributes(" -> Attributes:", e.Attributes()) |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
func (b *dataBuffer) logLinks(description string, sl ptrace.SpanLinkSlice) { |
| 298 |
if sl.Len() == 0 { |
| 299 |
return |
| 300 |
} |
| 301 |
|
| 302 |
b.logEntry("%s:", description) |
| 303 |
|
| 304 |
for i := 0; i < sl.Len(); i++ { |
| 305 |
l := sl.At(i) |
| 306 |
b.logEntry("SpanLink #%d", i) |
| 307 |
b.logEntry(" -> Trace ID: %s", l.TraceID()) |
| 308 |
b.logEntry(" -> ID: %s", l.SpanID()) |
| 309 |
b.logEntry(" -> TraceState: %s", l.TraceState().AsRaw()) |
| 310 |
b.logEntry(" -> DroppedAttributesCount: %d", l.DroppedAttributesCount()) |
| 311 |
b.logAttributes(" -> Attributes:", l.Attributes()) |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
func (b *dataBuffer) logExemplars(description string, se pmetric.ExemplarSlice) { |
| 316 |
if se.Len() == 0 { |
| 317 |
return |
| 318 |
} |
| 319 |
|
| 320 |
b.logEntry("%s:", description) |
| 321 |
|
| 322 |
for i := 0; i < se.Len(); i++ { |
| 323 |
e := se.At(i) |
| 324 |
b.logEntry("Exemplar #%d", i) |
| 325 |
b.logEntry(" -> Trace ID: %s", e.TraceID()) |
| 326 |
b.logEntry(" -> Span ID: %s", e.SpanID()) |
| 327 |
b.logEntry(" -> Timestamp: %s", e.Timestamp()) |
| 328 |
switch e.ValueType() { |
| 329 |
case pmetric.ExemplarValueTypeInt: |
| 330 |
b.logEntry(" -> Value: %d", e.IntValue()) |
| 331 |
case pmetric.ExemplarValueTypeDouble: |
| 332 |
b.logEntry(" -> Value: %f", e.DoubleValue()) |
| 333 |
} |
| 334 |
b.logAttributes(" -> FilteredAttributes", e.FilteredAttributes()) |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
func (b *dataBuffer) logProfileSamples(ss pprofile.SampleSlice, dic pprofile.ProfilesDictionary) { |
| 339 |
if ss.Len() == 0 { |
| 340 |
return |
| 341 |
} |
| 342 |
|
| 343 |
for i := 0; i < ss.Len(); i++ { |
| 344 |
b.logEntry(" Sample #%d", i) |
| 345 |
sample := ss.At(i) |
| 346 |
|
| 347 |
b.logEntry(" Values: %d", sample.Values().AsRaw()) |
| 348 |
|
| 349 |
if lai := sample.AttributeIndices().Len(); lai > 0 { |
| 350 |
b.logEntry(" Attributes:") |
| 351 |
for j := range lai { |
| 352 |
attr := dic.AttributeTable().At(int(sample.AttributeIndices().At(j))) |
| 353 |
b.logEntry(" -> %s: %s", dic.StringTable().At(int(attr.KeyStrindex())), attr.Value().AsRaw()) |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
func (b *dataBuffer) logProfileMappings(ms pprofile.MappingSlice) { |
| 360 |
if ms.Len() == 0 { |
| 361 |
return |
| 362 |
} |
| 363 |
|
| 364 |
for i := 0; i < ms.Len(); i++ { |
| 365 |
b.logEntry("Mapping #%d", i) |
| 366 |
mapping := ms.At(i) |
| 367 |
|
| 368 |
b.logEntry(" Memory start: %d", mapping.MemoryStart()) |
| 369 |
b.logEntry(" Memory limit: %d", mapping.MemoryLimit()) |
| 370 |
b.logEntry(" File offset: %d", mapping.FileOffset()) |
| 371 |
b.logEntry(" File name: %d", mapping.FilenameStrindex()) |
| 372 |
b.logEntry(" Attributes: %d", mapping.AttributeIndices().AsRaw()) |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
func (b *dataBuffer) logProfileLocations(ls pprofile.LocationSlice) { |
| 377 |
if ls.Len() == 0 { |
| 378 |
return |
| 379 |
} |
| 380 |
|
| 381 |
for i := 0; i < ls.Len(); i++ { |
| 382 |
b.logEntry("Location #%d", i) |
| 383 |
location := ls.At(i) |
| 384 |
|
| 385 |
b.logEntry(" Mapping index: %d", location.MappingIndex()) |
| 386 |
b.logEntry(" Address: %d", location.Address()) |
| 387 |
if ll := location.Lines().Len(); ll > 0 { |
| 388 |
for j := range ll { |
| 389 |
b.logEntry(" Line #%d", j) |
| 390 |
line := location.Lines().At(j) |
| 391 |
b.logEntry(" Function index: %d", line.FunctionIndex()) |
| 392 |
b.logEntry(" Line: %d", line.Line()) |
| 393 |
b.logEntry(" Column: %d", line.Column()) |
| 394 |
} |
| 395 |
} |
| 396 |
b.logEntry(" Attributes: %d", location.AttributeIndices().AsRaw()) |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
func (b *dataBuffer) logProfileFunctions(fs pprofile.FunctionSlice) { |
| 401 |
if fs.Len() == 0 { |
| 402 |
return |
| 403 |
} |
| 404 |
|
| 405 |
for i := 0; i < fs.Len(); i++ { |
| 406 |
b.logEntry("Function #%d", i) |
| 407 |
function := fs.At(i) |
| 408 |
|
| 409 |
b.logEntry(" Name: %d", function.NameStrindex()) |
| 410 |
b.logEntry(" System name: %d", function.SystemNameStrindex()) |
| 411 |
b.logEntry(" Filename: %d", function.FilenameStrindex()) |
| 412 |
b.logEntry(" Start line: %d", function.StartLine()) |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
func (b *dataBuffer) logStringTable(ss pcommon.StringSlice) { |
| 417 |
if ss.Len() == 0 { |
| 418 |
return |
| 419 |
} |
| 420 |
|
| 421 |
b.logEntry("String table:") |
| 422 |
for i := 0; i < ss.Len(); i++ { |
| 423 |
b.logEntry(" %s", ss.At(i)) |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
func keyValueAndUnitsToMap(aus pprofile.KeyValueAndUnitSlice) pcommon.Map { |
| 428 |
m := pcommon.NewMap() |
| 429 |
for i := 0; i < aus.Len(); i++ { |
| 430 |
au := aus.At(i) |
| 431 |
m.PutInt("Key", int64(au.KeyStrindex())) |
| 432 |
m.PutStr("Value", au.Value().AsString()) |
| 433 |
m.PutInt("unit", int64(au.UnitStrindex())) |
| 434 |
} |
| 435 |
return m |
| 436 |
} |
| 437 |
|
| 438 |
func linkTableToMap(ls pprofile.LinkSlice) pcommon.Map { |
| 439 |
m := pcommon.NewMap() |
| 440 |
for i := 0; i < ls.Len(); i++ { |
| 441 |
l := ls.At(i) |
| 442 |
m.PutStr("Trace ID", l.TraceID().String()) |
| 443 |
m.PutStr("Span ID", l.SpanID().String()) |
| 444 |
} |
| 445 |
return m |
| 446 |
} |
| 447 |
|
| 448 |
func valueToString(v pcommon.Value) string { |
| 449 |
return fmt.Sprintf("%s(%s)", v.Type().String(), v.AsString()) |
| 450 |
} |