-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/telemetrygen] add support exporting log data (#18741)
* [pkg/telemetrygen] add support exporting log data Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com> * add changelog Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com> * fix for lint checkt Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com> * fix for lint check Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com> * Update cmd/telemetrygen/internal/logs/logs.go Co-authored-by: Pablo Baeyens <pbaeyens31+github@gmail.com> * Update cmd/telemetrygen/internal/logs/logs.go Co-authored-by: Pablo Baeyens <pbaeyens31+github@gmail.com> * fix pdata pacakge version Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com> --------- Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com> Co-authored-by: Pablo Baeyens <pbaeyens31+github@gmail.com>
- Loading branch information
1 parent
73f6159
commit c35fe0a
Showing
8 changed files
with
372 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 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: Initial implementation of `telemetrygen logs` subcommand for exporting log data | ||
|
||
# One or more tracking issues related to the change | ||
issues: [12927] | ||
|
||
# (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: |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package logs | ||
|
||
import ( | ||
"github.com/spf13/pflag" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen/internal/common" | ||
) | ||
|
||
// Config describes the test scenario. | ||
type Config struct { | ||
common.Config | ||
NumLogs int | ||
} | ||
|
||
// Flags registers config flags. | ||
func (c *Config) Flags(fs *pflag.FlagSet) { | ||
c.CommonFlags(fs) | ||
fs.IntVar(&c.NumLogs, "logs", 1, "Number of logs to generate in each worker (ignored if duration is provided)") | ||
} |
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,124 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package logs | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"go.opentelemetry.io/collector/pdata/plog" | ||
"go.opentelemetry.io/collector/pdata/plog/plogotlp" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0" | ||
"go.uber.org/atomic" | ||
"go.uber.org/zap" | ||
"golang.org/x/time/rate" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen/internal/common" | ||
) | ||
|
||
type exporter interface { | ||
export(plog.Logs) error | ||
} | ||
|
||
type gRPCClientExporter struct { | ||
client plogotlp.GRPCClient | ||
} | ||
|
||
func (e *gRPCClientExporter) export(logs plog.Logs) error { | ||
req := plogotlp.NewExportRequestFromLogs(logs) | ||
if _, err := e.client.Export(context.Background(), req); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Start starts the log telemetry generator | ||
func Start(cfg *Config) error { | ||
logger, err := common.CreateLogger() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if cfg.UseHTTP { | ||
return fmt.Errorf("http is not supported by 'telemetrygen logs'") | ||
} | ||
|
||
if !cfg.Insecure { | ||
return fmt.Errorf("'telemetrygen logs' only supports insecure gRPC") | ||
} | ||
|
||
// only support grpc in insecure mode | ||
clientConn, err := grpc.DialContext(context.TODO(), cfg.Endpoint, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
if err != nil { | ||
return err | ||
} | ||
exporter := &gRPCClientExporter{ | ||
client: plogotlp.NewGRPCClient(clientConn), | ||
} | ||
|
||
if err = Run(cfg, exporter, logger); err != nil { | ||
logger.Error("failed to stop the exporter", zap.Error(err)) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Run executes the test scenario. | ||
func Run(c *Config, exp exporter, logger *zap.Logger) error { | ||
if c.TotalDuration > 0 { | ||
c.NumLogs = 0 | ||
} else if c.NumLogs <= 0 { | ||
return fmt.Errorf("either `logs` or `duration` must be greater than 0") | ||
} | ||
|
||
limit := rate.Limit(c.Rate) | ||
if c.Rate == 0 { | ||
limit = rate.Inf | ||
logger.Info("generation of logs isn't being throttled") | ||
} else { | ||
logger.Info("generation of logs is limited", zap.Float64("per-second", float64(limit))) | ||
} | ||
|
||
wg := sync.WaitGroup{} | ||
running := atomic.NewBool(true) | ||
res := resource.NewWithAttributes(semconv.SchemaURL, c.GetAttributes()...) | ||
|
||
for i := 0; i < c.WorkerCount; i++ { | ||
wg.Add(1) | ||
w := worker{ | ||
numLogs: c.NumLogs, | ||
limitPerSecond: limit, | ||
totalDuration: c.TotalDuration, | ||
running: running, | ||
wg: &wg, | ||
logger: logger.With(zap.Int("worker", i)), | ||
index: i, | ||
} | ||
|
||
go w.simulateLogs(res, exp) | ||
} | ||
if c.TotalDuration > 0 { | ||
time.Sleep(c.TotalDuration) | ||
running.Store(false) | ||
} | ||
wg.Wait() | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package logs | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/plog" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
"go.uber.org/atomic" | ||
"go.uber.org/zap" | ||
"golang.org/x/time/rate" | ||
) | ||
|
||
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) | ||
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) { | ||
limiter := rate.NewLimiter(w.limitPerSecond, 1) | ||
var i int64 | ||
|
||
for w.running.Load() { | ||
logs := plog.NewLogs() | ||
nRes := logs.ResourceLogs().AppendEmpty().Resource() | ||
attrs := res.Attributes() | ||
for _, attr := range attrs { | ||
nRes.Attributes().PutStr(string(attr.Key), attr.Value.AsString()) | ||
} | ||
log := logs.ResourceLogs().At(0).ScopeLogs().AppendEmpty().LogRecords().AppendEmpty() | ||
log.SetTimestamp(pcommon.NewTimestampFromTime(time.Now())) | ||
log.SetDroppedAttributesCount(1) | ||
log.SetSeverityNumber(plog.SeverityNumberInfo) | ||
log.SetSeverityText("Info") | ||
lattrs := log.Attributes() | ||
lattrs.PutStr("app", "server") | ||
|
||
if err := exporter.export(logs); err != nil { | ||
w.logger.Fatal("exporter failed", zap.Error(err)) | ||
} | ||
if err := limiter.Wait(context.Background()); err != nil { | ||
w.logger.Fatal("limiter wait failed, retry", zap.Error(err)) | ||
} | ||
|
||
i++ | ||
if w.numLogs != 0 && i >= int64(w.numLogs) { | ||
break | ||
} | ||
} | ||
|
||
w.logger.Info("logs generated", zap.Int64("logs", i)) | ||
w.wg.Done() | ||
} |
Oops, something went wrong.