Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Po-Yao Chen committed Nov 7, 2024
1 parent 78bf4af commit 51c5b07
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ type Config struct {
// certificate in seconds.
PKCS11RequestTimeout uint `json:"requestTimeout"`

// OTelConf defines the configuration for oTel instrumentation.
OTelConf struct {
// OTel defines the configuration for oTel instrumentation.
OTel struct {
// Enabled indicates whether to enable OTel metrics.
Enabled bool
// OTELCollectorEndpoint specifies the endpoint of OTel collector.
Expand Down
13 changes: 13 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ func TestParse(t *testing.T) {
ReadTimeout: 10,
WriteTimeout: 10,
PKCS11RequestTimeout: 15,
OTel: struct {
Enabled bool
OTELCollectorEndpoint string
ClientCertPath string
ClientKeyPath string
OTelCACertPath string
}{
Enabled: true,
OTELCollectorEndpoint: "http://localhost:4317",
ClientCertPath: "/path/to/client/cert",
ClientKeyPath: "/path/to/client/key",
OTelCACertPath: "/path/to/ca/cert",
},
}
testcases := map[string]struct {
filePath string
Expand Down
9 changes: 8 additions & 1 deletion config/testdata/testconf-good.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,12 @@
"IdleTimeout": 30,
"ReadTimeout": 10,
"WriteTimeout": 10,
"RequestTimeout": 15
"RequestTimeout": 15,
"OTel": {
"Enabled": true,
"OTELCollectorEndpoint": "http://localhost:4317",
"ClientCertPath": "/path/to/client/cert",
"ClientKeyPath": "/path/to/client/key",
"OTelCACertPath": "/path/to/ca/cert"
}
}
8 changes: 4 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,21 @@ func Main() {
log.SetOutput(file)
go logRotate(file)

if cfg.OTelConf.Enabled {
if cfg.OTel.Enabled {
otelResource, err := resource.Merge(
resource.Default(),
resource.NewWithAttributes(semconv.SchemaURL, semconv.ServiceNameKey.String("crypki")),
)
if err != nil {
log.Fatalf("Error merging resources: %v", err)
}
otelTLSConf, err := tlsClientConfiguration(cfg.OTelConf.OTelCACertPath, cfg.OTelConf.ClientCertPath,
cfg.OTelConf.ClientKeyPath)
otelTLSConf, err := tlsClientConfiguration(cfg.OTel.OTelCACertPath, cfg.OTel.ClientCertPath,
cfg.OTel.ClientKeyPath)
if err != nil {
log.Fatalf("Error loading otel TLS config: %v", err)
}
shutdownProvider := otellib.InitOTelSDK(context.Background(),
cfg.OTelConf.OTELCollectorEndpoint, otelTLSConf, otelResource)
cfg.OTel.OTELCollectorEndpoint, otelTLSConf, otelResource)

defer func() {
if err := shutdownProvider(context.Background()); err != nil {
Expand Down

0 comments on commit 51c5b07

Please sign in to comment.