Skip to content

Commit

Permalink
release: version 0.8.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-mwangi committed Nov 2, 2023
1 parent 244d138 commit bd51191
Show file tree
Hide file tree
Showing 33 changed files with 5,906 additions and 2,687 deletions.
3 changes: 3 additions & 0 deletions _examples/grpc-client/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
service_name: grpc-client
opa:
enabled: false
2 changes: 1 addition & 1 deletion _examples/grpc-client/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ replace github.com/Traceableai/goagent => ../../

require (
github.com/Traceableai/goagent v0.0.0-00010101000000-000000000000
google.golang.org/grpc v1.52.3
google.golang.org/grpc v1.58.2
)
946 changes: 831 additions & 115 deletions _examples/grpc-client/go.sum

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions _examples/grpc-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const (
)

func main() {
cfg := config.Load()
cfg.Tracing.ServiceName = config.String("grpc-client")
cfg := config.LoadFromFile("./config.yaml")

closer := goagent.Init(cfg)
defer closer()
Expand Down
16 changes: 16 additions & 0 deletions _examples/grpc-server/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
service_name: grpc-server

reporting:
endpoint: localhost:4317
secure: false
trace_reporter_type: OTLP

opa:
enabled: false

debug_log: true

remote_config:
enabled: true
endpoint: localhost:5441
poll_period_seconds: 30
2 changes: 1 addition & 1 deletion _examples/grpc-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ replace github.com/Traceableai/goagent => ../../

require (
github.com/Traceableai/goagent v0.0.0-00010101000000-000000000000
google.golang.org/grpc v1.52.3
google.golang.org/grpc v1.58.2
)
946 changes: 831 additions & 115 deletions _examples/grpc-server/go.sum

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions _examples/grpc-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRe
}

func main() {
cfg := config.Load()
cfg.Tracing.ServiceName = config.String("grpc-server")
cfg := config.LoadFromFile("./config.yaml")

closer := goagent.Init(cfg)
defer closer()

lis, err := net.Listen("tcp", port)
// gosec: "G102: Binds to all network interfaces". Ignore this error.
lis, err := net.Listen("tcp", port) // #nosec
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion _examples/http-client/config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
service_name: http_client
service_name: http-client
opa:
enabled: false
934 changes: 824 additions & 110 deletions _examples/http-client/go.sum

Large diffs are not rendered by default.

17 changes: 14 additions & 3 deletions _examples/http-server/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
service_name: http_server
service_name: http-server

reporting:
endpoint: localhost:4317
secure: false
trace_reporter_type: OTLP

opa:
enabled: false

debug_log: true

remote_config:
enabled: true
endpoint: http://localhost:8181/
pollPeriodSeconds: 30
endpoint: localhost:5441
poll_period_seconds: 30
934 changes: 824 additions & 110 deletions _examples/http-server/go.sum

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions _examples/http-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ func main() {
http.HandlerFunc(fooHandler),
"/foo",
))
log.Fatal(http.ListenAndServe(port, r))
// Using log.Fatal(http.ListenAndServe(":8081", r)) causes a gosec timeout error.
// G114 (CWE-676): Use of net/http serve function that has no support for setting timeouts (Confidence: HIGH, Severity: MEDIUM)
srv := http.Server{
Addr: ":8081",
Handler: r,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
ReadHeaderTimeout: 60 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}

type person struct {
Expand All @@ -55,5 +64,5 @@ func fooHandler(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf("{\"message\": \"Hello %s\"}", p.Name)))
_, _ = w.Write([]byte(fmt.Sprintf("{\"message\": \"Hello %s\"}", p.Name)))
}
934 changes: 824 additions & 110 deletions _examples/mux-server/go.sum

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions _examples/mux-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,22 @@ func main() {
r := mux.NewRouter()
r.Use(traceablemux.NewMiddleware()) // here we use the mux middleware
r.HandleFunc("/foo", http.HandlerFunc(fooHandler))
log.Fatal(http.ListenAndServe(":8081", r))
// Using log.Fatal(http.ListenAndServe(":8081", r)) causes a gosec timeout error.
// G114 (CWE-676): Use of net/http serve function that has no support for setting timeouts (Confidence: HIGH, Severity: MEDIUM)
srv := http.Server{
Addr: ":8081",
Handler: r,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
ReadHeaderTimeout: 60 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}

func fooHandler(w http.ResponseWriter, r *http.Request) {
<-time.After(300 * time.Millisecond)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte("{\"message\": \"Hello world\"}"))
_, _ = w.Write([]byte("{\"message\": \"Hello world\"}"))
}
2 changes: 1 addition & 1 deletion _tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ replace github.com/Traceableai/goagent => ../

