Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exporter/azureblobexporter Second PR #12419

Closed
Closed
Show file tree
Hide file tree
Changes from 16 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@

### 🚀 New components 🚀

- `azureblobexporter`: exports logs and traces to Azure Blob Storage
eedorenko marked this conversation as resolved.
Show resolved Hide resolved

## v0.45.1

### 💡 Enhancements 💡
Expand Down Expand Up @@ -253,7 +255,7 @@

- `clickhouse` exporter: Add ClickHouse Exporter (#6907)
- `pkg/translator/signalfx`: Extract signalfx to metrics conversion in a separate package (#7778)
- Extract FromMetrics to SignalFx translator package (#7823)
- Extract FromMetrics to SignalFx translator package (#7823)

## v0.44.0

Expand Down
1 change: 1 addition & 0 deletions exporter/azureblobexporter/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
24 changes: 24 additions & 0 deletions exporter/azureblobexporter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Azure Blob Exporter
eedorenko marked this conversation as resolved.
Show resolved Hide resolved

This exporter saves logs and trace data to [Azure Blob Storage](https://azure.microsoft.com/services/storage/blobs/).

## Configuration

The following settings are required:

- `connection_string` (no default): Azure Blob Storage connection key, which can be found in the Azure Blob Storage resource on the Azure Portal.

The following settings can be optionally configured:

- `logs_container_name` (default = "logs"): Name of the container where the exporter saves blobs with logs.
- `traces_container_name` (default = "traces"): Name of the container where the exporter saves blobs with traces.

Example:

```yaml
exporters:
azureblob:
connection_string: DefaultEndpointsProtocol=https;AccountName=accountName;AccountKey=+idLkHYcL0MUWIKYHm2j4Q==;EndpointSuffix=core.windows.net
```


82 changes: 82 additions & 0 deletions exporter/azureblobexporter/blobclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright OpenTelemetry Authors
//
// 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 azureblobexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/azureblobexporter"

import (
"bytes"
"context"
"fmt"
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/google/uuid"
"go.opentelemetry.io/collector/config"
"go.uber.org/zap"
)

type BlobClient interface {
UploadData(ctx context.Context, data []byte, dataType config.DataType) error
}

type AzureBlobClient struct {
containerClient azblob.ContainerClient
logger *zap.Logger
}

const (
containerNotFoundError = "ErrorCode=ContainerNotFound"
)

func (bc *AzureBlobClient) generateBlobName(dataType config.DataType) string {
return fmt.Sprintf("%s-%s", dataType, uuid.NewString())
}

func (bc *AzureBlobClient) checkOrCreateContainer() error {
_, err := bc.containerClient.GetProperties(context.TODO(), nil)
if err != nil && strings.Contains(err.Error(), containerNotFoundError) {
_, err = bc.containerClient.Create(context.TODO(), nil)
}
return err
}

func (bc *AzureBlobClient) UploadData(ctx context.Context, data []byte, dataType config.DataType) error {
blobName := bc.generateBlobName(dataType)

blockBlob := bc.containerClient.NewBlockBlobClient(blobName)

err := bc.checkOrCreateContainer()
if err != nil {
return err
}

_, err = blockBlob.Upload(ctx, streaming.NopCloser(bytes.NewReader(data)), nil)

return err
}

func NewBlobClient(connectionString string, containerName string, logger *zap.Logger) (*AzureBlobClient, error) {
serviceClient, err := azblob.NewServiceClientFromConnectionString(connectionString, nil)
if err != nil {
return nil, err
}

containerClient := serviceClient.NewContainerClient(containerName)

return &AzureBlobClient{
containerClient,
logger,
}, nil
}
66 changes: 66 additions & 0 deletions exporter/azureblobexporter/blobclient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright OpenTelemetry Authors
//
// 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 azureblobexporter

import (
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config"
"go.uber.org/zap/zaptest"
)

const (
goodConnectionString = "DefaultEndpointsProtocol=https;AccountName=accountName;AccountKey=+idLkHYcL0MUWIKYHm2j4Q==;EndpointSuffix=core.windows.net"
badConnectionString = "DefaultEndpointsProtocol=https;AccountName=accountName;AccountKey=accountkey;EndpointSuffix=core.windows.net"
)

func TestNewBlobClient(t *testing.T) {
blobClient, err := NewBlobClient(goodConnectionString, logsContainerName, zaptest.NewLogger(t))

require.Nil(t, err)
require.NotNil(t, blobClient)
assert.NotNil(t, blobClient.containerClient)
}

func TestNewBlobClientError(t *testing.T) {
blobClient, err := NewBlobClient(badConnectionString, logsContainerName, zaptest.NewLogger(t))

assert.NotNil(t, err)
assert.Nil(t, blobClient)
}

func TestGenerateBlobName(t *testing.T) {
blobClient, err := NewBlobClient(goodConnectionString, logsContainerName, zaptest.NewLogger(t))
require.Nil(t, err)

blobName := blobClient.generateBlobName(config.LogsDataType)
assert.True(t, strings.Contains(blobName, fmt.Sprintf("%s-", config.LogsDataType)))
}

func TestCheckOrCreateContainer(t *testing.T) {
blobClient, err := NewBlobClient(goodConnectionString, logsContainerName, zaptest.NewLogger(t))
require.Nil(t, err)

err = blobClient.checkOrCreateContainer()

assert.NotNil(t, err)

assert.False(t, strings.Contains(err.Error(), containerNotFoundError))

}
27 changes: 27 additions & 0 deletions exporter/azureblobexporter/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright OpenTelemetry Authors
//
// 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 azureblobexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/azureblobexporter"

import (
"go.opentelemetry.io/collector/config"
)

// Config defines configuration for Azure Monitor
type Config struct {
config.ExporterSettings `mapstructure:",squash"`
ConnectionString string `mapstructure:"connection_string"`
LogsContainerName string `mapstructure:"logs_container_name"`
TracesContainerName string `mapstructure:"traces_container_name"`
}
56 changes: 56 additions & 0 deletions exporter/azureblobexporter/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright OpenTelemetry Authors
//
// 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 azureblobexporter

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtest"
"go.opentelemetry.io/collector/service/servicetest"
)

func TestLoadConfig(t *testing.T) {
factories, err := componenttest.NopFactories()
assert.Nil(t, err)

factory := NewFactory()
factories.Exporters[typeStr] = factory
cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)

require.NoError(t, err)
require.NotNil(t, cfg)

assert.Equal(t, len(cfg.Exporters), 2)

exporter := cfg.Exporters[config.NewComponentID(typeStr)]
assert.Equal(t, factory.CreateDefaultConfig(), exporter)

exporter = cfg.Exporters[config.NewComponentIDWithName(typeStr, "2")].(*Config)
assert.NoError(t, configtest.CheckConfigStruct(exporter))
assert.Equal(
t,
&Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentIDWithName(typeStr, "2")),
ConnectionString: goodConnectionString,
LogsContainerName: logsContainerName,
TracesContainerName: tracesContainerName,
},
exporter)
}
92 changes: 92 additions & 0 deletions exporter/azureblobexporter/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright OpenTelemetry Authors
//
// 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 azureblobexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/azureblobexporter"

import (
"context"
"errors"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/exporter/exporterhelper"
)

const (
// The value of "type" key in configuration.
typeStr = "azureblob"
logsContainerName = "logs"
tracesContainerName = "traces"
)

var (
errUnexpectedConfigurationType = errors.New("failed to cast configuration to Azure Blob Config")
)

// NewFactory returns a factory for Azure Blob exporter.
func NewFactory() component.ExporterFactory {
return exporterhelper.NewFactory(
typeStr,
createDefaultConfig,
exporterhelper.WithTraces(createTracesExporter),
exporterhelper.WithLogs(createLogsExporter))
}

func createDefaultConfig() config.Exporter {
return &Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)),
LogsContainerName: logsContainerName,
TracesContainerName: tracesContainerName,
}
}

func createTracesExporter(
ctx context.Context,
set component.ExporterCreateSettings,
cfg config.Exporter,
) (component.TracesExporter, error) {
exporterConfig, ok := cfg.(*Config)

if !ok {
return nil, errUnexpectedConfigurationType
}

bc, err := NewBlobClient(exporterConfig.ConnectionString, exporterConfig.TracesContainerName, set.Logger)
if err != nil {
set.Logger.Error(err.Error())
return nil, err
}

return newTracesExporter(exporterConfig, bc, set)
}

func createLogsExporter(
ctx context.Context,
set component.ExporterCreateSettings,
cfg config.Exporter,
) (component.LogsExporter, error) {
exporterConfig, ok := cfg.(*Config)

if !ok {
return nil, errUnexpectedConfigurationType
}

bc, err := NewBlobClient(exporterConfig.ConnectionString, exporterConfig.LogsContainerName, set.Logger)
if err != nil {
set.Logger.Error(err.Error())
return nil, err
}

return newLogsExporter(exporterConfig, bc, set)
}
Loading