Skip to content

Commit

Permalink
Merge branch 'main' into gh-semconv-1.28plus
Browse files Browse the repository at this point in the history
  • Loading branch information
adrielp authored Dec 9, 2024
2 parents 83a55c0 + 94d097a commit 167fef7
Show file tree
Hide file tree
Showing 27 changed files with 474 additions and 48 deletions.
27 changes: 27 additions & 0 deletions .chloggen/f-profiles-fileexporter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: fileexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add support for profiles signal.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [35978]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
4 changes: 3 additions & 1 deletion exporter/fileexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
<!-- status autogenerated section -->
| Status | |
| ------------- |-----------|
| Stability | [alpha]: traces, metrics, logs |
| Stability | [development]: profiles |
| | [alpha]: traces, metrics, logs |
| Distributions | [core], [contrib], [k8s] |
| Issues | [![Open issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aopen%20label%3Aexporter%2Ffile%20&label=open&color=orange&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aopen+is%3Aissue+label%3Aexporter%2Ffile) [![Closed issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aclosed%20label%3Aexporter%2Ffile%20&label=closed&color=blue&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aclosed+is%3Aissue+label%3Aexporter%2Ffile) |
| [Code Owners](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/CONTRIBUTING.md#becoming-a-code-owner) | [@atingchen](https://www.github.com/atingchen) |

[development]: https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/component-stability.md#development
[alpha]: https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/component-stability.md#alpha
[core]: https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol
[contrib]: https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-contrib
Expand Down
19 changes: 19 additions & 0 deletions exporter/fileexporter/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/exporter/exporterprofiles"
"go.opentelemetry.io/collector/exporter/exportertest"
"go.opentelemetry.io/collector/extension/extensiontest"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/pprofile"
"go.opentelemetry.io/collector/pdata/ptrace"

"github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/otlpencodingextension"
Expand Down Expand Up @@ -51,28 +53,34 @@ func TestEncoding(t *testing.T) {
require.NoError(t, err)
le, err := f.CreateLogs(context.Background(), exportertest.NewNopSettings(), cfg)
require.NoError(t, err)
pe, err := f.(exporterprofiles.Factory).CreateProfiles(context.Background(), exportertest.NewNopSettings(), cfg)
require.NoError(t, err)
host := hostWithEncoding{
map[component.ID]component.Component{id: ext},
}
require.NoError(t, me.Start(context.Background(), host))
require.NoError(t, te.Start(context.Background(), host))
require.NoError(t, le.Start(context.Background(), host))
require.NoError(t, pe.Start(context.Background(), host))
t.Cleanup(func() {
})

require.NoError(t, me.ConsumeMetrics(context.Background(), generateMetrics()))
require.NoError(t, te.ConsumeTraces(context.Background(), generateTraces()))
require.NoError(t, le.ConsumeLogs(context.Background(), generateLogs()))
require.NoError(t, pe.ConsumeProfiles(context.Background(), generateProfiles()))

require.NoError(t, me.Shutdown(context.Background()))
require.NoError(t, te.Shutdown(context.Background()))
require.NoError(t, le.Shutdown(context.Background()))
require.NoError(t, pe.Shutdown(context.Background()))

b, err := os.ReadFile(cfg.Path)
require.NoError(t, err)
require.Contains(t, string(b), `{"resourceMetrics":`)
require.Contains(t, string(b), `{"resourceSpans":`)
require.Contains(t, string(b), `{"resourceLogs":`)
require.Contains(t, string(b), `{"resourceProfiles":`)
}

func generateLogs() plog.Logs {
Expand All @@ -85,6 +93,17 @@ func generateLogs() plog.Logs {
return logs
}

func generateProfiles() pprofile.Profiles {
proflies := pprofile.NewProfiles()
rp := proflies.ResourceProfiles().AppendEmpty()
rp.Resource().Attributes().PutStr("resource", "R1")
p := rp.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
p.SetProfileID(pprofile.NewProfileIDEmpty())
p.SetStartTime(pcommon.NewTimestampFromTime(time.Now().Add(-1 * time.Second)))
p.SetDuration(pcommon.Timestamp(1 * time.Second / time.Nanosecond))
return proflies
}

func generateMetrics() pmetric.Metrics {
metrics := pmetric.NewMetrics()
rm := metrics.ResourceMetrics().AppendEmpty()
Expand Down
30 changes: 26 additions & 4 deletions exporter/fileexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import (
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/exporter/exporterhelper/exporterhelperprofiles"
"go.opentelemetry.io/collector/exporter/exporterprofiles"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/pprofile"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/zap"
"gopkg.in/natefinch/lumberjack.v2"
Expand Down Expand Up @@ -44,16 +47,18 @@ type FileExporter interface {
consumeTraces(_ context.Context, td ptrace.Traces) error
consumeMetrics(_ context.Context, md pmetric.Metrics) error
consumeLogs(_ context.Context, ld plog.Logs) error
consumeProfiles(_ context.Context, pd pprofile.Profiles) error
}

// NewFactory creates a factory for OTLP exporter.
func NewFactory() exporter.Factory {
return exporter.NewFactory(
return exporterprofiles.NewFactory(
metadata.Type,
createDefaultConfig,
exporter.WithTraces(createTracesExporter, metadata.TracesStability),
exporter.WithMetrics(createMetricsExporter, metadata.MetricsStability),
exporter.WithLogs(createLogsExporter, metadata.LogsStability))
exporterprofiles.WithTraces(createTracesExporter, metadata.TracesStability),
exporterprofiles.WithMetrics(createMetricsExporter, metadata.MetricsStability),
exporterprofiles.WithLogs(createLogsExporter, metadata.LogsStability),
exporterprofiles.WithProfiles(createProfilesExporter, metadata.ProfilesStability))
}

func createDefaultConfig() component.Config {
Expand Down Expand Up @@ -118,6 +123,23 @@ func createLogsExporter(
)
}

func createProfilesExporter(
ctx context.Context,
set exporter.Settings,
cfg component.Config,
) (exporterprofiles.Profiles, error) {
fe := getOrCreateFileExporter(cfg, set.Logger)
return exporterhelperprofiles.NewProfilesExporter(
ctx,
set,
cfg,
fe.consumeProfiles,
exporterhelper.WithStart(fe.Start),
exporterhelper.WithShutdown(fe.Shutdown),
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
)
}

// getOrCreateFileExporter creates a FileExporter and caches it for a particular configuration,
// or returns the already cached one. Caching is required because the factory is asked trace and
// metric receivers separately when it gets CreateTraces() and CreateMetrics()
Expand Down
27 changes: 27 additions & 0 deletions exporter/fileexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,33 @@ func TestCreateLogsError(t *testing.T) {
assert.Error(t, err)
}

func TestCreateProfiles(t *testing.T) {
cfg := &Config{
FormatType: formatTypeJSON,
Path: tempFileName(t),
}
exp, err := createProfilesExporter(
context.Background(),
exportertest.NewNopSettings(),
cfg)
assert.NoError(t, err)
require.NotNil(t, exp)
assert.NoError(t, exp.Shutdown(context.Background()))
}

func TestCreateProfilesError(t *testing.T) {
cfg := &Config{
FormatType: formatTypeJSON,
}
e, err := createProfilesExporter(
context.Background(),
exportertest.NewNopSettings(),
cfg)
require.NoError(t, err)
err = e.Start(context.Background(), componenttest.NewNopHost())
assert.Error(t, err)
}

func TestNewFileWriter(t *testing.T) {
type args struct {
cfg *Config
Expand Down
9 changes: 9 additions & 0 deletions exporter/fileexporter/file_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/pprofile"
"go.opentelemetry.io/collector/pdata/ptrace"
)

Expand Down Expand Up @@ -43,6 +44,14 @@ func (e *fileExporter) consumeLogs(_ context.Context, ld plog.Logs) error {
return e.writer.export(buf)
}

func (e *fileExporter) consumeProfiles(_ context.Context, pd pprofile.Profiles) error {
buf, err := e.marshaller.marshalProfiles(pd)
if err != nil {
return err
}
return e.writer.export(buf)
}

// Start starts the flush timer if set.
func (e *fileExporter) Start(_ context.Context, host component.Host) error {
var err error
Expand Down
Loading

0 comments on commit 167fef7

Please sign in to comment.