require (
github.com/Traceableai/goagent v0.0.0-00010101000000-000000000000
go.uber.org/zap v1.19.1
go.uber.org/zap v1.26.0
)
934 changes: 824 additions & 110 deletions _tests/go.sum

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ func TestConfigLoadIsNotOverridenByDefaults(t *testing.T) {
},
TraceableConfig: &traceableconfig.AgentConfig{
Opa: &traceableconfig.Opa{
Enabled: traceableconfig.Bool(false),
Enabled: traceableconfig.Bool(true),
},
},
}

assert.Equal(t, false, cfg.Tracing.DataCapture.RpcMetadata.Request.Value)
assert.Equal(t, false, cfg.TraceableConfig.Opa.Enabled.Value)
assert.Equal(t, true, cfg.TraceableConfig.Opa.Enabled.Value)

LoadEnv(cfg)
// we verify here the value isn't overridden by default value (true)
assert.Equal(t, false, cfg.Tracing.DataCapture.RpcMetadata.Request.Value)
// we verify default value is used for undefined value (true)
assert.Equal(t, false, cfg.TraceableConfig.Opa.Enabled.Value)
assert.Equal(t, true, cfg.TraceableConfig.Opa.Enabled.Value)
}
11 changes: 7 additions & 4 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var defaultRemoteConfig = &traceableconfig.RemoteConfig{
PollPeriodSeconds: traceableconfig.Int32(30),
CertFile: traceableconfig.String(""),
GrpcMaxCallRecvMsgSize: traceableconfig.Int32(32 * 1024 * 1024),
UseSecureConnection: traceableconfig.Bool(false),
}

