From e0f45ff48140fa40088124dda3347d052d0c444b Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Fri, 11 Nov 2022 05:50:12 -0800 Subject: [PATCH] [chore] remove what looks to be debugging code from instanaexporter (#16258) Signed-off-by: Bogdan Drutu Signed-off-by: Bogdan Drutu --- .../internal/otlptext/databuffer.go | 328 ------------------ .../internal/otlptext/traces.go | 67 ---- 2 files changed, 395 deletions(-) delete mode 100644 exporter/instanaexporter/internal/otlptext/databuffer.go delete mode 100644 exporter/instanaexporter/internal/otlptext/traces.go diff --git a/exporter/instanaexporter/internal/otlptext/databuffer.go b/exporter/instanaexporter/internal/otlptext/databuffer.go deleted file mode 100644 index 3c8202fb5b4ad..0000000000000 --- a/exporter/instanaexporter/internal/otlptext/databuffer.go +++ /dev/null @@ -1,328 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// This file is copied from "go.opentelemetry.io/collector/exporter/loggingexporter/internal/otlptext" -// It should be kept uptodate with the original version -// TODO: Refactor to remove the duplication - -package otlptext // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/instanaexporter/internal/otlptext" - -import ( - "bytes" - "fmt" - "math" - "strconv" - "strings" - - "go.opentelemetry.io/collector/pdata/pcommon" - "go.opentelemetry.io/collector/pdata/pmetric" - "go.opentelemetry.io/collector/pdata/ptrace" -) - -type dataBuffer struct { - buf bytes.Buffer -} - -func (b *dataBuffer) logEntry(format string, a ...interface{}) { - b.buf.WriteString(fmt.Sprintf(format, a...)) - b.buf.WriteString("\n") -} - -func (b *dataBuffer) logAttr(attr string, value string) { - b.logEntry(" %-15s: %s", attr, value) -} - -func (b *dataBuffer) logAttributes(attr string, m pcommon.Map) { - if m.Len() == 0 { - return - } - - b.logEntry("%s:", attr) - m.Range(func(k string, v pcommon.Value) bool { - b.logEntry(" -> %s: %s(%s)", k, v.Type().String(), attributeValueToString(v)) - return true - }) -} - -func (b *dataBuffer) logInstrumentationScope(il pcommon.InstrumentationScope) { - b.logEntry( - "InstrumentationScope %s %s", - il.Name(), - il.Version()) - b.logAttributes("InstrumentationScope attributes", il.Attributes()) -} - -//lint:ignore U1000 Ignore unused function temporarily until metrics added -func (b *dataBuffer) logMetricDescriptor(md pmetric.Metric) { - b.logEntry("Descriptor:") - b.logEntry(" -> Name: %s", md.Name()) - b.logEntry(" -> Description: %s", md.Description()) - b.logEntry(" -> Unit: %s", md.Unit()) - b.logEntry(" -> DataType: %s", md.Type().String()) -} - -//lint:ignore U1000 Ignore unused function temporarily until metrics added -func (b *dataBuffer) logMetricDataPoints(m pmetric.Metric) { - switch m.Type() { - case pmetric.MetricTypeEmpty: - return - case pmetric.MetricTypeGauge: - b.logNumberDataPoints(m.Gauge().DataPoints()) - case pmetric.MetricTypeSum: - data := m.Sum() - b.logEntry(" -> IsMonotonic: %t", data.IsMonotonic()) - b.logEntry(" -> AggregationTemporality: %s", data.AggregationTemporality().String()) - b.logNumberDataPoints(data.DataPoints()) - case pmetric.MetricTypeHistogram: - data := m.Histogram() - b.logEntry(" -> AggregationTemporality: %s", data.AggregationTemporality().String()) - b.logHistogramDataPoints(data.DataPoints()) - case pmetric.MetricTypeExponentialHistogram: - data := m.ExponentialHistogram() - b.logEntry(" -> AggregationTemporality: %s", data.AggregationTemporality().String()) - b.logExponentialHistogramDataPoints(data.DataPoints()) - case pmetric.MetricTypeSummary: - data := m.Summary() - b.logDoubleSummaryDataPoints(data.DataPoints()) - } -} - -//lint:ignore U1000 Ignore unused function temporarily until metrics added -func (b *dataBuffer) logNumberDataPoints(ps pmetric.NumberDataPointSlice) { - for i := 0; i < ps.Len(); i++ { - p := ps.At(i) - b.logEntry("NumberDataPoints #%d", i) - b.logDataPointAttributes(p.Attributes()) - - b.logEntry("StartTimestamp: %s", p.StartTimestamp()) - b.logEntry("Timestamp: %s", p.Timestamp()) - switch p.ValueType() { - case pmetric.NumberDataPointValueTypeInt: - b.logEntry("Value: %d", p.IntValue()) - case pmetric.NumberDataPointValueTypeDouble: - b.logEntry("Value: %f", p.DoubleValue()) - } - } -} - -//lint:ignore U1000 Ignore unused function temporarily until metrics added -func (b *dataBuffer) logHistogramDataPoints(ps pmetric.HistogramDataPointSlice) { - for i := 0; i < ps.Len(); i++ { - p := ps.At(i) - b.logEntry("HistogramDataPoints #%d", i) - b.logDataPointAttributes(p.Attributes()) - - b.logEntry("StartTimestamp: %s", p.StartTimestamp()) - b.logEntry("Timestamp: %s", p.Timestamp()) - b.logEntry("Count: %d", p.Count()) - - if p.HasSum() { - b.logEntry("Sum: %f", p.Sum()) - } - - if p.HasMin() { - b.logEntry("Min: %f", p.Min()) - } - - if p.HasMax() { - b.logEntry("Max: %f", p.Max()) - } - - for i := 0; i < p.ExplicitBounds().Len(); i++ { - b.logEntry("ExplicitBounds #%d: %f", i, p.ExplicitBounds().At(i)) - } - - for j := 0; j < p.BucketCounts().Len(); j++ { - b.logEntry("Buckets #%d, Count: %d", j, p.BucketCounts().At(j)) - } - } -} - -//lint:ignore U1000 Ignore unused function temporarily until metrics added -func (b *dataBuffer) logExponentialHistogramDataPoints(ps pmetric.ExponentialHistogramDataPointSlice) { - for i := 0; i < ps.Len(); i++ { - p := ps.At(i) - b.logEntry("ExponentialHistogramDataPoints #%d", i) - b.logDataPointAttributes(p.Attributes()) - - b.logEntry("StartTimestamp: %s", p.StartTimestamp()) - b.logEntry("Timestamp: %s", p.Timestamp()) - b.logEntry("Count: %d", p.Count()) - - if p.HasSum() { - b.logEntry("Sum: %f", p.Sum()) - } - - if p.HasMin() { - b.logEntry("Min: %f", p.Min()) - } - - if p.HasMax() { - b.logEntry("Max: %f", p.Max()) - } - - scale := int(p.Scale()) - factor := math.Ldexp(math.Ln2, -scale) - // Note: the equation used here, which is - // math.Exp(index * factor) - // reports +Inf as the _lower_ boundary of the bucket nearest - // infinity, which is incorrect and can be addressed in various - // ways. The OTel-Go implementation of this histogram pending - // in https://github.com/open-telemetry/opentelemetry-go/pull/2393 - // uses a lookup table for the last finite boundary, which can be - // easily computed using `math/big` (for scales up to 20). - - negB := p.Negative().BucketCounts() - posB := p.Positive().BucketCounts() - - for i := 0; i < negB.Len(); i++ { - pos := negB.Len() - i - 1 - index := p.Negative().Offset() + int32(pos) - lower := math.Exp(float64(index) * factor) - upper := math.Exp(float64(index+1) * factor) - b.logEntry("Bucket (%f, %f], Count: %d", -upper, -lower, negB.At(pos)) - } - - if p.ZeroCount() != 0 { - b.logEntry("Bucket [0, 0], Count: %d", p.ZeroCount()) - } - - for pos := 0; pos < posB.Len(); pos++ { - index := p.Positive().Offset() + int32(pos) - lower := math.Exp(float64(index) * factor) - upper := math.Exp(float64(index+1) * factor) - b.logEntry("Bucket [%f, %f), Count: %d", lower, upper, posB.At(pos)) - } - } -} - -//lint:ignore U1000 Ignore unused function temporarily until metrics added -func (b *dataBuffer) logDoubleSummaryDataPoints(ps pmetric.SummaryDataPointSlice) { - for i := 0; i < ps.Len(); i++ { - p := ps.At(i) - b.logEntry("SummaryDataPoints #%d", i) - b.logDataPointAttributes(p.Attributes()) - - b.logEntry("StartTimestamp: %s", p.StartTimestamp()) - b.logEntry("Timestamp: %s", p.Timestamp()) - b.logEntry("Count: %d", p.Count()) - b.logEntry("Sum: %f", p.Sum()) - - quantiles := p.QuantileValues() - for i := 0; i < quantiles.Len(); i++ { - quantile := quantiles.At(i) - b.logEntry("QuantileValue #%d: Quantile %f, Value %f", i, quantile.Quantile(), quantile.Value()) - } - } -} - -//lint:ignore U1000 Ignore unused function temporarily until metrics added -func (b *dataBuffer) logDataPointAttributes(attributes pcommon.Map) { - b.logAttributes("Data point attributes", attributes) -} - -func (b *dataBuffer) logEvents(description string, se ptrace.SpanEventSlice) { - if se.Len() == 0 { - return - } - - b.logEntry("%s:", description) - for i := 0; i < se.Len(); i++ { - e := se.At(i) - b.logEntry("SpanEvent #%d", i) - b.logEntry(" -> Name: %s", e.Name()) - b.logEntry(" -> Timestamp: %s", e.Timestamp()) - b.logEntry(" -> DroppedAttributesCount: %d", e.DroppedAttributesCount()) - - if e.Attributes().Len() == 0 { - continue - } - b.logEntry(" -> Attributes:") - e.Attributes().Range(func(k string, v pcommon.Value) bool { - b.logEntry(" -> %s: %s(%s)", k, v.Type().String(), attributeValueToString(v)) - return true - }) - } -} - -func (b *dataBuffer) logLinks(description string, sl ptrace.SpanLinkSlice) { - if sl.Len() == 0 { - return - } - - b.logEntry("%s:", description) - - for i := 0; i < sl.Len(); i++ { - l := sl.At(i) - b.logEntry("SpanLink #%d", i) - b.logEntry(" -> Trace ID: %s", l.TraceID().HexString()) - b.logEntry(" -> ID: %s", l.SpanID().HexString()) - b.logEntry(" -> TraceState: %s", l.TraceState().AsRaw()) - b.logEntry(" -> DroppedAttributesCount: %d", l.DroppedAttributesCount()) - if l.Attributes().Len() == 0 { - continue - } - b.logEntry(" -> Attributes:") - l.Attributes().Range(func(k string, v pcommon.Value) bool { - b.logEntry(" -> %s: %s(%s)", k, v.Type().String(), attributeValueToString(v)) - return true - }) - } -} - -func attributeValueToString(v pcommon.Value) string { - switch v.Type() { - case pcommon.ValueTypeStr: - return v.Str() - case pcommon.ValueTypeBool: - return strconv.FormatBool(v.Bool()) - case pcommon.ValueTypeDouble: - return strconv.FormatFloat(v.Double(), 'f', -1, 64) - case pcommon.ValueTypeInt: - return strconv.FormatInt(v.Int(), 10) - case pcommon.ValueTypeSlice: - return sliceToString(v.Slice()) - case pcommon.ValueTypeMap: - return mapToString(v.Map()) - default: - return fmt.Sprintf("", v.Type()) - } -} - -func sliceToString(s pcommon.Slice) string { - var b strings.Builder - b.WriteByte('[') - for i := 0; i < s.Len(); i++ { - if i < s.Len()-1 { - fmt.Fprintf(&b, "%s, ", attributeValueToString(s.At(i))) - } else { - b.WriteString(attributeValueToString(s.At(i))) - } - } - - b.WriteByte(']') - return b.String() -} - -func mapToString(m pcommon.Map) string { - var b strings.Builder - b.WriteString("{\n") - - m.Sort().Range(func(k string, v pcommon.Value) bool { - fmt.Fprintf(&b, " -> %s: %s(%s)\n", k, v.Type(), v.AsString()) - return true - }) - b.WriteByte('}') - return b.String() -} diff --git a/exporter/instanaexporter/internal/otlptext/traces.go b/exporter/instanaexporter/internal/otlptext/traces.go deleted file mode 100644 index 7bc34eef3025a..0000000000000 --- a/exporter/instanaexporter/internal/otlptext/traces.go +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright 2022, OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package otlptext // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/instanaexporter/internal/otlptext" - -import ( - "go.opentelemetry.io/collector/pdata/ptrace" - - "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/traceutil" -) - -// NewTextTracesMarshaler returns a serializer.TracesMarshaler to encode to OTLP text bytes. -func NewTextTracesMarshaler() ptrace.Marshaler { - return textTracesMarshaler{} -} - -type textTracesMarshaler struct{} - -// MarshalTraces pdata.Traces to OTLP text. -func (textTracesMarshaler) MarshalTraces(td ptrace.Traces) ([]byte, error) { - buf := dataBuffer{} - rss := td.ResourceSpans() - for i := 0; i < rss.Len(); i++ { - buf.logEntry("ResourceSpans #%d", i) - rs := rss.At(i) - buf.logAttributes("Resource labels", rs.Resource().Attributes()) - ilss := rs.ScopeSpans() - for j := 0; j < ilss.Len(); j++ { - buf.logEntry("InstrumentationLibrarySpans #%d", j) - ils := ilss.At(j) - buf.logInstrumentationScope(ils.Scope()) - - spans := ils.Spans() - for k := 0; k < spans.Len(); k++ { - buf.logEntry("Span #%d", k) - span := spans.At(k) - buf.logAttr("Trace ID", span.TraceID().HexString()) - buf.logAttr("Parent ID", span.ParentSpanID().HexString()) - buf.logAttr("ID", span.SpanID().HexString()) - buf.logAttr("Name", span.Name()) - buf.logAttr("Kind", traceutil.SpanKindStr(span.Kind())) - buf.logAttr("Start time", span.StartTimestamp().String()) - buf.logAttr("End time", span.EndTimestamp().String()) - - buf.logAttr("Status code", traceutil.StatusCodeStr(span.Status().Code())) - buf.logAttr("Status message", span.Status().Message()) - - buf.logAttributes("Attributes", span.Attributes()) - buf.logEvents("Events", span.Events()) - buf.logLinks("Links", span.Links()) - } - } - } - - return buf.buf.Bytes(), nil -}