-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore][pkg/stanza] Cleanup output operator files (#32071)
Contributes to #32058
- Loading branch information
1 parent
0556ed6
commit ba0c279
Showing
8 changed files
with
137 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package drop // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/output/drop" | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" | ||
) | ||
|
||
// Output is an operator that consumes and ignores incoming entries. | ||
type Output struct { | ||
helper.OutputOperator | ||
} | ||
|
||
// Process will drop the incoming entry. | ||
func (o *Output) Process(_ context.Context, _ *entry.Entry) error { | ||
return nil | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package file // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/output/file" | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"html/template" | ||
"os" | ||
"sync" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" | ||
) | ||
|
||
// Output is an operator that writes logs to a file. | ||
type Output struct { | ||
helper.OutputOperator | ||
|
||
path string | ||
tmpl *template.Template | ||
encoder *json.Encoder | ||
file *os.File | ||
mux sync.Mutex | ||
} | ||
|
||
// Start will open the output file. | ||
func (o *Output) Start(_ operator.Persister) error { | ||
var err error | ||
o.file, err = os.OpenFile(o.path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
o.encoder = json.NewEncoder(o.file) | ||
o.encoder.SetEscapeHTML(false) | ||
|
||
return nil | ||
} | ||
|
||
// Stop will close the output file. | ||
func (o *Output) Stop() error { | ||
if o.file != nil { | ||
if err := o.file.Close(); err != nil { | ||
o.Errorf(err.Error()) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Process will write an entry to the output file. | ||
func (o *Output) Process(_ context.Context, entry *entry.Entry) error { | ||
o.mux.Lock() | ||
defer o.mux.Unlock() | ||
|
||
if o.tmpl != nil { | ||
err := o.tmpl.Execute(o.file, entry) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
err := o.encoder.Encode(entry) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package stdout // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/output/stdout" | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"sync" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" | ||
) | ||
|
||
// Output is an operator that logs entries using stdout. | ||
type Output struct { | ||
helper.OutputOperator | ||
encoder *json.Encoder | ||
mux sync.Mutex | ||
} | ||
|
||
// Process will log entries received. | ||
func (o *Output) Process(_ context.Context, entry *entry.Entry) error { | ||
o.mux.Lock() | ||
err := o.encoder.Encode(entry) | ||
if err != nil { | ||
o.mux.Unlock() | ||
o.Errorf("Failed to process entry: %s", err) | ||
return err | ||
} | ||
o.mux.Unlock() | ||
return nil | ||
} |
File renamed without changes.