-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[querier] add external apm trace adapter
- Loading branch information
Showing
12 changed files
with
586 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
server/querier/app/tracing-adapter/common/httpclient.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright (c) 2023 Yunshan Networks | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package common | ||
|
||
import ( | ||
"bytes" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/deepflowio/deepflow/server/querier/app/tracing-adapter/config" | ||
"github.com/goccy/go-json" | ||
"github.com/op/go-logging" | ||
) | ||
|
||
var log = logging.MustGetLogger("tracing-adapter.common") | ||
|
||
func DefaultContentTypeHeader() map[string]string { | ||
return map[string]string{ | ||
"Content-Type": "application/json", | ||
} | ||
} | ||
|
||
func Serialize[T any](obj T) ([]byte, error) { | ||
result, err := json.Marshal(obj) | ||
return result, err | ||
} | ||
|
||
func Deserialize[T any](data []byte) (*T, error) { | ||
var result T | ||
err := json.Unmarshal(data, &result) | ||
return &result, err | ||
} | ||
|
||
func prepareRequest(timeout time.Duration, tlsConfig *config.TLSConfig) (*http.Client, error) { | ||
client := &http.Client{} | ||
http.DefaultClient.Timeout = timeout | ||
if tlsConfig != nil { | ||
tlsClientConfig := &tls.Config{} | ||
if tlsConfig.Insecure { | ||
tlsClientConfig.InsecureSkipVerify = true | ||
} else { | ||
clientTLSCert, err := tls.LoadX509KeyPair(tlsConfig.CertFile, tlsConfig.KeyFile) | ||
if err != nil { | ||
log.Errorf("load cert file fot tls verification false! err: %s", err) | ||
return nil, err | ||
} | ||
certPool, err := x509.SystemCertPool() | ||
if err != nil { | ||
log.Errorf("create cert pool false! err: %s", err) | ||
return nil, err | ||
} | ||
caCertPEM, err := os.ReadFile(tlsConfig.CAFile) | ||
if err != nil { | ||
log.Errorf("read ca file false! err: %s", err) | ||
return nil, err | ||
} | ||
|
||
if ok := certPool.AppendCertsFromPEM(caCertPEM); !ok { | ||
log.Errorf("invalid cert for CA PEM! err: %s", err) | ||
return nil, err | ||
} | ||
tlsClientConfig.RootCAs = certPool | ||
tlsClientConfig.Certificates = []tls.Certificate{clientTLSCert} | ||
} | ||
|
||
http.DefaultClient.Transport = &http.Transport{TLSClientConfig: tlsClientConfig} | ||
} | ||
return client, nil | ||
} | ||
|
||
func DoRequest(method string, addr string, body []byte, headers map[string]string, timeout time.Duration, tlsConfig *config.TLSConfig) ([]byte, error) { | ||
client, err := prepareRequest(timeout, tlsConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req, err := http.NewRequest(method, addr, bytes.NewReader(body)) | ||
if err != nil { | ||
log.Error(err) | ||
return nil, err | ||
} | ||
for k, v := range headers { | ||
req.Header.Set(k, v) | ||
} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
log.Errorf("http client do request at %s, error: %s", addr, err) | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
errMsg := fmt.Sprintf("http client request: %s failed, response detail: %+v", addr, resp) | ||
log.Error(errMsg) | ||
return nil, errors.New(errMsg) | ||
} | ||
|
||
respBytes, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Errorf("read (%s) body failed, (%v)", addr, err) | ||
return nil, err | ||
} | ||
|
||
return respBytes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2023 Yunshan Networks | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package config | ||
|
||
import "time" | ||
|
||
type ExternalAPM struct { | ||
Name string `yaml:"name"` | ||
Addr string `yaml:"addr"` // e.g.: http://host:port | ||
Timeout time.Duration `default:"60s" yaml:"timeout"` | ||
TLS *TLSConfig `yaml:"tls_config"` | ||
ExtraConfig map[string]string `yaml:"extra_config"` | ||
} | ||
|
||
type TLSConfig struct { | ||
CAFile string `yaml:"ca-file"` | ||
CertFile string `yaml:"cert-file"` | ||
KeyFile string `yaml:"key-file"` | ||
Insecure bool `yaml:"insecure"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright (c) 2023 Yunshan Networks | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package tracing_adapter | ||
|
||
import ( | ||
"net/http" | ||
"sync" | ||
|
||
adapter_config "github.com/deepflowio/deepflow/server/querier/app/tracing-adapter/config" | ||
"github.com/deepflowio/deepflow/server/querier/app/tracing-adapter/model" | ||
"github.com/deepflowio/deepflow/server/querier/app/tracing-adapter/service" | ||
"github.com/deepflowio/deepflow/server/querier/common" | ||
"github.com/deepflowio/deepflow/server/querier/config" | ||
"github.com/gin-gonic/gin" | ||
"github.com/op/go-logging" | ||
) | ||
|
||
var ( | ||
log = logging.MustGetLogger("tracing-adapter") | ||
pipeline []adapterPipeline | ||
once sync.Once | ||
) | ||
|
||
type adapterPipeline struct { | ||
config adapter_config.ExternalAPM | ||
adpater service.TraceAdapter | ||
} | ||
|
||
func ensureAdapterConfig() { | ||
if config.Cfg == nil || len(config.Cfg.ExternalAPM) == 0 { | ||
return | ||
} | ||
once.Do(func() { | ||
if len(pipeline) == 0 { | ||
pipeline = make([]adapterPipeline, 0, len(config.Cfg.ExternalAPM)) | ||
} | ||
// build external trace pipelines | ||
for _, apm := range config.Cfg.ExternalAPM { | ||
if service.Adapters[apm.Name] == nil { | ||
continue | ||
} | ||
// allow multiple hosts configs | ||
log.Debugf("load adapter configs, name: %s, addr: %s", apm.Name, apm.Addr) | ||
pipeline = append(pipeline, adapterPipeline{apm, service.Adapters[apm.Name]}) | ||
} | ||
}) | ||
} | ||
|
||
func GetAdaptTrace(traceID string) (*model.ExTrace, error) { | ||
ensureAdapterConfig() | ||
result := &model.ExTrace{Spans: make([]model.ExSpan, 0)} | ||
if pipeline == nil { | ||
return result, nil | ||
} | ||
for _, v := range pipeline { | ||
if v.adpater == nil { | ||
continue | ||
} | ||
r, err := v.adpater.GetTrace(traceID, &v.config) | ||
if err != nil { | ||
log.Errorf("load %s data with traceid %s gets error: %s", v.config.Name, traceID, err) | ||
continue | ||
} | ||
if r != nil && len(r.Spans) > 0 { | ||
log.Debugf("load %s data with traceid %s get %d spans", v.config.Name, traceID, len(r.Spans)) | ||
result.Spans = append(result.Spans, r.Spans...) | ||
} | ||
} | ||
return result, nil | ||
} | ||
|
||
func TraceHandler() gin.HandlerFunc { | ||
return gin.HandlerFunc(func(ctx *gin.Context) { | ||
traceID := ctx.Query("traceid") | ||
result, err := GetAdaptTrace(traceID) | ||
if err != nil { | ||
ctx.JSON(http.StatusInternalServerError, model.ExTraceResponse{Error: err.Error(), Status: common.FAIL, Data: nil}) | ||
return | ||
} | ||
ctx.JSON(http.StatusOK, model.ExTraceResponse{Status: common.SUCCESS, Data: result}) | ||
}) | ||
} |
Oops, something went wrong.