-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate otel collector from apmbench (#43)
- Loading branch information
Showing
20 changed files
with
669 additions
and
1,533 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,48 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License 2.0; | ||
// you may not use this file except in compliance with the Elastic License 2.0. | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type telemetry struct { | ||
endpoint string | ||
} | ||
|
||
func (t telemetry) GetAll() (map[string]float64, error) { | ||
resp, err := http.Get(t.endpoint + "/") | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get telemetry data: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
switch resp.StatusCode / 100 { | ||
case 2: | ||
m := make(map[string]float64) | ||
if err := json.NewDecoder(resp.Body).Decode(&m); err != nil { | ||
return nil, fmt.Errorf("failed to decode response body for getting telemetry data: %w", err) | ||
} | ||
return m, nil | ||
default: | ||
return nil, fmt.Errorf("unsuccessful response from benchmark telemetry server: %d", resp.StatusCode) | ||
} | ||
} | ||
|
||
func (t telemetry) Reset() error { | ||
resp, err := http.Post(t.endpoint+"/reset", "application/json", nil) | ||
if err != nil { | ||
return errors.New("failed to reset telemetry") | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return errors.New("failed to reset telemetry") | ||
} | ||
return nil | ||
} |
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,31 @@ | ||
# OTEL in-memory exporter | ||
|
||
Aggregates collected metrics as per the configured aggregation type and exposes them via a HTTP endpoint. Current supported aggregation types are `last`, `sum`, and `rate`. | ||
|
||
## Usage | ||
|
||
The exporter is built to aid collection of metrics for benchmarking using [apmbench](../apmbench). It can be used to build OpenTelemetry collector using [OpenTelemetry Collector Builder (ocb)](https://pkg.go.dev/go.opentelemetry.io/collector/cmd/builder#section-readme). Example configurartion file for the builder: | ||
|
||
```yaml | ||
dist: | ||
module: opentelemetry-collector | ||
name: otel | ||
description: Test otel collector with in-memory exporter. | ||
output_path: ./generated | ||
otelcol_version: 0.88.0 | ||
|
||
exporters: | ||
- gomod: github.com/elastic/apm-perf/cmd/otelinmemexporter v0.0.0-00010101000000-000000000000 | ||
|
||
processors: | ||
- gomod: go.opentelemetry.io/collector/processor/batchprocessor v0.88.0 | ||
- gomod: github.com/open-telemetry/opentelemetry-collector-contrib/processor/cumulativetodeltaprocessor v0.88.0 | ||
|
||
receivers: | ||
- gomod: go.opentelemetry.io/collector/receiver/otlpreceiver v0.88.0 | ||
|
||
``` | ||
|
||
The above configuration file can be used to generate code for OpenTelemetry Collector using `builder --skip-compilation --config=ocb-config.yaml`. | ||
|
||
NOTE: The in-memory exporter should be used for a single benchmark at a time to avoid conflicts in collected metrics. |
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,35 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License 2.0; | ||
// you may not use this file except in compliance with the Elastic License 2.0. | ||
|
||
// Package otelinmemexporter contains code for creating an in-memory OTEL exporter. | ||
package otelinmemexporter | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
) | ||
|
||
type serverConfig struct { | ||
Endpoint string `mapstructure:"endpoint"` | ||
} | ||
|
||
type Config struct { | ||
Aggregations []AggregationConfig `mapstructure:"aggregations"` | ||
Server serverConfig `mapstructure:"server"` | ||
} | ||
|
||
var _ component.Config = (*Config)(nil) | ||
|
||
// Validate checks if the exporter configuration is valid | ||
func (cfg *Config) Validate() error { | ||
if _, err := validateAggregationConfig(cfg.Aggregations); err != nil { | ||
return fmt.Errorf("failed to validate aggregation config: %w", err) | ||
} | ||
if cfg.Server.Endpoint == "" { | ||
return errors.New("failed to validate server config: address cannot be empty") | ||
} | ||
return nil | ||
} |
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,60 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License 2.0; | ||
// you may not use this file except in compliance with the Elastic License 2.0. | ||
|
||
package otelinmemexporter | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/consumer" | ||
"go.opentelemetry.io/collector/exporter" | ||
"go.opentelemetry.io/collector/exporter/exporterhelper" | ||
) | ||
|
||
func NewFactory() exporter.Factory { | ||
return exporter.NewFactory( | ||
componentID, | ||
createDefaultConfig, | ||
exporter.WithMetrics( | ||
createMetricsExporter, | ||
component.StabilityLevelDevelopment, | ||
), | ||
) | ||
} | ||
|
||
func createDefaultConfig() component.Config { | ||
return &Config{ | ||
Server: serverConfig{Endpoint: ":8081"}, | ||
} | ||
} | ||
|
||
func createMetricsExporter( | ||
ctx context.Context, | ||
settings exporter.CreateSettings, | ||
rawCfg component.Config, | ||
) (exporter.Metrics, error) { | ||
cfg := rawCfg.(*Config) | ||
logger := settings.TelemetrySettings.Logger | ||
|
||
// create in memory metrics store | ||
store, err := NewStore(cfg.Aggregations, logger) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create in-memory metrics store: %w", err) | ||
} | ||
// Start http server | ||
newServer(store, cfg.Server.Endpoint, logger).Start() | ||
|
||
exp := new(*cfg, store, logger) | ||
return exporterhelper.NewMetricsExporter( | ||
ctx, settings, cfg, | ||
exp.consumeMetrics, | ||
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}), | ||
// Disable Timeout/RetryOnFailure and SendingQueue | ||
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0}), | ||
exporterhelper.WithRetry(exporterhelper.RetrySettings{Enabled: false}), | ||
exporterhelper.WithQueue(exporterhelper.QueueSettings{Enabled: false}), | ||
) | ||
} |
Oops, something went wrong.