// defaultConfig holds the default config values for agent.
Expand Down Expand Up @@ -50,10 +51,11 @@ var defaultConfig = &AgentConfig{
},
TraceableConfig: &traceableconfig.AgentConfig{
Opa: &traceableconfig.Opa{
Enabled: traceableconfig.Bool(true),
Endpoint: traceableconfig.String("http://localhost:8181/"),
PollPeriodSeconds: traceableconfig.Int32(30),
CertFile: traceableconfig.String(""),
Enabled: traceableconfig.Bool(false),
Endpoint: traceableconfig.String("http://localhost:8181/"),
PollPeriodSeconds: traceableconfig.Int32(30),
CertFile: traceableconfig.String(""),
UseSecureConnection: traceableconfig.Bool(false),
},
BlockingConfig: &traceableconfig.BlockingConfig{
Enabled: traceableconfig.Bool(true),
Expand All @@ -68,6 +70,7 @@ var defaultConfig = &AgentConfig{
SkipInternalRequest: traceableconfig.Bool(true),
RemoteConfig: defaultRemoteConfig,
ResponseStatusCode: traceableconfig.Int32(403),
MaxRecursionDepth: traceableconfig.Int32(20),
},
DebugLog: traceableconfig.Bool(false),
RemoteConfig: defaultRemoteConfig,
Expand Down
2 changes: 1 addition & 1 deletion config/loaders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestLoadWithDefaults(t *testing.T) {
assert.Equal(t, "localhost:4317", cfg.Tracing.Reporting.Endpoint.Value)
assert.Equal(t, false, cfg.Tracing.Reporting.Secure.Value)
assert.Equal(t, TraceReporterType_OTLP, cfg.Tracing.Reporting.TraceReporterType)
assert.Equal(t, true, cfg.TraceableConfig.Opa.Enabled.Value)
assert.Equal(t, false, cfg.TraceableConfig.Opa.Enabled.Value)
assert.Equal(t, "http://localhost:8181/", cfg.TraceableConfig.Opa.Endpoint.Value)
assert.Equal(t, int32(30), cfg.TraceableConfig.Opa.PollPeriodSeconds.Value)

Expand Down
42 changes: 36 additions & 6 deletions filter/traceable/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,12 @@ import (
"go.uber.org/zap"
)

const defaultAgentManagerEndpoint = "localhost:5441"
const defaultPollPeriodSec = 30
const (
defaultAgentManagerEndpoint = "localhost:5441"
defaultPollPeriodSec = 30
httpUrlKey = "http.url"
httpTargetKey = "http.target"
)

type Filter struct {
libtraceableHandle C.traceable_libtraceable
Expand All @@ -146,6 +150,8 @@ type libtraceableMethods struct {
initLibtraceableConfig C.init_libtraceable_config_type
}

var URL_ATTRIBUTES = []string{"http.scheme", "net.host.name", "net.host.port", httpTargetKey}

var _ filter.Filter = (*Filter)(nil)

// NewFilter creates libtraceable based filter
Expand Down Expand Up @@ -319,7 +325,7 @@ const (
grpcRequestMetadataPrefix = "rpc.request.metadata."
)

func toFQNHeaders(headers map[string][]string, prefix string) map[string]string {
func toFQNHeaders(headers map[string][]string, prefix string, span sdk.Span) map[string]string {
headerAttributes := map[string]string{}
for k, v := range headers {
k = strings.ToLower(k)
Expand All @@ -334,6 +340,27 @@ func toFQNHeaders(headers map[string][]string, prefix string) map[string]string
}
}
}

// add url attributes so that libtraceable can construct the url if needed
attributes := span.GetAttributes()
if url := attributes.GetValue(httpUrlKey); url != nil {
value := fmt.Sprintf("%s", url)
if strings.Contains(value, "://") {
headerAttributes[httpUrlKey] = value
} else {
// TODO remove after updating ht goagent
// special case when the span attribute http.url set is http.target
if _, found := headerAttributes[httpTargetKey]; !found {
headerAttributes[httpTargetKey] = value
}
for _, key := range URL_ATTRIBUTES {
if value := attributes.GetValue(key); value != nil {
headerAttributes[key] = fmt.Sprintf("%v", value)
}
}
}
}

return headerAttributes
}

Expand All @@ -350,7 +377,7 @@ func (f *Filter) EvaluateURLAndHeaders(span sdk.Span, url string, headers map[st
prefix = grpcRequestMetadataPrefix
}

headerAttributes := toFQNHeaders(headers, prefix)
headerAttributes := toFQNHeaders(headers, prefix, span)
headerAttributes["http.url"] = url

inputLibTraceableAttributes := createLibTraceableAttributes(headerAttributes)
Expand Down Expand Up @@ -396,7 +423,7 @@ func (f *Filter) EvaluateBody(span sdk.Span, body []byte, headers map[string][]s
bodyAttributeName = "rpc.request.body"
}

headerAttributes := toFQNHeaders(headers, headerPrefix)
headerAttributes := toFQNHeaders(headers, headerPrefix, span)
headerAttributes[bodyAttributeName] = string(body)

inputLibTraceableAttributes := createLibTraceableAttributes(headerAttributes)
Expand Down Expand Up @@ -437,7 +464,7 @@ func (f *Filter) Evaluate(span sdk.Span, url string, body []byte, headers map[st
bodyAttributeName = "rpc.request.body"
}

headerAttributes := toFQNHeaders(headers, headerPrefix)
headerAttributes := toFQNHeaders(headers, headerPrefix, span)
headerAttributes["http.url"] = url
if len(body) > 0 {
headerAttributes[bodyAttributeName] = string(body)
Expand Down Expand Up @@ -540,6 +567,7 @@ func populateLibtraceableConfig(libtraceableConfig *C.traceable_libtraceable_con
libtraceableConfig.remote_config.poll_period_sec = C.int(remoteConfigPb.PollPeriodSeconds.Value)
libtraceableConfig.remote_config.cert_file = C.CString(remoteConfigPb.CertFile.Value)
libtraceableConfig.remote_config.grpc_max_call_recv_msg_size = C.long(remoteConfigPb.GrpcMaxCallRecvMsgSize.Value)
libtraceableConfig.remote_config.use_secure_connection = getCBool(remoteConfigPb.UseSecureConnection.Value)

libtraceableConfig.blocking_config.opa_config.enabled = getCBool(config.Opa.Enabled.Value)
libtraceableConfig.blocking_config.opa_config.opa_server_url = C.CString(config.Opa.Endpoint.Value)
Expand All @@ -548,12 +576,14 @@ func populateLibtraceableConfig(libtraceableConfig *C.traceable_libtraceable_con
libtraceableConfig.blocking_config.opa_config.debug_log = getCBool(debugLog)
libtraceableConfig.blocking_config.opa_config.min_delay = C.int(config.Opa.PollPeriodSeconds.Value)
libtraceableConfig.blocking_config.opa_config.max_delay = C.int(config.Opa.PollPeriodSeconds.Value)
libtraceableConfig.blocking_config.opa_config.use_secure_connection = getCBool(config.Opa.UseSecureConnection.Value)

libtraceableConfig.blocking_config.enabled = getCBool(config.BlockingConfig.Enabled.Value)
libtraceableConfig.blocking_config.modsecurity_config.enabled = getCBool(config.BlockingConfig.Modsecurity.Enabled.Value)
libtraceableConfig.blocking_config.rb_config.enabled = getCBool(config.BlockingConfig.RegionBlocking.Enabled.Value)
libtraceableConfig.blocking_config.evaluate_body = getCBool(config.BlockingConfig.EvaluateBody.Value)
libtraceableConfig.blocking_config.skip_internal_request = getCBool(config.BlockingConfig.SkipInternalRequest.Value)
libtraceableConfig.blocking_config.max_recursion_depth = C.int(config.BlockingConfig.MaxRecursionDepth.Value)

libtraceableConfig.agent_config.service_name = C.CString(serviceName)

Expand Down
Loading

0 comments on commit bd51191

Please sign in to comment.