-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
language_detection.go
110 lines (92 loc) · 3.64 KB
/
language_detection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build linux
package modules
import (
"fmt"
"io"
"net/http"
"google.golang.org/protobuf/proto"
"github.com/DataDog/datadog-agent/cmd/system-probe/api/module"
"github.com/DataDog/datadog-agent/cmd/system-probe/config"
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/pkg/languagedetection/languagemodels"
"github.com/DataDog/datadog-agent/pkg/languagedetection/privileged"
languageDetectionProto "github.com/DataDog/datadog-agent/pkg/proto/pbgo/languagedetection"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/optional"
)
// LanguageDetectionModule is the language detection module factory
var LanguageDetectionModule = module.Factory{
Name: config.LanguageDetectionModule,
ConfigNamespaces: []string{"language_detection"},
Fn: func(cfg *sysconfigtypes.Config, _ optional.Option[workloadmeta.Component]) (module.Module, error) {
return &languageDetectionModule{
languageDetector: privileged.NewLanguageDetector(),
}, nil
},
NeedsEBPF: func() bool {
return false
},
}
type languageDetectionModule struct {
languageDetector privileged.LanguageDetector
}
func (l *languageDetectionModule) GetStats() map[string]interface{} {
return nil
}
func (l *languageDetectionModule) Register(router *module.Router) error {
router.HandleFunc("/detect", l.detectLanguage)
return nil
}
// Close closes resources associated with the language detection module.
// The language detection module doesn't do anything except route to the privileged language detection api.
// This API currently does not hold any resources over its lifetime, so there is no need to release any resources when the
// module is closed.
func (l *languageDetectionModule) Close() {}
func toDetectLanguageResponse(langs []languagemodels.Language) *languageDetectionProto.DetectLanguageResponse {
resp := &languageDetectionProto.DetectLanguageResponse{
Languages: make([]*languageDetectionProto.Language, len(langs)),
}
for i, lang := range langs {
resp.Languages[i] = &languageDetectionProto.Language{
Name: string(lang.Name),
Version: lang.Version,
}
}
return resp
}
func handleError(writer http.ResponseWriter, status int, err error) {
_ = log.Error("Failed to properly handle /language_detection/detect request:", err)
writer.WriteHeader(status)
}
func (l *languageDetectionModule) detectLanguage(writer http.ResponseWriter, request *http.Request) {
b, err := io.ReadAll(request.Body)
if err != nil {
handleError(writer, http.StatusInternalServerError, fmt.Errorf("read request body: %v", err))
return
}
defer request.Body.Close()
var req languageDetectionProto.DetectLanguageRequest
if err := proto.Unmarshal(b, &req); err != nil {
handleError(writer, http.StatusInternalServerError, fmt.Errorf("unmarshal request: %v", err))
return
}
procs := make([]languagemodels.Process, 0, len(req.Processes))
for _, proc := range req.Processes {
procs = append(procs, proc)
}
resp := toDetectLanguageResponse(l.languageDetector.DetectWithPrivileges(procs))
b, err = proto.Marshal(resp)
if err != nil {
handleError(writer, http.StatusInternalServerError, fmt.Errorf("seralize response: %v", err))
return
}
_, err = writer.Write(b)
if err != nil {
log.Error("/language_detection/detect succeeded but failed to write a response:", err.Error())
}
}