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 text of the log")
fs.Int32Var(&c.SeverityNumber, "severity-number", 9, "Severity number of the log, range from 1 to 24 (inclusive)")
}
49 changes: 48 additions & 1 deletion 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 All @@ -31,7 +32,7 @@ func Start(cfg *Config) error {
}

if err = Run(cfg, e, logger); err != nil {
logger.Error("failed to stop the exporter", zap.Error(err))
logger.Error("failed to execute the test scenario.", zap.Error(err))
return err
}

Expand Down Expand Up @@ -60,12 +61,19 @@ func Run(c *Config, exp exporter, logger *zap.Logger) error {
running := &atomic.Bool{}
running.Store(true)

severityText, severityNumber, err := parseSeverity(c.SeverityText, c.SeverityNumber)
if err != nil {
return err
}

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 +90,42 @@ func Run(c *Config, exp exporter, logger *zap.Logger) error {
wg.Wait()
return nil
}

func parseSeverity(severityText string, severityNumber int32) (string, plog.SeverityNumber, error) {
// severityNumber must range in [1,24]
if severityNumber <= 0 || severityNumber >= 25 {
return "", 0, fmt.Errorf("severity-number is out of range, the valid range is [1,24]")
}

sn := plog.SeverityNumber(severityNumber)

// severity number should match well-known severityText
switch severityText {
case plog.SeverityNumberTrace.String():
if !(severityNumber >= 1 && severityNumber <= 4) {
return "", 0, fmt.Errorf("severity text %q does not match severity number %d, the valid range is [1,4]", severityText, severityNumber)
}
case plog.SeverityNumberDebug.String():
if !(severityNumber >= 5 && severityNumber <= 8) {
return "", 0, fmt.Errorf("severity text %q does not match severity number %d, the valid range is [5,8]", severityText, severityNumber)
}
case plog.SeverityNumberInfo.String():
if !(severityNumber >= 9 && severityNumber <= 12) {
return "", 0, fmt.Errorf("severity text %q does not match severity number %d, the valid range is [9,12]", severityText, severityNumber)
}
case plog.SeverityNumberWarn.String():
if !(severityNumber >= 13 && severityNumber <= 16) {
return "", 0, fmt.Errorf("severity text %q does not match severity number %d, the valid range is [13,16]", severityText, severityNumber)
}
case plog.SeverityNumberError.String():
if !(severityNumber >= 17 && severityNumber <= 20) {
return "", 0, fmt.Errorf("severity text %q does not match severity number %d, the valid range is [17,20]", severityText, severityNumber)
}
case plog.SeverityNumberFatal.String():
if !(severityNumber >= 21 && severityNumber <= 24) {
return "", 0, fmt.Errorf("severity text %q does not match severity number %d, the valid range is [21,24]", severityText, severityNumber)
}
}

return severityText, sn, nil
}
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
16 changes: 15 additions & 1 deletion cmd/telemetrygen/internal/logs/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func TestFixedNumberOfLogs(t *testing.T) {
Config: common.Config{
WorkerCount: 1,
},
NumLogs: 5,
NumLogs: 5,
SeverityText: "Info",
SeverityNumber: 9,
}

exp := &mockExporter{}
Expand All @@ -58,6 +60,8 @@ func TestRateOfLogs(t *testing.T) {
TotalDuration: time.Second / 2,
WorkerCount: 1,
},
SeverityText: "Info",
SeverityNumber: 9,
}
exp := &mockExporter{}

Expand All @@ -77,6 +81,8 @@ func TestUnthrottled(t *testing.T) {
TotalDuration: 1 * time.Second,
WorkerCount: 1,
},
SeverityText: "Info",
SeverityNumber: 9,
}
exp := &mockExporter{}

Expand All @@ -94,6 +100,8 @@ func TestCustomBody(t *testing.T) {
Config: common.Config{
WorkerCount: 1,
},
SeverityText: "Info",
SeverityNumber: 9,
}
exp := &mockExporter{}

Expand Down Expand Up @@ -187,6 +195,8 @@ func configWithNoAttributes(qty int, body string) *Config {
WorkerCount: 1,
TelemetryAttributes: nil,
},
SeverityText: "Info",
SeverityNumber: 9,
}
}

Expand All @@ -198,6 +208,8 @@ func configWithOneAttribute(qty int, body string) *Config {
WorkerCount: 1,
TelemetryAttributes: common.KeyValue{telemetryAttrKeyOne: telemetryAttrValueOne},
},
SeverityText: "Info",
SeverityNumber: 9,
}
}

Expand All @@ -210,5 +222,7 @@ func configWithMultipleAttributes(qty int, body string) *Config {
WorkerCount: 1,
TelemetryAttributes: kvs,
},
SeverityText: "Info",
SeverityNumber: 9,
}
}