mirror of
https://github.com/hanzoai/otel-collector.git
synced 2026-11-03 02:20:04 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
124 lines
3.8 KiB
Go
124 lines
3.8 KiB
Go
// Copyright 2020, 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 datastorelogsexporter
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jellydator/ttlcache/v3"
|
|
"go.opentelemetry.io/collector/component"
|
|
"go.opentelemetry.io/collector/config/configoptional"
|
|
"go.opentelemetry.io/collector/config/configretry"
|
|
"go.opentelemetry.io/collector/exporter"
|
|
"go.opentelemetry.io/collector/exporter/exporterhelper"
|
|
|
|
"github.com/hanzoai/otel-collector/exporter/datastorelogsexporter/internal/metadata"
|
|
"github.com/hanzoai/otel-collector/utils"
|
|
)
|
|
|
|
const (
|
|
databaseName = "o11y_logs"
|
|
)
|
|
|
|
// NewFactory creates a factory for Elastic exporter.
|
|
func NewFactory() exporter.Factory {
|
|
return exporter.NewFactory(
|
|
metadata.Type,
|
|
createDefaultConfig,
|
|
exporter.WithLogs(createLogsExporter, metadata.LogsStability),
|
|
)
|
|
}
|
|
|
|
func createDefaultConfig() component.Config {
|
|
return &Config{
|
|
TimeoutConfig: exporterhelper.NewDefaultTimeoutConfig(),
|
|
QueueBatchConfig: configoptional.Some(exporterhelper.NewDefaultQueueConfig()),
|
|
BackOffConfig: configretry.NewDefaultBackOffConfig(),
|
|
AttributesLimits: AttributesLimits{
|
|
FetchKeysInterval: 10 * time.Minute,
|
|
MaxDistinctValues: 25000,
|
|
},
|
|
LogLevelConcurrency: new(utils.Concurrency()),
|
|
PromotedPathsSyncInterval: new(defaultPromotedPathsSyncInterval),
|
|
MaxAllowedDataAgeDays: new(defaultMaxAllowedDataAgeDays),
|
|
}
|
|
}
|
|
|
|
// createLogsExporter creates a new exporter for logs.
|
|
// Logs are directly insert into datastore.
|
|
func createLogsExporter(
|
|
ctx context.Context,
|
|
set exporter.Settings,
|
|
cfg component.Config,
|
|
) (exporter.Logs, error) {
|
|
c := cfg.(*Config)
|
|
|
|
client, err := newDatastoreClient(set.Logger, c)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot configure datastore logs exporter: %w", err)
|
|
}
|
|
|
|
id := uuid.New()
|
|
|
|
// keys cache is used to avoid duplicate inserts for the same attribute key.
|
|
keysCache := ttlcache.New(
|
|
ttlcache.WithTTL[string, struct{}](240*time.Minute),
|
|
ttlcache.WithCapacity[string, struct{}](50000),
|
|
ttlcache.WithDisableTouchOnHit[string, struct{}](),
|
|
)
|
|
go keysCache.Start()
|
|
|
|
// resource fingerprint cache is used to avoid duplicate inserts for the same resource fingerprint.
|
|
// the ttl is set to the same as the bucket rounded value i.e 1800 seconds.
|
|
// if a resource fingerprint is seen in the bucket already, skip inserting it again.
|
|
rfCache := ttlcache.New(
|
|
ttlcache.WithTTL[string, struct{}](distributedLogsResourceV2Seconds*time.Second),
|
|
ttlcache.WithDisableTouchOnHit[string, struct{}](),
|
|
ttlcache.WithCapacity[string, struct{}](100000),
|
|
)
|
|
go rfCache.Start()
|
|
|
|
meter := set.MeterProvider.Meter(metadata.ScopeName)
|
|
opts := []LogExporterOption{
|
|
WithDatastoreClient(client),
|
|
WithLogger(set.Logger),
|
|
WithNewUsageCollector(id, client),
|
|
WithMeter(meter),
|
|
WithKeysCache(keysCache),
|
|
WithRFCache(rfCache),
|
|
WithConcurrency(*c.LogLevelConcurrency),
|
|
}
|
|
|
|
exporter, err := newExporter(set, c, opts...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot configure datastore logs exporter: %w", err)
|
|
}
|
|
|
|
return exporterhelper.NewLogs(
|
|
ctx,
|
|
set,
|
|
cfg,
|
|
exporter.pushLogsData,
|
|
exporterhelper.WithStart(exporter.Start),
|
|
exporterhelper.WithShutdown(exporter.Shutdown),
|
|
exporterhelper.WithTimeout(c.TimeoutConfig),
|
|
exporterhelper.WithQueue(c.QueueBatchConfig),
|
|
exporterhelper.WithRetry(c.BackOffConfig),
|
|
)
|
|
}
|