Skip to content

Commit c698df3

Browse files
authored
API and Metrics Collection Update (#37)
* SCP-198 update apis to accept /api/ and / * added option to control OTEL host metrics
1 parent 51c3252 commit c698df3

18 files changed

+72
-39
lines changed

.idea/dictionaries/egans.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- Upcoming changes...
1111

12+
## [1.4.1] - 2024-03-22
13+
### Added
14+
- Added Telemetry option to fine tune which host level metrics are produced by default
15+
- `ExtraMetrics` can be set to true/false to
16+
17+
## [1.4.0] - 2024-03-15
18+
### Added
19+
- Added support for request routing through root
20+
- So it is now possible to route through `/api/` and `/`
21+
- i.e. `https://localhost/api/scan/direct` and `https://localhost/scan/api`
22+
1223
## [1.3.5] - 2023-12-30
1324
### Added
1425
- Added scan file size metric
@@ -104,3 +115,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
104115
[1.3.3]: https://github.com/scanoss/api.go/compare/v1.3.2...v1.3.3
105116
[1.3.4]: https://github.com/scanoss/api.go/compare/v1.3.3...v1.3.4
106117
[1.3.5]: https://github.com/scanoss/api.go/compare/v1.3.4...v1.3.5
118+
[1.4.0]: https://github.com/scanoss/api.go/compare/v1.3.5...v1.4.0
119+
[1.4.1]: https://github.com/scanoss/api.go/compare/v1.4.0...v1.4.1

config/app-config-dev.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"Scanning": {
1010
"ScanBinary": "./test-support/scanoss.sh",
11-
"ScanningURL": "http://localhost:5443/api",
11+
"ScanningURL": "http://localhost:5443",
1212
"TmpFileDelete": true
1313
}
1414
}

