Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmd/telemetrygen] Add Support for specifying Log Severity #30990

Merged
Merged
28 changes: 28 additions & 0 deletions .chloggen/telemetrygen_support_for_specifying_log_severity.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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: cmd/telemetrygen

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

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

# (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: [user]
8 changes: 6 additions & 2 deletions cmd/telemetrygen/internal/logs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
// Config describes the test scenario.
type Config struct {
common.Config
NumLogs int
Body string
NumLogs int
Body string
SeverityText string
SeverityNumber int32
}

// Flags registers config flags.
Expand All @@ -24,4 +26,6 @@ func (c *Config) Flags(fs *pflag.FlagSet) {

fs.IntVar(&c.NumLogs, "logs", 1, "Number of logs to generate in each worker (ignored if duration is provided)")
fs.StringVar(&c.Body, "body", "the message", "Body of the log")
fs.StringVar(&c.SeverityText, "severity-text", "Info", "Severity of the log, one of: Trace, Debug, Info, Warn, Error, Fatal")
Frapschen marked this conversation as resolved.
Show resolved Hide resolved
fs.Int32Var(&c.SeverityNumber, "severity-number", 9, "Severity of the log, range from 1 to 24 (inclusive)")
Frapschen marked this conversation as resolved.
Show resolved Hide resolved
}
43 changes: 43 additions & 0 deletions cmd/telemetrygen/internal/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync/atomic"
"time"

"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"go.uber.org/zap"
Expand Down Expand Up @@ -60,12 +61,15 @@ func Run(c *Config, exp exporter, logger *zap.Logger) error {
running := &atomic.Bool{}
running.Store(true)

severityText, severityNumber := parseSeverity(c.SeverityText, c.SeverityNumber)
for i := 0; i < c.WorkerCount; i++ {
wg.Add(1)
w := worker{
numLogs: c.NumLogs,
limitPerSecond: limit,
body: c.Body,
severityText: severityText,
severityNumber: severityNumber,
totalDuration: c.TotalDuration,
running: running,
wg: &wg,
Expand All @@ -82,3 +86,42 @@ func Run(c *Config, exp exporter, logger *zap.Logger) error {
wg.Wait()
return nil
}

func parseSeverity(severityText string, severityNumber int32) (string, plog.SeverityNumber) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using the default when invalid data is present, we should return an error.

For example, if severityNumber is outside of the valid range, we should return an error that says that the valid range is [1,24].

We may need to take care of the default case separately, but otherwise we shouldn't ignore what the user set, we should instead tell them that it is invalid.

// severityNumber must range in [1,24]
if severityNumber <= 0 || severityNumber >= 25 {
severityNumber = 9
}

sn := plog.SeverityNumber(severityNumber)

// severity number should match well-known severityText
switch severityText {
case plog.SeverityNumberTrace.String():
if !(severityNumber >= 1 && severityNumber <= 4) {
sn = plog.SeverityNumberTrace
}
mx-psi marked this conversation as resolved.
Show resolved Hide resolved
case plog.SeverityNumberDebug.String():
if !(severityNumber >= 5 && severityNumber <= 8) {
sn = plog.SeverityNumberDebug
}
case plog.SeverityNumberInfo.String():
if !(severityNumber >= 9 && severityNumber <= 12) {
sn = plog.SeverityNumberInfo
}
case plog.SeverityNumberWarn.String():
if !(severityNumber >= 13 && severityNumber <= 16) {
sn = plog.SeverityNumberWarn
}
case plog.SeverityNumberError.String():
if !(severityNumber >= 17 && severityNumber <= 20) {
sn = plog.SeverityNumberError
}
case plog.SeverityNumberFatal.String():
if !(severityNumber >= 21 && severityNumber <= 24) {
sn = plog.SeverityNumberFatal
}
}

return severityText, sn
}
22 changes: 12 additions & 10 deletions cmd/telemetrygen/internal/logs/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ import (
)

type worker struct {
running *atomic.Bool // pointer to shared flag that indicates it's time to stop the test
numLogs int // how many logs the worker has to generate (only when duration==0)
body string // the body of the log
totalDuration time.Duration // how long to run the test for (overrides `numLogs`)
limitPerSecond rate.Limit // how many logs per second to generate
wg *sync.WaitGroup // notify when done
logger *zap.Logger // logger
index int // worker index
running *atomic.Bool // pointer to shared flag that indicates it's time to stop the test
numLogs int // how many logs the worker has to generate (only when duration==0)
body string // the body of the log
severityNumber plog.SeverityNumber // the severityNumber of the log
severityText string // the severityText of the log
totalDuration time.Duration // how long to run the test for (overrides `numLogs`)
limitPerSecond rate.Limit // how many logs per second to generate
wg *sync.WaitGroup // notify when done
logger *zap.Logger // logger
index int // worker index
}

func (w worker) simulateLogs(res *resource.Resource, exporter exporter, telemetryAttributes []attribute.KeyValue) {
Expand All @@ -43,8 +45,8 @@ func (w worker) simulateLogs(res *resource.Resource, exporter exporter, telemetr
log.Body().SetStr(w.body)
log.SetTimestamp(pcommon.NewTimestampFromTime(time.Now()))
log.SetDroppedAttributesCount(1)
log.SetSeverityNumber(plog.SeverityNumberInfo)
log.SetSeverityText("Info")
log.SetSeverityNumber(w.severityNumber)
log.SetSeverityText(w.severityText)
log.Attributes()
lattrs := log.Attributes()
lattrs.PutStr("app", "server")
Expand Down