-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06ac688
commit 912c0ac
Showing
10 changed files
with
1,062 additions
and
7 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
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
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,201 @@ | ||
/* | ||
* Copyright (C) 2023 IBM, Inc. | ||
* | ||
* 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 opentelemetry | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
otel "github.com/agoda-com/opentelemetry-logs-go" | ||
"github.com/agoda-com/opentelemetry-logs-go/logs" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/api" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/config" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/operational" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/encode" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/test" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const testOtlpConfig = `--- | ||
log-level: debug | ||
pipeline: | ||
- name: encode1 | ||
parameters: | ||
- name: encode1 | ||
encode: | ||
type: otlplogs | ||
otlplogs: | ||
address: localhost | ||
port: 4317 | ||
connectionType: grpc | ||
` | ||
|
||
type fakeOltpLoggerProvider struct { | ||
} | ||
type fakeOltpLogger struct { | ||
} | ||
|
||
var receivedData []logs.LogRecord | ||
|
||
func (f *fakeOltpLogger) Emit(msg logs.LogRecord) { | ||
receivedData = append(receivedData, msg) | ||
} | ||
|
||
func (f *fakeOltpLoggerProvider) Logger(name string, options ...logs.LoggerOption) logs.Logger { | ||
return &fakeOltpLogger{} | ||
} | ||
|
||
func initNewEncodeOtlpLogs(t *testing.T) encode.Encoder { | ||
receivedData = []logs.LogRecord{} | ||
v, cfg := test.InitConfig(t, testOtlpConfig) | ||
require.NotNil(t, v) | ||
|
||
newEncode, err := NewEncodeOtlpLogs(operational.NewMetrics(&config.MetricsSettings{}), cfg.Parameters[0]) | ||
require.NoError(t, err) | ||
|
||
// intercept the loggerProvider function | ||
loggerProvider := fakeOltpLoggerProvider{} | ||
otel.SetLoggerProvider(&loggerProvider) | ||
return newEncode | ||
} | ||
|
||
func Test_EncodeOtlpLogs(t *testing.T) { | ||
newEncode := initNewEncodeOtlpLogs(t) | ||
encodeOtlp := newEncode.(*EncodeOtlpLogs) | ||
require.Equal(t, "localhost", encodeOtlp.cfg.OtlpConnectionInfo.Address) | ||
require.Equal(t, 4317, encodeOtlp.cfg.OtlpConnectionInfo.Port) | ||
require.Equal(t, "grpc", encodeOtlp.cfg.ConnectionType) | ||
require.Nil(t, encodeOtlp.cfg.TLS) | ||
require.Empty(t, encodeOtlp.cfg.Headers) | ||
|
||
entry1 := test.GetIngestMockEntry(true) | ||
entry2 := test.GetIngestMockEntry(false) | ||
newEncode.Encode(entry1) | ||
newEncode.Encode(entry2) | ||
// verify contents of the output | ||
require.Len(t, receivedData, 2) | ||
expected1, _ := json.Marshal(entry1) | ||
expected2, _ := json.Marshal(entry2) | ||
require.Equal(t, string(expected1), *receivedData[0].Body()) | ||
require.Equal(t, string(expected2), *receivedData[1].Body()) | ||
} | ||
|
||
func Test_TLSConfigEmpty(t *testing.T) { | ||
cfg := config.StageParam{ | ||
Encode: &config.Encode{ | ||
OtlpLogs: &api.EncodeOtlpLogs{OtlpConnectionInfo: &api.OtlpConnectionInfo{ | ||
Address: "1.2.3.4", | ||
Port: 999, | ||
TLS: nil, | ||
ConnectionType: "grpc", | ||
Headers: nil, | ||
}}}, | ||
} | ||
newEncode, err := NewEncodeOtlpLogs(operational.NewMetrics(&config.MetricsSettings{}), cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, newEncode) | ||
} | ||
|
||
func Test_TLSConfigNotEmpty(t *testing.T) { | ||
cfg := config.StageParam{ | ||
Encode: &config.Encode{ | ||
OtlpLogs: &api.EncodeOtlpLogs{OtlpConnectionInfo: &api.OtlpConnectionInfo{ | ||
Address: "1.2.3.4", | ||
Port: 999, | ||
ConnectionType: "grpc", | ||
TLS: &api.ClientTLS{InsecureSkipVerify: true}, | ||
}}}, | ||
} | ||
newEncode, err := NewEncodeOtlpLogs(operational.NewMetrics(&config.MetricsSettings{}), cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, newEncode) | ||
} | ||
|
||
func Test_HeadersNotEmpty(t *testing.T) { | ||
headers := make(map[string]string) | ||
headers["key1"] = "value1" | ||
headers["key2"] = "value2" | ||
cfg := config.StageParam{ | ||
Encode: &config.Encode{ | ||
OtlpLogs: &api.EncodeOtlpLogs{OtlpConnectionInfo: &api.OtlpConnectionInfo{ | ||
Address: "1.2.3.4", | ||
Port: 999, | ||
ConnectionType: "grpc", | ||
Headers: headers, | ||
}}}, | ||
} | ||
newEncode, err := NewEncodeOtlpLogs(operational.NewMetrics(&config.MetricsSettings{}), cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, newEncode) | ||
} | ||
|
||
func Test_NoConnectionType(t *testing.T) { | ||
cfg := config.StageParam{ | ||
Encode: &config.Encode{ | ||
OtlpLogs: &api.EncodeOtlpLogs{OtlpConnectionInfo: &api.OtlpConnectionInfo{ | ||
Address: "1.2.3.4", | ||
Port: 999, | ||
}}}, | ||
} | ||
newEncode, err := NewEncodeOtlpLogs(operational.NewMetrics(&config.MetricsSettings{}), cfg) | ||
require.Error(t, err) | ||
require.Nil(t, newEncode) | ||
} | ||
|
||
func Test_EncodeOtlpTraces(t *testing.T) { | ||
cfg := config.StageParam{ | ||
Encode: &config.Encode{ | ||
OtlpTraces: &api.EncodeOtlpTraces{OtlpConnectionInfo: &api.OtlpConnectionInfo{ | ||
Address: "1.2.3.4", | ||
Port: 999, | ||
ConnectionType: "grpc", | ||
Headers: nil, | ||
}}}, | ||
} | ||
newEncode, err := NewEncodeOtlpTraces(operational.NewMetrics(&config.MetricsSettings{}), cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, newEncode) | ||
} | ||
|
||
func Test_EncodeOtlpMetrics(t *testing.T) { | ||
cfg := config.StageParam{ | ||
Encode: &config.Encode{ | ||
OtlpMetrics: &api.EncodeOtlpMetrics{ | ||
OtlpConnectionInfo: &api.OtlpConnectionInfo{ | ||
Address: "1.2.3.4", | ||
Port: 999, | ||
ConnectionType: "grpc", | ||
Headers: nil, | ||
}, | ||
Prefix: "flp_test", | ||
Metrics: []api.PromMetricsItem{ | ||
{Name: "metric1", Type: "counter", Labels: []string{"label11", "label12"}}, | ||
{Name: "metric2", Type: "gauge", Labels: []string{"label21", "label22"}}, | ||
{Name: "metric3", Type: "counter", Labels: []string{"label31", "label32"}}, | ||
}, | ||
}}, | ||
} | ||
newEncode, err := NewEncodeOtlpMetrics(operational.NewMetrics(&config.MetricsSettings{}), cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, newEncode) | ||
em := newEncode.(*EncodeOtlpMetrics) | ||
require.Equal(t, 2, len(em.counters)) | ||
require.Equal(t, 1, len(em.gauges)) | ||
require.Equal(t, "metric3", em.counters[1].info.Name) | ||
require.Equal(t, []string{"label21", "label22"}, em.gauges[0].info.Labels) | ||
} |
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,68 @@ | ||
/* | ||
* Copyright (C) 2023 IBM, Inc. | ||
* | ||
* 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 opentelemetry | ||
|
||
import ( | ||
"context" | ||
|
||
sdklog "github.com/agoda-com/opentelemetry-logs-go/sdk/logs" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/api" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/config" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/operational" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/encode" | ||
log "github.com/sirupsen/logrus" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
) | ||
|
||
type EncodeOtlpLogs struct { | ||
cfg api.EncodeOtlpLogs | ||
ctx context.Context | ||
res *resource.Resource | ||
lp *sdklog.LoggerProvider | ||
} | ||
|
||
// Encode encodes a log entry to be exported | ||
func (e *EncodeOtlpLogs) Encode(entry config.GenericMap) { | ||
log.Tracef("entering EncodeOtlpLogs. entry = %v", entry) | ||
e.LogWrite(entry) | ||
} | ||
|
||
func NewEncodeOtlpLogs(opMetrics *operational.Metrics, params config.StageParam) (encode.Encoder, error) { | ||
log.Tracef("entering NewEncodeOtlpLogs \n") | ||
cfg := api.EncodeOtlpLogs{} | ||
if params.Encode != nil && params.Encode.OtlpLogs != nil { | ||
cfg = *params.Encode.OtlpLogs | ||
} | ||
log.Debugf("NewEncodeOtlpLogs cfg = %v \n", cfg) | ||
|
||
ctx := context.Background() | ||
res := newResource() | ||
|
||
lp, err := NewOtlpLoggerProvider(ctx, params, res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
w := &EncodeOtlpLogs{ | ||
cfg: cfg, | ||
ctx: ctx, | ||
res: res, | ||
lp: lp, | ||
} | ||
return w, nil | ||
} |
Oops, something went wrong.