config/app-config-prod.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"Telemetry": {
1919
"Enabled": false,
20+
"ExtraMetrics": false,
2021
"OltpExporter": "0.0.0.0:4317"
2122
},
2223
"Scanning": {

pkg/config/server_config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type ServerConfig struct {
5151
}
5252
Telemetry struct {
5353
Enabled bool `env:"OTEL_ENABLED"` // true/false
54+
ExtraMetrics bool `env:"OTEL_EXTRA"` // true/false
5455
OltpExporter string `env:"OTEL_EXPORTER_OLTP"` // OTEL OLTP exporter (default 0.0.0.0:4317)
5556
}
5657
Scanning struct {
@@ -111,6 +112,7 @@ func setServerConfigDefaults(cfg *ServerConfig) {
111112
cfg.Scanning.WfpGrouping = 3 // Default number of WFPs to group into a single scan request (when Workers > 1)
112113
cfg.Scanning.HPSMEnabled = true
113114
cfg.Telemetry.Enabled = false
115+
cfg.Telemetry.ExtraMetrics = true // Default to sending all the extra service metrics
114116
cfg.Telemetry.OltpExporter = "0.0.0.0:4317" // Default OTEL OLTP gRPC Exporter endpoint
115117
cfg.Scanning.FileContents = true // Matched File URL response enabled (true) by default
116118
}

pkg/protocol/rest/server.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func RunServer(config *myconfig.ServerConfig, version string) error {
6363
return err
6464
}
6565
if config.Telemetry.Enabled {
66-
oltpShutdown, err := initProviders(config, version)
66+
oltpShutdown, err := initProviders(config, version, config.Telemetry.ExtraMetrics)
6767
if err != nil {
6868
return err
6969
}
@@ -82,15 +82,25 @@ func RunServer(config *myconfig.ServerConfig, version string) error {
8282
router.HandleFunc("/api/", service.WelcomeMsg).Methods(http.MethodGet)
8383
router.HandleFunc("/api/", service.HeadResponse).Methods(http.MethodHead)
8484
router.HandleFunc("/api/health", service.HealthCheck).Methods(http.MethodGet)
85+
router.HandleFunc("/health", service.HealthCheck).Methods(http.MethodGet)
8586
router.HandleFunc("/api/health", service.HeadResponse).Methods(http.MethodHead)
87+
router.HandleFunc("/health", service.HeadResponse).Methods(http.MethodHead)
8688
router.HandleFunc("/api/health-check", service.HealthCheck).Methods(http.MethodGet)
89+
router.HandleFunc("/health-check", service.HealthCheck).Methods(http.MethodGet)
8790
router.HandleFunc("/api/health-check", service.HeadResponse).Methods(http.MethodHead)
91+
router.HandleFunc("/health-check", service.HeadResponse).Methods(http.MethodHead)
8892
router.HandleFunc("/api/metrics/{type}", service.MetricsHandler).Methods(http.MethodGet)
93+
router.HandleFunc("/metrics/{type}", service.MetricsHandler).Methods(http.MethodGet)
8994
router.HandleFunc("/api/file_contents/{md5}", apiService.FileContents).Methods(http.MethodGet)
95+
router.HandleFunc("/file_contents/{md5}", apiService.FileContents).Methods(http.MethodGet)
9096
router.HandleFunc("/api/kb/details", apiService.KBDetails).Methods(http.MethodGet)
97+
router.HandleFunc("/kb/details", apiService.KBDetails).Methods(http.MethodGet)
9198
router.HandleFunc("/api/license/obligations/{license}", apiService.LicenseDetails).Methods(http.MethodGet)
99+
router.HandleFunc("/license/obligations/{license}", apiService.LicenseDetails).Methods(http.MethodGet)
92100
router.HandleFunc("/api/scan/direct", apiService.ScanDirect).Methods(http.MethodPost)
101+
router.HandleFunc("/scan/direct", apiService.ScanDirect).Methods(http.MethodPost)
93102
router.HandleFunc("/api/sbom/attribution", apiService.SbomAttribution).Methods(http.MethodPost)
103+
router.HandleFunc("/sbom/attribution", apiService.SbomAttribution).Methods(http.MethodPost)
94104
// Setup Open Telemetry (OTEL)
95105
if config.Telemetry.Enabled {
96106
router.Use(otelmux.Middleware("scanoss-api"))
@@ -309,22 +319,26 @@ func loadFiltering(config *myconfig.ServerConfig) ([]string, []string, error) {
309319
}
310320

311321
// initProviders sets up the OLTP Meter and Trace providers and the OLTP gRPC exporter.
312-
func initProviders(config *myconfig.ServerConfig, version string) (func(), error) {
322+
func initProviders(config *myconfig.ServerConfig, version string, extraAttributes bool) (func(), error) {
313323
zlog.L.Info("Setting up Open Telemetry providers.")
314324
// Setup resource for the providers
315325
ctx := context.Background()
316-
res, err := resource.New(ctx,
317-
resource.WithFromEnv(),
318-
resource.WithProcess(),
319-
resource.WithTelemetrySDK(),
320-
resource.WithHost(),
321-
resource.WithAttributes(
322-
// the service name & version used to display traces in backends
323-
semconv.ServiceName(config.App.Name),
324-
semconv.ServiceNamespace("scanoss-api"),
325-
semconv.ServiceVersion(strings.TrimSpace(version)),
326-
),
326+
var opts []resource.Option
327+
// Extra service level attributes to report
328+
if extraAttributes {
329+
opts = append(opts, resource.WithFromEnv())
330+
opts = append(opts, resource.WithProcess())
331+
opts = append(opts, resource.WithTelemetrySDK())
332+
}
333+
opts = append(opts, resource.WithHost())
334+
// the service name & version used to display traces in backends
335+
opts = append(opts, resource.WithAttributes(
336+
semconv.ServiceName(config.App.Name),
337+
semconv.ServiceNamespace("scanoss-api"),
338+
semconv.ServiceVersion(strings.TrimSpace(version)),
339+
),
327340
)
341+
res, err := resource.New(ctx, opts...)
328342
if err != nil {
329343
zlog.S.Errorf("Failed to create oltp resource: %v", err)
330344
return nil, err

pkg/service/attribution_service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func TestSbomAttribution(t *testing.T) {
117117
}
118118
_ = mw.Close() // close the writer before making the request
119119

120-
req := httptest.NewRequest(http.MethodPost, "http://localhost/api/sbom/attribution", postBody)
120+
req := httptest.NewRequest(http.MethodPost, "http://localhost/sbom/attribution", postBody)
121121
w := httptest.NewRecorder()
122122
req.Header.Add("Content-Type", mw.FormDataContentType())
123123
apiService.SbomAttribution(w, req)

pkg/service/filecontents_service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestFileContents(t *testing.T) {
9191
}
9292
myConfig.Scanning.ScanBinary = test.binary
9393
myConfig.Telemetry.Enabled = test.telemetry
94-
req := newReq("GET", "http://localhost/api/file_contents/{md5}", "", test.input)
94+
req := newReq("GET", "http://localhost/file_contents/{md5}", "", test.input)
9595
w := httptest.NewRecorder()
9696
apiService.FileContents(w, req)
9797
resp := w.Result()

pkg/service/licensedetails_service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestLicenseDetails(t *testing.T) {
9292
}
9393
myConfig.Scanning.ScanBinary = test.binary
9494
myConfig.Telemetry.Enabled = test.telemetry
95-
req := newReq("GET", "http://localhost/api/license/obligations/{license}", "", test.input)
95+
req := newReq("GET", "http://localhost/license/obligations/{license}", "", test.input)
9696
w := httptest.NewRecorder()
9797
apiService.LicenseDetails(w, req)
9898
resp := w.Result()

pkg/service/scanning_service_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func TestScanDirectSingle(t *testing.T) {
226226
}
227227
_ = mw.Close() // close the writer before making the request
228228

229-
req := httptest.NewRequest(http.MethodPost, "http://localhost/api/api/scan/direct", postBody)
229+
req := httptest.NewRequest(http.MethodPost, "http://localhost/scan/direct", postBody)
230230
w := httptest.NewRecorder()
231231
req.Header.Add("Content-Type", mw.FormDataContentType())
232232
apiService.ScanDirect(w, req)
@@ -340,7 +340,7 @@ func TestScanDirectThreaded(t *testing.T) {
340340
}
341341
_ = mw.Close() // close the writer before making the request
342342

343-
req := httptest.NewRequest(http.MethodPost, "http://localhost/api/api/scan/direct", postBody)
343+
req := httptest.NewRequest(http.MethodPost, "http://localhost/scan/direct", postBody)
344344
w := httptest.NewRecorder()
345345
req.Header.Add("Content-Type", mw.FormDataContentType())
346346
apiService.ScanDirect(w, req)
@@ -433,7 +433,7 @@ func TestScanDirectSingleHPSM(t *testing.T) {
433433
}
434434
_ = mw.Close() // close the writer before making the request
435435

436-
req := httptest.NewRequest(http.MethodPost, "http://localhost/api/api/scan/direct", postBody)
436+
req := httptest.NewRequest(http.MethodPost, "http://localhost/scan/direct", postBody)
437437
w := httptest.NewRecorder()
438438
req.Header.Add("Content-Type", mw.FormDataContentType())
439439
apiService.ScanDirect(w, req)

0 commit comments

Comments
 (0)