Skip to content

Commit

Permalink
Make pdata.LogsToOtlp impossible to call from outside this repo
Browse files Browse the repository at this point in the history
pdata.LogsToOtlp is intended to be used only by OTLP exporter (for which is the
native data format) and fileexporter (for which we do want to output internal
representation for debugging purposes).

Everyone else is supposed to use public member functions of pdata.Logs.

This changes makes it impossible to use pdata.LogsToOtlp outside this repo by
hiding it behind an intermediary struct that is declared in an internal package.

This is necessary to prevent accidental use of LogsToOtlp and the OTLP data
struct which we prohibit to do since the OTLP data structs may change in the
future.

As part of this change I had to move timestamp-related functions from internal
to pdata package (where they logically belong) to avoid circular package
references.
  • Loading branch information
Tigran Najaryan committed Sep 1, 2020
1 parent eaaebf1 commit cf9e4e0
Show file tree
Hide file tree
Showing 18 changed files with 314 additions and 71 deletions.
14 changes: 8 additions & 6 deletions consumer/pdata/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package pdata
import (
"github.com/gogo/protobuf/proto"

"go.opentelemetry.io/collector/internal"
otlplogs "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/logs/v1"
)

Expand All @@ -37,20 +38,21 @@ func LogsFromOtlp(orig []*otlplogs.ResourceLogs) Logs {
return Logs{&orig}
}

// LogsToOtlp converts the internal Logs to the ProtoBuf.
func LogsToOtlp(ld Logs) []*otlplogs.ResourceLogs {
return *ld.orig
}

// NewLogs creates a new Logs.
func NewLogs() Logs {
orig := []*otlplogs.ResourceLogs(nil)
return Logs{&orig}
}

// InternalRep returns internal representation of the logs. Should not be used outside
// this repository.
func (ld Logs) InternalRep() internal.OtlpLogsWrapper {
return internal.OtlpLogsWrapper{Orig: ld.orig}
}

// Clone returns a copy of Logs.
func (ld Logs) Clone() Logs {
otlp := LogsToOtlp(ld)
otlp := *ld.orig
resourceSpansClones := make([]*otlplogs.ResourceLogs, 0, len(otlp))
for _, resourceSpans := range otlp {
resourceSpansClones = append(resourceSpansClones,
Expand Down
2 changes: 1 addition & 1 deletion consumer/pdata/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ func TestToFromLogProto(t *testing.T) {
otlp := []*otlplogs.ResourceLogs(nil)
td := LogsFromOtlp(otlp)
assert.EqualValues(t, NewLogs(), td)
assert.EqualValues(t, otlp, LogsToOtlp(td))
assert.EqualValues(t, otlp, *td.orig)
}
12 changes: 5 additions & 7 deletions internal/internal.go → consumer/pdata/timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,30 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package internal
package pdata

import (
"time"

"google.golang.org/protobuf/types/known/timestamppb"

"go.opentelemetry.io/collector/consumer/pdata"
)

func TimestampToUnixNano(ts *timestamppb.Timestamp) (t pdata.TimestampUnixNano) {
func TimestampToUnixNano(ts *timestamppb.Timestamp) (t TimestampUnixNano) {
if ts == nil {
return
}
return pdata.TimestampUnixNano(uint64(ts.AsTime().UnixNano()))
return TimestampUnixNano(uint64(ts.AsTime().UnixNano()))
}

func UnixNanoToTimestamp(u pdata.TimestampUnixNano) *timestamppb.Timestamp {
func UnixNanoToTimestamp(u TimestampUnixNano) *timestamppb.Timestamp {
// 0 is a special case and want to make sure we return nil.
if u == 0 {
return nil
}
return timestamppb.New(UnixNanoToTime(u))
}

func UnixNanoToTime(u pdata.TimestampUnixNano) time.Time {
func UnixNanoToTime(u TimestampUnixNano) time.Time {
// 0 is a special case and want to make sure we return a time that IsZero() returns true.
if u == 0 {
return time.Time{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package internal
package pdata

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/timestamppb"

"go.opentelemetry.io/collector/consumer/pdata"
)

func TestUnixNanosConverters(t *testing.T) {
t1 := time.Date(2020, 03, 24, 1, 13, 23, 789, time.UTC)
tun := pdata.TimestampUnixNano(t1.UnixNano())
tun := TimestampUnixNano(t1.UnixNano())

assert.EqualValues(t, uint64(1585012403000000789), tun)
tp := UnixNanoToTimestamp(tun)
Expand Down
3 changes: 2 additions & 1 deletion exporter/fileexporter/file_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/consumer/pdatautil"
"go.opentelemetry.io/collector/internal"
"go.opentelemetry.io/collector/internal/data"
otlplogs "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/logs/v1"
otlpmetrics "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/metrics/v1"
Expand Down Expand Up @@ -57,7 +58,7 @@ func (e *fileExporter) ConsumeMetrics(_ context.Context, md pdata.Metrics) error

func (e *fileExporter) ConsumeLogs(_ context.Context, ld pdata.Logs) error {
request := otlplogs.ExportLogsServiceRequest{
ResourceLogs: pdata.LogsToOtlp(ld),
ResourceLogs: internal.LogsToOtlp(ld.InternalRep()),
}
return exportMessageAsLine(e, &request)
}
Expand Down
3 changes: 2 additions & 1 deletion exporter/otlpexporter/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/consumer/pdatautil"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/internal"
"go.opentelemetry.io/collector/internal/data"
otlplogs "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/logs/v1"
otlpmetrics "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/metrics/v1"
Expand Down Expand Up @@ -104,7 +105,7 @@ func (e *exporterImp) pushMetricsData(ctx context.Context, md pdata.Metrics) (in

func (e *exporterImp) pushLogData(ctx context.Context, logs pdata.Logs) (int, error) {
request := &otlplogs.ExportLogsServiceRequest{
ResourceLogs: pdata.LogsToOtlp(logs),
ResourceLogs: internal.LogsToOtlp(logs.InternalRep()),
}
err := e.w.exportLogs(ctx, request)

Expand Down
Loading

0 comments on commit cf9e4e0

Please sign in to comment.