Skip to content

Commit

Permalink
Working version for Lambda - AWS Managed Prometheus integration
Browse files Browse the repository at this point in the history
  • Loading branch information
pcolazurdo committed Jun 14, 2022
1 parent 15412bd commit add5813
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 7 deletions.
3 changes: 2 additions & 1 deletion collector/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ GOBUILD=GO111MODULE=on CGO_ENABLED=0 installsuffix=cgo go build -trimpath
BUILD_INFO_IMPORT_PATH=main

LDFLAGS=-ldflags "-s -w -X $(BUILD_INFO_IMPORT_PATH).GitHash=$(GIT_SHA) -X $(BUILD_INFO_IMPORT_PATH).Version=$(VERSION) \
-X github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsxrayexporter.collectorDistribution=opentelemetry-collector-lambda"
-X github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsxrayexporter.collectorDistribution=opentelemetry-collector-lambda \
-X github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsprometheusremotewriteexporter.collectorDistribution=opentelemetry-collector-lambda"

clean:
rm -rf build/
Expand Down
7 changes: 6 additions & 1 deletion collector/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ receivers:
exporters:
logging:
loglevel: debug
awsprometheusremotewrite:
endpoint: "https://aps-workspaces.eu-west-1.amazonaws.com/workspaces/ws-12345678-1234-1234-1234-123456789012/api/v1/remote_write"
aws_auth:
service: "aps"
region: "eu-west-1"

