forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jared Tan <[email protected]>
- Loading branch information
1 parent
bf7f46a
commit 5bb8a12
Showing
9 changed files
with
272 additions
and
8 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
24 changes: 24 additions & 0 deletions
24
processor/resourcedetectionprocessor/internal/http/config.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,24 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package http // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/http" | ||
import ( | ||
"go.opentelemetry.io/collector/config/confighttp" | ||
"go.opentelemetry.io/collector/config/configopaque" | ||
) | ||
|
||
type Config struct { | ||
// APIKey is the authentication token | ||
APIKey configopaque.String `mapstructure:"api_key"` | ||
|
||
// API URL to use | ||
APIURL string `mapstructure:"api_url"` | ||
|
||
confighttp.ClientConfig `mapstructure:",squash"` | ||
} | ||
|
||
func CreateDefaultConfig() Config { | ||
return Config{ | ||
APIURL: "http://localhost:8080", | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
processor/resourcedetectionprocessor/internal/http/http.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,98 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package http // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/http" | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/config/configopaque" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/processor" | ||
conventions "go.opentelemetry.io/collector/semconv/v1.22.0" | ||
"go.uber.org/zap" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal" | ||
) | ||
|
||
const ( | ||
// TypeStr is type of detector. | ||
TypeStr = "http" | ||
) | ||
|
||
type resourceAttribute struct { | ||
Key string | ||
Value string | ||
} | ||
|
||
var _ internal.Detector = (*detector)(nil) | ||
|
||
type detector struct { | ||
logger *zap.Logger | ||
// | ||
set component.TelemetrySettings | ||
interval time.Duration | ||
client *http.Client | ||
apiURL string | ||
apiKey configopaque.String | ||
requestIntervalTicker *time.Ticker | ||
} | ||
|
||
// NewDetector returns a detector which can detect resource attributes on Heroku | ||
func NewDetector(set processor.Settings, dcfg internal.DetectorConfig) (internal.Detector, error) { | ||
cfg := dcfg.(Config) | ||
|
||
if cfg.APIURL == "" { | ||
return nil, fmt.Errorf("apiUrl could not be empty") | ||
} | ||
|
||
return &detector{ | ||
apiKey: cfg.APIKey, | ||
apiURL: cfg.APIURL, | ||
logger: set.Logger, | ||
// TODO: interval request | ||
interval: time.Second * 5, | ||
}, nil | ||
} | ||
|
||
// Detect detects http response metadata and returns a resource with the available ones | ||
func (d detector) Detect(ctx context.Context) (resource pcommon.Resource, schemaURL string, err error) { | ||
res := pcommon.NewResource() | ||
|
||
detectedResources := d.requestResourceAttributes() | ||
|
||
for _, resAttr := range detectedResources { | ||
res.Attributes().PutStr(resAttr.Key, resAttr.Value) | ||
} | ||
|
||
return res, conventions.SchemaURL, nil | ||
} | ||
|
||
func (d detector) requestResourceAttributes() []resourceAttribute { | ||
var resources []resourceAttribute | ||
resp, err := http.Get(d.apiURL) | ||
if err != nil { | ||
d.logger.Warn("Failed to fetch resource", zap.Error(err)) | ||
return resources | ||
} | ||
|
||
defer resp.Body.Close() | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
d.logger.Warn("Failed to fetch resource", zap.Error(err)) | ||
return resources | ||
} | ||
|
||
err = jsoniter.Unmarshal(body, &resources) | ||
if err != nil { | ||
d.logger.Warn("Failed to fetch resource", zap.Error(err)) | ||
return resources | ||
} | ||
|
||
return resources | ||
} |
40 changes: 40 additions & 0 deletions
40
processor/resourcedetectionprocessor/internal/http/http_test.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,40 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/processor/processortest" | ||
conventions "go.opentelemetry.io/collector/semconv/v1.22.0" | ||
) | ||
|
||
type mockMetadata struct { | ||
mock.Mock | ||
} | ||
|
||
func TestDetect(t *testing.T) { | ||
handler := http.NotFound | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
handler(w, r) | ||
})) | ||
defer ts.Close() | ||
handler = func(w http.ResponseWriter, r *http.Request) { | ||
outPut := `[{"key":"attributes_1","value":"foo"},{"key":"attributes_2","value":"bar"}]` | ||
_, _ = w.Write([]byte(outPut)) | ||
} | ||
defaultCfg := CreateDefaultConfig() | ||
defaultCfg.APIURL = ts.URL | ||
|
||
d, err := NewDetector(processortest.NewNopSettings(), defaultCfg) | ||
require.NoError(t, err) | ||
res, schemaURL, err := d.Detect(context.Background()) | ||
require.NoError(t, err) | ||
require.Equal(t, 2, res.Attributes().Len()) | ||
require.NotNil(t, res) | ||
assert.Equal(t, conventions.SchemaURL, schemaURL) | ||
} |
8 changes: 8 additions & 0 deletions
8
processor/resourcedetectionprocessor/internal/http/metadata.yaml
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,8 @@ | ||
type: resourcedetectionprocessor/http | ||
|
||
parent: resourcedetection | ||
|
||
status: | ||
class: pkg | ||
codeowners: | ||
active: [JaredTan95] |
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
62 changes: 62 additions & 0 deletions
62
processor/resourcedetectionprocessor/testdata/otel-col-config.yaml
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,62 @@ | ||
receivers: | ||
otlp: | ||
protocols: | ||
grpc: | ||
endpoint: 0.0.0.0:4317 | ||
|
||
exporters: | ||
debug: | ||
verbosity: detailed | ||
sampling_initial: 5 | ||
sampling_thereafter: 200 | ||
otlp/jaeger: | ||
endpoint: localhost:14317 | ||
tls: | ||
insecure: true | ||
retry_on_failure: | ||
enabled: true | ||
max_elapsed_time: 500s | ||
sending_queue: | ||
enabled: true | ||
|
||
processors: | ||
batch: | ||
resourcedetection/sgm: | ||
detectors: [env,http] | ||
timeout: 2s | ||
override: false | ||
detect_interval: 10s | ||
http: | ||
api_url: https://apifoxmock.com/m1/3552517-2307922-default/mock/auto-tagging | ||
resourcedetection/system: | ||
detectors: [env, system] | ||
timeout: 2s | ||
override: false | ||
system: | ||
resource_attributes: | ||
host.name: | ||
enabled: true | ||
host.id: | ||
enabled: true | ||
os.type: | ||
enabled: true | ||
attributes: ["a", "b"] | ||
|
||
extensions: | ||
health_check: | ||
pprof: | ||
endpoint: :1888 | ||
zpages: | ||
endpoint: :55679 | ||
|
||
service: | ||
extensions: [pprof, zpages, health_check] | ||
pipelines: | ||
traces: | ||
receivers: [otlp] | ||
processors: [batch,resourcedetection/sgm] | ||
exporters: [debug,otlp/jaeger] | ||
metrics: | ||
receivers: [otlp] | ||
processors: [batch,resourcedetection/sgm] | ||
exporters: [debug] |