Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/feature_UpdateLibraryVersions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: azuredataexplorerexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Update Azure Data Explorer Exporter to use the latest version of the Azure SDK for Go."

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [39279]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
85 changes: 39 additions & 46 deletions exporter/azuredataexplorerexporter/adx_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (
"strconv"
"strings"

"github.com/Azure/azure-kusto-go/kusto"
kustoerrors "github.com/Azure/azure-kusto-go/kusto/data/errors"
"github.com/Azure/azure-kusto-go/kusto/ingest"
"github.com/Azure/azure-kusto-go/kusto/ingest/ingestoptions"
"github.com/Azure/azure-kusto-go/azkustodata"
"github.com/Azure/azure-kusto-go/azkustoingest"
"github.com/Azure/azure-kusto-go/azkustoingest/ingestoptions"
jsoniter "github.com/json-iterator/go"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
Expand All @@ -23,10 +22,9 @@ import (

// adxDataProducer uses the ADX client to perform ingestion
type adxDataProducer struct {
client *kusto.Client // client for logs, traces and metrics
ingestor ingest.Ingestor // ingestion for logs, traces and metrics
ingestOptions []ingest.FileOption // options for the ingestion
logger *zap.Logger // logger for tracing the flow
ingestor azkustoingest.Ingestor // ingestion for logs, traces and metrics
ingestOptions []azkustoingest.FileOption // options for the ingestion
logger *zap.Logger // logger for tracing the flow
}

const nextline = "\n"
Expand Down Expand Up @@ -127,13 +125,8 @@ func (e *adxDataProducer) tracesDataPusher(_ context.Context, traceData ptrace.T
}

func (e *adxDataProducer) Close(context.Context) error {
var err error

err = e.ingestor.Close()

if clientErr := e.client.Close(); clientErr != nil {
err = kustoerrors.GetCombinedError(err, clientErr)
}
// Close the ingestor and client connections
err := e.ingestor.Close()
if err != nil {
e.logger.Warn("Error closing connections", zap.Error(err))
} else {
Expand All @@ -149,94 +142,94 @@ func newExporter(config *Config, logger *zap.Logger, telemetryDataType int, vers
if err != nil {
return nil, err
}
metricClient, err := buildAdxClient(config, version)
if err != nil {
return nil, err
}

var ingestor ingest.Ingestor
var ingestor azkustoingest.Ingestor

var ingestOptions []ingest.FileOption
ingestOptions = append(ingestOptions, ingest.FileFormat(ingest.JSON))
ingestOptions = append(ingestOptions, ingest.CompressionType(ingestoptions.GZIP))
var ingestOptions []azkustoingest.FileOption
ingestOptions = append(ingestOptions, azkustoingest.FileFormat(azkustoingest.JSON))
ingestOptions = append(ingestOptions, azkustoingest.CompressionType(ingestoptions.GZIP))
// Expect that this mapping is already existent
if refOption := getMappingRef(config, telemetryDataType); refOption != nil {
ingestOptions = append(ingestOptions, refOption)
}
// The exporter could be configured to run in either modes. Using managedstreaming or batched queueing
if strings.ToLower(config.IngestionType) == managedIngestType {
mi, err := createManagedStreamingIngestor(config, metricClient, tableName)
mi, err := createManagedStreamingIngestor(config, version, tableName)
if err != nil {
return nil, err
}
ingestor = mi
} else {
qi, err := createQueuedIngestor(config, metricClient, tableName)
qi, err := createQueuedIngestor(config, version, tableName)
if err != nil {
return nil, err
}
ingestor = qi
}
return &adxDataProducer{
client: metricClient,
ingestOptions: ingestOptions,
ingestor: ingestor,
logger: logger,
}, nil
}

// Fetches the corresponding ingestionRef if the mapping is provided
func getMappingRef(config *Config, telemetryDataType int) ingest.FileOption {
func getMappingRef(config *Config, telemetryDataType int) azkustoingest.FileOption {
switch telemetryDataType {
case metricsType:
if !isEmpty(config.MetricTableMapping) {
return ingest.IngestionMappingRef(config.MetricTableMapping, ingest.JSON)
return azkustoingest.IngestionMappingRef(config.MetricTableMapping, azkustoingest.JSON)
}
case tracesType:
if !isEmpty(config.TraceTableMapping) {
return ingest.IngestionMappingRef(config.TraceTableMapping, ingest.JSON)
return azkustoingest.IngestionMappingRef(config.TraceTableMapping, azkustoingest.JSON)
}
case logsType:
if !isEmpty(config.LogTableMapping) {
return ingest.IngestionMappingRef(config.LogTableMapping, ingest.JSON)
return azkustoingest.IngestionMappingRef(config.LogTableMapping, azkustoingest.JSON)
}
}
return nil
}

func buildAdxClient(config *Config, version string) (*kusto.Client, error) {
client, err := kusto.New(createKcsb(config, version))
return client, err
}

func createKcsb(config *Config, version string) *kusto.ConnectionStringBuilder {
var kcsb *kusto.ConnectionStringBuilder
func createKcsb(config *Config, version string) *azkustodata.ConnectionStringBuilder {
var kcsb *azkustodata.ConnectionStringBuilder
isManagedIdentity := len(strings.TrimSpace(config.ManagedIdentityID)) > 0
isSystemManagedIdentity := strings.EqualFold(strings.TrimSpace(config.ManagedIdentityID), "SYSTEM")
// If the user has managed identity done, use it. For System managed identity use the MI as system
switch {
case config.UseAzureAuth:
kcsb = kusto.NewConnectionStringBuilder(config.ClusterURI).WithDefaultAzureCredential()
kcsb = azkustodata.NewConnectionStringBuilder(config.ClusterURI).WithDefaultAzureCredential()
case !isManagedIdentity:
kcsb = kusto.NewConnectionStringBuilder(config.ClusterURI).WithAadAppKey(config.ApplicationID, string(config.ApplicationKey), config.TenantID)
kcsb = azkustodata.NewConnectionStringBuilder(config.ClusterURI).WithAadAppKey(config.ApplicationID, string(config.ApplicationKey), config.TenantID)
case isManagedIdentity && isSystemManagedIdentity:
kcsb = kusto.NewConnectionStringBuilder(config.ClusterURI).WithSystemManagedIdentity()
kcsb = azkustodata.NewConnectionStringBuilder(config.ClusterURI).WithSystemManagedIdentity()
case isManagedIdentity && !isSystemManagedIdentity:
kcsb = kusto.NewConnectionStringBuilder(config.ClusterURI).WithUserManagedIdentity(config.ManagedIdentityID)
kcsb = azkustodata.NewConnectionStringBuilder(config.ClusterURI).WithUserAssignedIdentityClientId(config.ManagedIdentityID)
}
kcsb.SetConnectorDetails("OpenTelemetry", version, "", "", false, "", kusto.StringPair{Key: "isManagedIdentity", Value: strconv.FormatBool(isManagedIdentity)})
kcsb.SetConnectorDetails("OpenTelemetry", version, "", "", false, "", azkustodata.StringPair{Key: "isManagedIdentity", Value: strconv.FormatBool(isManagedIdentity)})
return kcsb
}

// Depending on the table, create separate ingestors
func createManagedStreamingIngestor(config *Config, adxclient *kusto.Client, tablename string) (*ingest.Managed, error) {
ingestor, err := ingest.NewManaged(adxclient, config.Database, tablename)
func createManagedStreamingIngestor(config *Config, version string, tablename string) (*azkustoingest.Managed, error) {
kcsb := createKcsb(config, version)
ingestopts := []azkustoingest.Option{
azkustoingest.WithDefaultDatabase(config.Database),
azkustoingest.WithDefaultTable(tablename),
}
ingestor, err := azkustoingest.NewManaged(kcsb, ingestopts...)
return ingestor, err
}

// A queued ingestor in case that is provided as the config option
func createQueuedIngestor(config *Config, adxclient *kusto.Client, tablename string) (*ingest.Ingestion, error) {
ingestor, err := ingest.New(adxclient, config.Database, tablename)
func createQueuedIngestor(config *Config, version string, tablename string) (*azkustoingest.Ingestion, error) {
kcsb := createKcsb(config, version)
ingestopts := []azkustoingest.Option{
azkustoingest.WithDefaultDatabase(config.Database),
azkustoingest.WithDefaultTable(tablename),
}
ingestor, err := azkustoingest.New(kcsb, ingestopts...)
return ingestor, err
}

Expand Down
76 changes: 40 additions & 36 deletions exporter/azuredataexplorerexporter/adx_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"testing"
"time"

"github.com/Azure/azure-kusto-go/kusto"
"github.com/Azure/azure-kusto-go/kusto/ingest"
"github.com/Azure/azure-kusto-go/azkustodata"
"github.com/Azure/azure-kusto-go/azkustoingest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -58,15 +58,29 @@ func TestNewExporter(t *testing.T) {
assert.Nil(t, fexp)
}

func getKcsb() *azkustodata.ConnectionStringBuilder {
return createKcsb(&Config{
ClusterURI: "https://CLUSTER.kusto.windows.net",
ApplicationID: "unknown",
ApplicationKey: "unknown",
TenantID: "unknown",
Database: "not-configured",
MetricTable: "OTELMetrics",
LogTable: "OTELLogs",
TraceTable: "OTELTraces",
MetricTableMapping: "otelmetrics_mapping",
LogTableMapping: "otellogs_mapping",
TraceTableMapping: "oteltraces_mapping",
}, "1.0.0")
}

func TestMetricsDataPusherStreaming(t *testing.T) {
logger := zaptest.NewLogger(t)
kustoClient := kusto.NewMockClient()
var ingestOptions []ingest.FileOption
ingestOptions = append(ingestOptions, ingest.FileFormat(ingest.JSON))
managedStreamingIngest, _ := ingest.NewManaged(kustoClient, "testDB", "OTELMetrics")
var ingestOptions []azkustoingest.FileOption
ingestOptions = append(ingestOptions, azkustoingest.FileFormat(azkustoingest.JSON))
managedStreamingIngest, _ := azkustoingest.NewManaged(getKcsb(), azkustoingest.WithDefaultDatabase("testDB"), azkustoingest.WithDefaultTable("OTELMetrics"))

adxDataProducer := &adxDataProducer{
client: kustoClient,
ingestor: managedStreamingIngest,
ingestOptions: ingestOptions,
logger: logger,
Expand All @@ -79,13 +93,11 @@ func TestMetricsDataPusherStreaming(t *testing.T) {

func TestMetricsDataPusherQueued(t *testing.T) {
logger := zaptest.NewLogger(t)
kustoClient := kusto.NewMockClient()
var ingestOptions []ingest.FileOption
ingestOptions = append(ingestOptions, ingest.FileFormat(ingest.JSON))
queuedingest, _ := ingest.New(kustoClient, "testDB", "OTELMetrics")
var ingestOptions []azkustoingest.FileOption
ingestOptions = append(ingestOptions, azkustoingest.FileFormat(azkustoingest.JSON))
queuedingest, _ := azkustoingest.New(getKcsb(), azkustoingest.WithDefaultDatabase("testDB"), azkustoingest.WithDefaultTable("OTELMetrics"))

adxDataProducer := &adxDataProducer{
client: kustoClient,
ingestor: queuedingest,
ingestOptions: ingestOptions,
logger: logger,
Expand All @@ -98,13 +110,11 @@ func TestMetricsDataPusherQueued(t *testing.T) {

func TestLogsDataPusher(t *testing.T) {
logger := zaptest.NewLogger(t)
kustoClient := kusto.NewMockClient()
var ingestOptions []ingest.FileOption
ingestOptions = append(ingestOptions, ingest.FileFormat(ingest.JSON))
managedStreamingIngest, _ := ingest.NewManaged(kustoClient, "testDB", "OTELLogs")
var ingestOptions []azkustoingest.FileOption
ingestOptions = append(ingestOptions, azkustoingest.FileFormat(azkustoingest.JSON))
managedStreamingIngest, _ := azkustoingest.NewManaged(getKcsb(), azkustoingest.WithDefaultDatabase("testDB"), azkustoingest.WithDefaultTable("OTELLogs"))

adxDataProducer := &adxDataProducer{
client: kustoClient,
ingestor: managedStreamingIngest,
ingestOptions: ingestOptions,
logger: logger,
Expand All @@ -117,13 +127,11 @@ func TestLogsDataPusher(t *testing.T) {

func TestTracesDataPusher(t *testing.T) {
logger := zaptest.NewLogger(t)
kustoClient := kusto.NewMockClient()
var ingestOptions []ingest.FileOption
ingestOptions = append(ingestOptions, ingest.FileFormat(ingest.JSON))
managedStreamingIngest, _ := ingest.NewManaged(kustoClient, "testDB", "OTELLogs")
var ingestOptions []azkustoingest.FileOption
ingestOptions = append(ingestOptions, azkustoingest.FileFormat(azkustoingest.JSON))
managedStreamingIngest, _ := azkustoingest.NewManaged(getKcsb(), azkustoingest.WithDefaultDatabase("testDB"), azkustoingest.WithDefaultTable("OTELLogs"))

adxDataProducer := &adxDataProducer{
client: kustoClient,
ingestor: managedStreamingIngest,
ingestOptions: ingestOptions,
logger: logger,
Expand All @@ -136,13 +144,11 @@ func TestTracesDataPusher(t *testing.T) {

func TestClose(t *testing.T) {
logger := zaptest.NewLogger(t)
kustoClient := kusto.NewMockClient()
var ingestOptions []ingest.FileOption
ingestOptions = append(ingestOptions, ingest.FileFormat(ingest.JSON))
managedStreamingIngest, _ := ingest.NewManaged(kustoClient, "testDB", "OTELMetrics")
var ingestOptions []azkustoingest.FileOption
ingestOptions = append(ingestOptions, azkustoingest.FileFormat(azkustoingest.JSON))
managedStreamingIngest, _ := azkustoingest.NewManaged(getKcsb(), azkustoingest.WithDefaultDatabase("testDB"), azkustoingest.WithDefaultTable("OTELMetrics"))

adxDataProducer := &adxDataProducer{
client: kustoClient,
ingestor: managedStreamingIngest,
ingestOptions: ingestOptions,
logger: logger,
Expand All @@ -153,13 +159,11 @@ func TestClose(t *testing.T) {

func TestIngestedDataRecordCount(t *testing.T) {
logger := zaptest.NewLogger(t)
kustoClient := kusto.NewMockClient()
ingestOptions := make([]ingest.FileOption, 2)
ingestOptions[0] = ingest.FileFormat(ingest.JSON)
ingestOptions := make([]azkustoingest.FileOption, 2)
ingestOptions[0] = azkustoingest.FileFormat(azkustoingest.JSON)
ingestor := &mockingestor{}

adxDataProducer := &adxDataProducer{
client: kustoClient,
ingestor: ingestor,
ingestOptions: ingestOptions,
logger: logger,
Expand Down Expand Up @@ -235,7 +239,7 @@ func TestCreateKcsb(t *testing.T) {
wantIsMsi := tt.isMsi
assert.Equal(t, wantIsMsi, gotKcsb.MsiAuthentication)
wantManagedID := tt.managedIdentityID
assert.Equal(t, wantManagedID, gotKcsb.ManagedServiceIdentity)
assert.Equal(t, wantManagedID, gotKcsb.ManagedServiceIdentityClientId)
assert.Equal(t, "https://CLUSTER.kusto.windows.net", gotKcsb.DataSource)
wantIsAzure := tt.isAzureAuth
assert.Equal(t, wantIsAzure, gotKcsb.DefaultAuth)
Expand All @@ -247,15 +251,15 @@ type mockingestor struct {
records []string
}

func (m *mockingestor) FromReader(_ context.Context, reader io.Reader, _ ...ingest.FileOption) (*ingest.Result, error) {
func (m *mockingestor) FromReader(_ context.Context, reader io.Reader, _ ...azkustoingest.FileOption) (*azkustoingest.Result, error) {
bufbytes, _ := io.ReadAll(reader)
metricjson := string(bufbytes)
m.SetRecords(strings.Split(metricjson, "\n"))
return &ingest.Result{}, nil
return &azkustoingest.Result{}, nil
}

func (m *mockingestor) FromFile(_ context.Context, _ string, _ ...ingest.FileOption) (*ingest.Result, error) {
return &ingest.Result{}, nil
func (m *mockingestor) FromFile(_ context.Context, _ string, _ ...azkustoingest.FileOption) (*azkustoingest.Result, error) {
return &azkustoingest.Result{}, nil
}

func (m *mockingestor) SetRecords(records []string) {
Expand Down
Loading
Loading