service:
pipelines:
Expand All @@ -15,4 +20,4 @@ service:
exporters: [logging]
metrics:
receivers: [otlp]
exporters: [logging]
exporters: [logging, awsprometheusremotewrite]
3 changes: 3 additions & 0 deletions collector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
cloud.google.com/go/compute v1.6.0 // indirect
contrib.go.opencensus.io/exporter/prometheus v0.4.1 // indirect
github.com/antonmedv/expr v1.9.0 // indirect
github.com/aws/aws-sdk-go v1.44.11 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
Expand All @@ -31,6 +32,7 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.3 // indirect
github.com/knadh/koanf v1.4.1 // indirect
Expand All @@ -43,6 +45,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mostynb/go-grpc-compression v1.1.16 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsprometheusremotewriteexporter v0.51.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusremotewriteexporter v0.51.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.51.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/resourcetotelemetry v0.51.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions collector/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ github.com/aws/aws-sdk-go v1.40.11/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm
github.com/aws/aws-sdk-go v1.42.31/go.mod h1:OGr6lGMAKGlG9CVrYnWYDKIyb829c6EVBRjxqjmPepc=
github.com/aws/aws-sdk-go v1.43.11/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/aws/aws-sdk-go v1.43.31/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/aws/aws-sdk-go v1.44.11 h1:eIC59RrNY7xXYmGy/kKkLj4PGB325Jca22lcxZwbpBE=
github.com/aws/aws-sdk-go v1.44.11/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/aws/aws-sdk-go-v2 v1.9.2/go.mod h1:cK/D0BBs0b/oWPIcX/Z/obahJK1TT7IPVjy53i/mX/4=
github.com/aws/aws-sdk-go-v2/config v1.8.3/go.mod h1:4AEiLtAb8kLs7vgw2ZV3p2VZ1+hBavOc84hqxVNpCyw=
Expand Down Expand Up @@ -952,7 +954,9 @@ github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht
github.com/jmespath/go-jmespath v0.0.0-20160803190731-bd40a432e4c7/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/joefitzgerald/rainbow-reporter v0.1.0/go.mod h1:481CNgqmVHQZzdIbN52CupLJyoVwB10FQ/IQlF1pdL8=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
Expand Down Expand Up @@ -1186,6 +1190,8 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDsH8xc=
github.com/onsi/gomega v1.15.0/go.mod h1:cIuvLEne0aoVhAgh/O6ac0Op8WWw9H6eYCriF+tEHG0=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsprometheusremotewriteexporter v0.51.0 h1:z/qJ3Lkp/fCjLjXAQLHxQoEepeoEblvEHGQD8Qj9owI=
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsprometheusremotewriteexporter v0.51.0/go.mod h1:3KW/KsZb2QPxANoDhpfLIRVmxLWp4ITGVPBRvAgQboM=
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusremotewriteexporter v0.51.0 h1:dYTzPgICn/zrS4mwvO2H7pWtOxc/mWR0hyMva1KeGB8=
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusremotewriteexporter v0.51.0/go.mod h1:8EM88Rm2bwLSWadU8/jqEi/o3LwMcqXMLLwis4MCFMg=
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.51.0 h1:fOaDqCw89M+GDEsCWGWuWZB3qPZek81to/fLshHArNg=
Expand Down
2 changes: 2 additions & 0 deletions collector/lambdacomponents/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"go.opentelemetry.io/collector/receiver/otlpreceiver"
"go.uber.org/multierr"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsprometheusremotewriteexporter"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusremotewriteexporter"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/attributesprocessor"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/filterprocessor"
Expand All @@ -46,6 +47,7 @@ func Components() (component.Factories, error) {
otlpexporter.NewFactory(),
otlphttpexporter.NewFactory(),
prometheusremotewriteexporter.NewFactory(),
awsprometheusremotewriteexporter.NewFactory(),
)
if err != nil {
errs = append(errs, err)
Expand Down
1 change: 1 addition & 0 deletions collector/lambdacomponents/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusremotewriteexporter v0.51.0
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsprometheusremotewriteexporter v0.51.0
github.com/open-telemetry/opentelemetry-collector-contrib/processor/attributesprocessor v0.51.0
github.com/open-telemetry/opentelemetry-collector-contrib/processor/filterprocessor v0.51.0
github.com/open-telemetry/opentelemetry-collector-contrib/processor/probabilisticsamplerprocessor v0.51.0
Expand Down
38 changes: 36 additions & 2 deletions python/sample-apps/function/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@
import aiohttp
import asyncio
import boto3
import os
import time

from opentelemetry import _metrics
from opentelemetry.exporter.otlp.proto.grpc._metric_exporter import (
OTLPMetricExporter,
)
import opentelemetry.exporter.otlp.proto.grpc.version

print( "opentelemetry.exporter.otlp.proto.grpc version", opentelemetry.exporter.otlp.proto.grpc.version.__version__)

from opentelemetry._metrics import (
get_meter_provider,
set_meter_provider,
)
from opentelemetry.sdk._metrics import MeterProvider
from opentelemetry.sdk._metrics.export import PeriodicExportingMetricReader

exporter = OTLPMetricExporter(insecure=True)
reader = PeriodicExportingMetricReader(exporter)
provider = MeterProvider(metric_readers=[reader])
set_meter_provider(provider)

meter = get_meter_provider().get_meter("otel_stack_function", "0.1.2")


async def fetch(session, url):
Expand All @@ -21,8 +45,18 @@ def lambda_handler(event, context):

loop = asyncio.get_event_loop()
loop.run_until_complete(callAioHttp())

counter = meter.create_counter(name="first_counter", description="TODO", unit="1",)

i = 0
for bucket in s3.buckets.all():
print(bucket.name)

i = i + 1
# print(bucket.name)

counter.add(i, attributes={"invocation": context.aws_request_id})

provider.force_flush() # needed to be sure that metrics are sent to OTel before shutting down the worker
time.sleep(1) # This is needed because at the moment there is no API to force the collector to push the message before the Worker is suspended


return {"statusCode": 200, "body": json.dumps(os.environ.get("_X_AMZN_TRACE_ID"))}
5 changes: 4 additions & 1 deletion python/sample-apps/function/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
aiohttp
aiohttp
opentelemetry-api
opentelemetry-sdk
opentelemetry-exporter-otlp-proto-grpc
Empty file modified python/sample-apps/run.sh
100644 → 100755
Empty file.
11 changes: 9 additions & 2 deletions python/sample-apps/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,26 @@ Resources:
Type: AWS::Serverless::Function
Properties:
Handler: lambda_function.lambda_handler
Runtime: python3.9
Runtime: python3.7
CodeUri: ./function
Description: Build OTel Python Lambda Sample from scratch
MemorySize: 512
Timeout: 15
Timeout: 310
Policies:
- AWSLambdaBasicExecutionRole
- AWSLambda_ReadOnlyAccess
- AWSXrayWriteOnlyAccess
- AmazonS3FullAccess
- Statement:
- Sid: AMPRemoteWrite
Effect: Allow
Action:
- aps:RemoteWrite
Resource: '*'
Environment:
Variables:
AWS_LAMBDA_EXEC_WRAPPER: /opt/python/otel-instrument
PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION: python # needed because https://github.com/open-telemetry/opentelemetry-python/issues/2717 / when new release of library is released this should be solved and can be removed
Tracing: Active
Layers:
- !Ref OTelLayer
Expand Down

0 comments on commit add5813

Please sign in to comment.