Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Commit

Permalink
Adding custom report filter (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ-Jasmin authored Jul 28, 2022
1 parent 6492c49 commit 0ce76f9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ if err != nil {
}
defer r.Close()
tracer, err := go2sky.NewTracer("example", go2sky.WithReporter(r))
```
In some scenarios, we may need a filter to filter segments that do not need to be submitted, for example, to reduce the load of gRPC reporting, or only track the request of error.

```go
r, err := reporter.NewGRPCReporter("oap-skywalking:11800", reporter.WithReportStrategy(func(s *v3.SegmentObject) bool {
var isReport bool
for _, span := s.GetSpans() {
if span.GetIsError() {
isReport = true
break
}
}

return isReport
}))

```

You can also create tracer with sampling rate. It supports decimals between **0-1** (two decimal places), representing the sampling percentage of trace.
Expand Down Expand Up @@ -292,4 +308,4 @@ Also, when the go2sky keep alive with backend, modify and open time of the metad
Please read the [official documentation of rover](https://skywalking.apache.org/docs/skywalking-rover/latest/en/setup/configuration/process_discovery/scanner/#agent-sensor-mode) to get more information.

# License
Apache License 2.0. See [LICENSE](LICENSE) file for details.
Apache License 2.0. See [LICENSE](LICENSE) file for details.
6 changes: 6 additions & 0 deletions reporter/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ type gRPCReporter struct {
cdsService *go2sky.ConfigDiscoveryService
cdsClient configuration.ConfigurationDiscoveryServiceClient

// set report strategy
rs ReportStrategy

md metadata.MD
creds credentials.TransportCredentials

Expand Down Expand Up @@ -232,6 +235,9 @@ func (r *gRPCReporter) initSendPipeline() {
continue StreamLoop
}
for s := range r.sendCh {
if r.rs != nil && !r.rs(s) {
continue
}
err = stream.Send(s)
if err != nil {
r.logger.Errorf("send segment error %v", err)
Expand Down
11 changes: 11 additions & 0 deletions reporter/grpc_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import (
agentv3 "skywalking.apache.org/repo/goapi/collect/language/agent/v3"
)

// ReportStrategy allowed to set a custom filter
// to filter the reported segment
type ReportStrategy func(s *agentv3.SegmentObject) bool

// GRPCReporterOption allows for functional options to adjust behaviour
// of a gRPC reporter to be created by NewGRPCReporter
type GRPCReporterOption func(r *gRPCReporter)
Expand Down Expand Up @@ -119,3 +123,10 @@ func WithProcessStatusHook(enable bool) GRPCReporterOption {
r.processStatusHookEnable = enable
}
}

// WithReportStrategy set report strategy
func WithReportStrategy(rs ReportStrategy) GRPCReporterOption {
return func(r *gRPCReporter) {
r.rs = rs
}
}

0 comments on commit 0ce76f9

Please sign in to comment.