Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

Commit

Permalink
add alias to data collectors
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Jul 2, 2021
1 parent 6bc26d9 commit 40a69c1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
60 changes: 54 additions & 6 deletions client/env/traceconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package env

import (
"encoding/json"
"fmt"
"net/url"
"path"
Expand All @@ -23,6 +24,17 @@ import (
"github.com/srinandan/apigeecli/apiclient"
)

type traceCfg struct {
Exporter string `json:"exporter,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
SamplingConfig samplingCfg `json:"samplingConfig,omitempty"`
}

type samplingCfg struct {
Sampler string `json:"sampler,omitempty"`
SamplingRate float64 `json:"samplingRate,omitempty"`
}

//GetTraceConfig
func GetTraceConfig() (respBody []byte, err error) {
u, _ := url.Parse(apiclient.BaseURL)
Expand All @@ -47,12 +59,7 @@ func UpdateTraceConfig(exporter string, endpoint string, sampler string, sample_
traceConfig = append(traceConfig, "\"endpoint\":\""+endpoint+"\"")
}

sampling := []string{}
sampling = append(sampling, "\"sampler\":\""+sampler+"\"")
sampling = append(sampling, "\"sampling_rate\":"+sample_rate)
sampling_config := "\"sampling_config\":{" + strings.Join(sampling, ",") + "}"

traceConfig = append(traceConfig, sampling_config)
traceConfig = append(traceConfig, getSamplingConfig(sampler, sample_rate))

payload := "{" + strings.Join(traceConfig, ",") + "}"

Expand All @@ -61,3 +68,44 @@ func UpdateTraceConfig(exporter string, endpoint string, sampler string, sample_
respBody, err = apiclient.HttpClient(apiclient.GetPrintOutput(), u.String(), payload, "PATCH")
return respBody, err
}

func DisableTraceConfig() (respBody []byte, err error) {

var traceRespBody []byte
var payload []byte

apiclient.SetPrintOutput(false)
if traceRespBody, err = GetTraceConfig(); err != nil {
return nil, err
}
apiclient.SetPrintOutput(true)

traceResp := traceCfg{}
if err = json.Unmarshal(traceRespBody, &traceResp); err != nil {
return nil, err
}

if traceResp.Exporter != "CLOUD_TRACE" && traceResp.Exporter != "JAEGER" {
return nil, fmt.Errorf("Distributed trace not configured for the environment")
}

//disable trace
traceResp.SamplingConfig.Sampler = "OFF"

if payload, err = json.Marshal(traceResp); err != nil {
return nil, err
}

u, _ := url.Parse(apiclient.BaseURL)
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "environments", apiclient.GetApigeeEnv(), "traceConfig")
respBody, err = apiclient.HttpClient(apiclient.GetPrintOutput(), u.String(), string(payload), "PATCH")
return respBody, err
}

func getSamplingConfig(sampler string, sample_rate string) (sampling_config string) {
sampling := []string{}
sampling = append(sampling, "\"sampler\":\""+sampler+"\"")
sampling = append(sampling, "\"sampling_rate\":"+sample_rate)
sampling_config = "\"sampling_config\":{" + strings.Join(sampling, ",") + "}"
return sampling_config
}
7 changes: 4 additions & 3 deletions cmd/datacollectors/datacollectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (

//Cmd to manage datacollectors
var Cmd = &cobra.Command{
Use: "datacollectors",
Short: "Manage Apigee datacollectors entities",
Long: "Manage Apigee datacollectors entities",
Use: "datacollectors",
Aliases: []string{"dc"},
Short: "Manage Apigee datacollectors entities",
Long: "Manage Apigee datacollectors entities",
}

var org, name string
Expand Down

0 comments on commit 40a69c1

Please sign in to comment.