├── .github └── workflows │ └── ci.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── go.work ├── go.work.sum ├── sample-app ├── go.mod ├── go.sum ├── main.go └── workflow_example.go ├── semconv-ai ├── attributes.go ├── go.mod └── go.sum └── traceloop-sdk ├── config.go ├── go.mod ├── go.sum ├── model └── prompt_registry.go ├── prompt_registry.go ├── sdk.go ├── tracing.go ├── tracing_types.go ├── utils.go ├── version.go └── workflow.go /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | pull_request_target: 4 | types: [opened, synchronize] 5 | push: 6 | branches: 7 | - "main" 8 | 9 | jobs: 10 | build-and-test: 11 | runs-on: ubuntu-latest 12 | strategy: 13 | matrix: 14 | go-version: ["1.18", "1.19", "1.20", "1.21"] 15 | 16 | steps: 17 | - uses: actions/checkout@v4 18 | - name: Setup Go ${{ matrix.go-version }} 19 | uses: actions/setup-go@v4 20 | with: 21 | go-version: ${{ matrix.go-version }} 22 | - name: Install dependencies 23 | run: find . -name go.mod -execdir go get . \; 24 | - name: Build 25 | run: find . -name go.mod -execdir go build . \; 26 | - name: Test 27 | run: find . -name go.mod -execdir go test ./... \; 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # If you prefer the allow list template instead of the deny list, see community template: 2 | # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore 3 | # 4 | # Binaries for programs and plugins 5 | *.exe 6 | *.exe~ 7 | *.dll 8 | *.so 9 | *.tar.gz 10 | /release/ 11 | sample-app 12 | 13 | # Test binary, built with `go test -c` 14 | *.test 15 | cover* 16 | coverage* 17 | 18 | # Credentials 19 | cert*/ 20 | *.pem 21 | *.crt 22 | *.cer 23 | *.key 24 | *.p12 25 | 26 | # Output of the go coverage tool, specifically when used with LiteIDE 27 | *.out 28 | 29 | # Dependency directories (remove the comment below to include it) 30 | # vendor/ 31 | 32 | # macOS 33 | .DS_Store 34 | 35 | .vscode/ 36 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, religion, or sexual identity 10 | and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | - Demonstrating empathy and kindness toward other people 21 | - Being respectful of differing opinions, viewpoints, and experiences 22 | - Giving and gracefully accepting constructive feedback 23 | - Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | - Focusing on what is best not just for us as individuals, but for the 26 | overall community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | - The use of sexualized language or imagery, and sexual attention or 31 | advances of any kind 32 | - Trolling, insulting or derogatory comments, and personal or political attacks 33 | - Public or private harassment 34 | - Publishing others' private information, such as a physical or email 35 | address, without their explicit permission 36 | - Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at 63 | support@traceloop.dev. 64 | All complaints will be reviewed and investigated promptly and fairly. 65 | 66 | All community leaders are obligated to respect the privacy and security of the 67 | reporter of any incident. 68 | 69 | ## Enforcement Guidelines 70 | 71 | Community leaders will follow these Community Impact Guidelines in determining 72 | the consequences for any action they deem in violation of this Code of Conduct: 73 | 74 | ### 1. Correction 75 | 76 | **Community Impact**: Use of inappropriate language or other behavior deemed 77 | unprofessional or unwelcome in the community. 78 | 79 | **Consequence**: A private, written warning from community leaders, providing 80 | clarity around the nature of the violation and an explanation of why the 81 | behavior was inappropriate. A public apology may be requested. 82 | 83 | ### 2. Warning 84 | 85 | **Community Impact**: A violation through a single incident or series 86 | of actions. 87 | 88 | **Consequence**: A warning with consequences for continued behavior. No 89 | interaction with the people involved, including unsolicited interaction with 90 | those enforcing the Code of Conduct, for a specified period of time. This 91 | includes avoiding interactions in community spaces as well as external channels 92 | like social media. Violating these terms may lead to a temporary or 93 | permanent ban. 94 | 95 | ### 3. Temporary Ban 96 | 97 | **Community Impact**: A serious violation of community standards, including 98 | sustained inappropriate behavior. 99 | 100 | **Consequence**: A temporary ban from any sort of interaction or public 101 | communication with the community for a specified period of time. No public or 102 | private interaction with the people involved, including unsolicited interaction 103 | with those enforcing the Code of Conduct, is allowed during this period. 104 | Violating these terms may lead to a permanent ban. 105 | 106 | ### 4. Permanent Ban 107 | 108 | **Community Impact**: Demonstrating a pattern of violation of community 109 | standards, including sustained inappropriate behavior, harassment of an 110 | individual, or aggression toward or disparagement of classes of individuals. 111 | 112 | **Consequence**: A permanent ban from any sort of public interaction within 113 | the community. 114 | 115 | ## Attribution 116 | 117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 118 | version 2.0, available at 119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 120 | 121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct 122 | enforcement ladder](https://github.com/mozilla/diversity). 123 | 124 | [homepage]: https://www.contributor-covenant.org 125 | 126 | For answers to common questions about this code of conduct, see the FAQ at 127 | https://www.contributor-covenant.org/faq. Translations are available at 128 | https://www.contributor-covenant.org/translations. 129 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to `go-openllmetry` 2 | 3 | Thanks for taking the time to contribute! 😃 🚀 4 | 5 | Please refer to our [Contributing Guide](https://traceloop.com/docs/openllmetry/contributing/overview) for instructions on how to contribute. 6 | 7 | ### Prerequisites 8 | 9 | - `go` version `v1.21.0+` 10 | 11 | ## Development 12 | 13 | Clone the repository: 14 | 15 | ```bash 16 | git clone https://github.com/traceloop/go-openllmetry 17 | cd go-openllmetry 18 | ``` 19 | 20 | ## Release 21 | 22 | ```bash 23 | git tag -a /v -m "release: @v" 24 | ``` 25 | 26 | For example: 27 | 28 | ```bash 29 | git tag -a traceloop-sdk/v0.0.4 -m "release: traceloop-sdk@v0.0.4" 30 | ``` 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | 4 | 5 | 6 | 7 | 8 |

9 |

For Go

10 |

11 |

Open-source observability for your LLM application

12 |

13 |

14 | Get started » 15 |
16 |
17 | Slack | 18 | Docs | 19 | Website 20 |

21 | 22 |

23 | 24 | OpenLLMetry is released under the Apache-2.0 License 25 | 26 | 27 | 28 | PRs welcome! 29 | 30 | 31 | git commit activity 32 | 33 | 34 | Slack community channel 35 | 36 | 37 | Traceloop Twitter 38 | 39 |

40 | 41 | OpenLLMetry is a set of extensions built on top of [OpenTelemetry](https://opentelemetry.io/) that gives you complete observability over your LLM application. Because it uses OpenTelemetry under the hood, it can be connected to your existing observability solutions - Datadog, Honeycomb, and others. 42 | 43 | It's built and maintained by Traceloop under the Apache 2.0 license. 44 | 45 | The repo contains standard OpenTelemetry instrumentations for LLM providers and Vector DBs, as well as a Traceloop SDK that makes it easy to get started with OpenLLMetry, while still outputting standard OpenTelemetry data that can be connected to your observability stack. 46 | If you already have OpenTelemetry instrumented, you can just add any of our instrumentations directly. 47 | 48 | ## 🚀 Getting Started 49 | 50 | The easiest way to get started is to use our SDK. 51 | For a complete guide, go to our [docs](https://traceloop.com/docs/openllmetry/getting-started-go). 52 | 53 | Install the SDK: 54 | 55 | ```bash 56 | go get github.com/traceloop/go-openllmetry/traceloop-sdk 57 | ``` 58 | 59 | Then, initialize the SDK in your code: 60 | 61 | ```go 62 | package main 63 | 64 | import ( 65 | "context" 66 | 67 | sdk "github.com/traceloop/go-openllmetry/traceloop-sdk" 68 | ) 69 | 70 | func main() { 71 | ctx := context.Background() 72 | 73 | traceloop := sdk.NewClient(ctx, sdk.Config{ 74 | APIKey: os.Getenv("TRACELOOP_API_KEY"), 75 | }) 76 | defer func() { traceloop.Shutdown(ctx) }() 77 | } 78 | ``` 79 | 80 | That's it. You're now tracing your code with OpenLLMetry! 81 | 82 | Now, you need to decide where to export the traces to. 83 | 84 | ## ⏫ Supported (and tested) destinations 85 | 86 | - [x] [Traceloop](https://www.traceloop.com/docs/openllmetry/integrations/traceloop) 87 | - [x] [Axiom](https://www.traceloop.com/docs/openllmetry/integrations/axiom) 88 | - [x] [Azure Application Insights](https://www.traceloop.com/docs/openllmetry/integrations/azure) 89 | - [x] [Datadog](https://www.traceloop.com/docs/openllmetry/integrations/datadog) 90 | - [x] [Dynatrace](https://www.traceloop.com/docs/openllmetry/integrations/dynatrace) 91 | - [x] [Grafana](https://www.traceloop.com/docs/openllmetry/integrations/grafana) 92 | - [x] [Highlight](https://www.traceloop.com/docs/openllmetry/integrations/highlight) 93 | - [x] [Honeycomb](https://www.traceloop.com/docs/openllmetry/integrations/honeycomb) 94 | - [x] [HyperDX](https://www.traceloop.com/docs/openllmetry/integrations/hyperdx) 95 | - [x] [IBM Instana](https://www.traceloop.com/docs/openllmetry/integrations/instana) 96 | - [x] [KloudMate](https://www.traceloop.com/docs/openllmetry/integrations/kloudmate) 97 | - [x] [Langfuse](https://langfuse.com/docs/opentelemetry/example-openllmetry) 98 | - [x] [New Relic](https://www.traceloop.com/docs/openllmetry/integrations/newrelic) 99 | - [x] [OpenTelemetry Collector](https://www.traceloop.com/docs/openllmetry/integrations/otel-collector) 100 | - [x] [Service Now Cloud Observability](https://www.traceloop.com/docs/openllmetry/integrations/service-now) 101 | - [x] [SigNoz](https://www.traceloop.com/docs/openllmetry/integrations/signoz) 102 | - [x] [Sentry](https://www.traceloop.com/docs/openllmetry/integrations/sentry) 103 | - [x] [Splunk](https://www.traceloop.com/docs/openllmetry/integrations/splunk) 104 | 105 | 106 | See [our docs](https://traceloop.com/docs/openllmetry/integrations/exporting) for instructions on connecting to each one. 107 | 108 | ## 🪗 What do we instrument? 109 | 110 | OpenLLMetry is in early-alpha exploratory stage, and we're still figuring out what to instrument. 111 | As opposed to other languages, there aren't many official LLM libraries (yet?), so for now you'll have to manually log prompts: 112 | 113 | ```go 114 | package main 115 | 116 | import ( 117 | "context" 118 | "fmt" 119 | "os" 120 | 121 | "github.com/sashabaranov/go-openai" 122 | sdk "github.com/traceloop/go-openllmetry/traceloop-sdk" 123 | ) 124 | 125 | func main() { 126 | ctx := context.Background() 127 | 128 | // Initialize Traceloop 129 | traceloop := sdk.NewClient(ctx, config.Config{ 130 | APIKey: os.Getenv("TRACELOOP_API_KEY"), 131 | }) 132 | defer func() { traceloop.Shutdown(ctx) }() 133 | 134 | // Call OpenAI like you normally would 135 | resp, err := client.CreateChatCompletion( 136 | context.Background(), 137 | openai.ChatCompletionRequest{ 138 | Model: openai.GPT3Dot5Turbo, 139 | Messages: []openai.ChatCompletionMessage{ 140 | { 141 | Role: openai.ChatMessageRoleUser, 142 | Content: "Tell me a joke about OpenTelemetry!", 143 | }, 144 | }, 145 | }, 146 | ) 147 | 148 | var promptMsgs []sdk.Message 149 | for i, message := range request.Messages { 150 | promptMsgs = append(promptMsgs, sdk.Message{ 151 | Index: i, 152 | Content: message.Content, 153 | Role: message.Role, 154 | }) 155 | } 156 | 157 | // Log the request 158 | llmSpan, err := traceloop.LogPrompt( 159 | ctx, 160 | sdk.Prompt{ 161 | Vendor: "openai", 162 | Mode: "chat", 163 | Model: request.Model, 164 | Messages: promptMsgs, 165 | }, 166 | sdk.TraceloopAttributes{ 167 | WorkflowName: "example-workflow", 168 | EntityName: "example-entity", 169 | }, 170 | ) 171 | if err != nil { 172 | fmt.Printf("LogPrompt error: %v\n", err) 173 | return 174 | } 175 | 176 | client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) 177 | resp, err := client.CreateChatCompletion( 178 | context.Background(), 179 | *request, 180 | ) 181 | if err != nil { 182 | fmt.Printf("ChatCompletion error: %v\n", err) 183 | return 184 | } 185 | 186 | var completionMsgs []sdk.Message 187 | for _, choice := range resp.Choices { 188 | completionMsgs = append(completionMsgs, sdk.Message{ 189 | Index: choice.Index, 190 | Content: choice.Message.Content, 191 | Role: choice.Message.Role, 192 | }) 193 | } 194 | 195 | // Log the response 196 | llmSpan.LogCompletion(ctx, sdk.Completion{ 197 | Model: resp.Model, 198 | Messages: completionMsgs, 199 | }, sdk.Usage{ 200 | TotalTokens: resp.Usage.TotalTokens, 201 | CompletionTokens: resp.Usage.CompletionTokens, 202 | PromptTokens: resp.Usage.PromptTokens, 203 | }) 204 | } 205 | ``` 206 | 207 | ## 🌱 Contributing 208 | 209 | Whether it's big or small, we love contributions ❤️ Check out our guide to see how to [get started](https://traceloop.com/docs/openllmetry/contributing/overview). 210 | 211 | Not sure where to get started? You can: 212 | 213 | - [Book a free pairing session with one of our teammates](mailto:nir@traceloop.com?subject=Pairing%20session&body=I'd%20like%20to%20do%20a%20pairing%20session!)! 214 | - Join our Slack, and ask us any questions there. 215 | 216 | ## 💚 Community & Support 217 | 218 | - [Slack](https://join.slack.com/t/traceloopcommunity/shared_invite/zt-1plpfpm6r-zOHKI028VkpcWdobX65C~g) (For live discussion with the community and the Traceloop team) 219 | - [GitHub Discussions](https://github.com/traceloop/go-openllmetry/discussions) (For help with building and deeper conversations about features) 220 | - [GitHub Issues](https://github.com/traceloop/go-openllmetry/issues) (For any bugs and errors you encounter using OpenLLMetry) 221 | - [Twitter](https://twitter.com/traceloopdev) (Get news fast) 222 | 223 | ## 🙏 Special Thanks 224 | 225 | To @patrickdebois, who [suggested the great name](https://x.com/patrickdebois/status/1695518950715473991?s=46&t=zn2SOuJcSVq-Pe2Ysevzkg) we're now using for this repo! 226 | -------------------------------------------------------------------------------- /go.work: -------------------------------------------------------------------------------- 1 | go 1.21 2 | 3 | use ( 4 | semconv-ai 5 | traceloop-sdk 6 | sample-app 7 | ) 8 | -------------------------------------------------------------------------------- /go.work.sum: -------------------------------------------------------------------------------- 1 | cloud.google.com/go/compute v1.23.0/go.mod h1:4tCnrn48xsqlwSAiLf1HXMQk8CONslYbdiEZc9FEIbM= 2 | cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA= 3 | dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= 4 | github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= 5 | github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 h1:kkhsdkhsCvIsutKu5zLMgWtgh9YxGCNAw8Ad8hjwfYg= 6 | github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= 7 | github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= 8 | github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= 9 | github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= 10 | github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw= 11 | github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= 12 | github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs= 13 | github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= 14 | github.com/cncf/udpa/go v0.0.0-20220112060539-c52dc94e7fbe/go.mod h1:6pvJx4me5XPnfI9Z40ddWsdw2W/uZgQLFXToKeRcDiI= 15 | github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= 16 | github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= 17 | github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= 18 | github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= 19 | github.com/envoyproxy/go-control-plane v0.11.1/go.mod h1:uhMcXKCQMEJHiAb0w+YGefQLaTEw+YhGluxZkrTmD0g= 20 | github.com/envoyproxy/protoc-gen-validate v1.0.2/go.mod h1:GpiZQP3dDbg4JouG/NNS7QWXpgx6x8QiMKdmN72jogE= 21 | github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4= 22 | github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII= 23 | github.com/golang/glog v1.1.2/go.mod h1:zR+okUeTbrL6EL3xHUDxZuEtGv04p5shwip1+mL/rLQ= 24 | github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= 25 | github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= 26 | github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 27 | github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= 28 | github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= 29 | github.com/klauspost/compress v1.15.15/go.mod h1:ZcK2JAFqKOpnBlxcLsJzYfrS9X1akm9fHZNnD9+Vo/4= 30 | github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M= 31 | github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= 32 | github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI= 33 | github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= 34 | github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= 35 | github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= 36 | github.com/skeema/knownhosts v1.2.1 h1:SHWdIUa82uGZz+F+47k8SY4QhhI291cXCpopT1lK2AQ= 37 | github.com/skeema/knownhosts v1.2.1/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= 38 | github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= 39 | github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= 40 | go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= 41 | golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= 42 | golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= 43 | golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= 44 | golang.org/x/oauth2 v0.13.0/go.mod h1:/JMhi4ZRXAf4HG9LiNmxvk+45+96RUlVThiH8FzNBn0= 45 | golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= 46 | golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= 47 | google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds= 48 | google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97/go.mod h1:t1VqOqqvce95G3hIDCT5FeO3YUc6Q4Oe24L/+rNMxRk= 49 | -------------------------------------------------------------------------------- /sample-app/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/traceloop/go-openllmetry/sample-app 2 | 3 | go 1.21 4 | 5 | require github.com/sashabaranov/go-openai v1.18.1 6 | -------------------------------------------------------------------------------- /sample-app/go.sum: -------------------------------------------------------------------------------- 1 | github.com/sashabaranov/go-openai v1.18.1 h1:AnLoJrFaFtcUYWCtz+8V0zrlXxkiwqpWlAmCAZUnDNQ= 2 | github.com/sashabaranov/go-openai v1.18.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= 3 | -------------------------------------------------------------------------------- /sample-app/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "os" 7 | "time" 8 | 9 | "github.com/sashabaranov/go-openai" 10 | tlp "github.com/traceloop/go-openllmetry/traceloop-sdk" 11 | ) 12 | 13 | func workflow_example() { 14 | ctx := context.Background() 15 | 16 | traceloop, err := tlp.NewClient(ctx, tlp.Config{ 17 | BaseURL: "api-staging.traceloop.com", 18 | APIKey: os.Getenv("TRACELOOP_API_KEY"), 19 | }) 20 | defer func() { traceloop.Shutdown(ctx) }() 21 | 22 | if err != nil { 23 | fmt.Printf("NewClient error: %v\n", err) 24 | return 25 | } 26 | 27 | request, err := traceloop.GetOpenAIChatCompletionRequest("example-prompt", map[string]interface{}{"date": time.Now().Format("01/02")}) 28 | if err != nil { 29 | fmt.Printf("GetOpenAIChatCompletionRequest error: %v\n", err) 30 | return 31 | } 32 | 33 | var promptMsgs []tlp.Message 34 | for i, message := range request.Messages { 35 | promptMsgs = append(promptMsgs, tlp.Message{ 36 | Index: i, 37 | Content: message.Content, 38 | Role: message.Role, 39 | }) 40 | } 41 | 42 | llmSpan, err := traceloop.LogPrompt( 43 | ctx, 44 | tlp.Prompt{ 45 | Vendor: "openai", 46 | Mode: "chat", 47 | Model: request.Model, 48 | Messages: promptMsgs, 49 | }, 50 | tlp.WorkflowAttributes{ 51 | Name: "example-workflow", 52 | }, 53 | ) 54 | if err != nil { 55 | fmt.Printf("LogPrompt error: %v\n", err) 56 | return 57 | } 58 | 59 | client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) 60 | resp, err := client.CreateChatCompletion( 61 | context.Background(), 62 | *request, 63 | ) 64 | if err != nil { 65 | fmt.Printf("ChatCompletion error: %v\n", err) 66 | return 67 | } 68 | 69 | var completionMsgs []tlp.Message 70 | for _, choice := range resp.Choices { 71 | completionMsgs = append(completionMsgs, tlp.Message{ 72 | Index: choice.Index, 73 | Content: choice.Message.Content, 74 | Role: choice.Message.Role, 75 | }) 76 | } 77 | 78 | llmSpan.LogCompletion(ctx, tlp.Completion{ 79 | Model: resp.Model, 80 | Messages: completionMsgs, 81 | }, tlp.Usage{ 82 | TotalTokens: resp.Usage.TotalTokens, 83 | CompletionTokens: resp.Usage.CompletionTokens, 84 | PromptTokens: resp.Usage.PromptTokens, 85 | }) 86 | 87 | fmt.Println(resp.Choices[0].Message.Content) 88 | } 89 | -------------------------------------------------------------------------------- /sample-app/workflow_example.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "os" 7 | "time" 8 | 9 | "github.com/sashabaranov/go-openai" 10 | tlp "github.com/traceloop/go-openllmetry/traceloop-sdk" 11 | ) 12 | 13 | func main() { 14 | ctx := context.Background() 15 | 16 | traceloop, err := tlp.NewClient(ctx, tlp.Config{ 17 | BaseURL: "api-staging.traceloop.com", 18 | APIKey: os.Getenv("TRACELOOP_API_KEY"), 19 | }) 20 | defer func() { traceloop.Shutdown(ctx) }() 21 | 22 | if err != nil { 23 | fmt.Printf("NewClient error: %v\n", err) 24 | return 25 | } 26 | 27 | wf := traceloop.NewWorkflow(ctx, tlp.WorkflowAttributes{ 28 | Name: "history_generation", 29 | }) 30 | defer wf.End() 31 | 32 | factGenTask := wf.NewTask("current_date_fact_generation") 33 | defer factGenTask.End() 34 | 35 | request, err := traceloop.GetOpenAIChatCompletionRequest("example-prompt", map[string]interface{}{"date": time.Now().Format("01/02")}) 36 | if err != nil { 37 | fmt.Printf("GetOpenAIChatCompletionRequest error: %v\n", err) 38 | return 39 | } 40 | 41 | var promptMsgs []tlp.Message 42 | for i, message := range request.Messages { 43 | promptMsgs = append(promptMsgs, tlp.Message{ 44 | Index: i, 45 | Content: message.Content, 46 | Role: message.Role, 47 | }) 48 | } 49 | 50 | llmSpan, err := factGenTask.LogPrompt( 51 | tlp.Prompt{ 52 | Vendor: "openai", 53 | Mode: "chat", 54 | Model: request.Model, 55 | Messages: promptMsgs, 56 | }, 57 | ) 58 | if err != nil { 59 | fmt.Printf("LogPrompt error: %v\n", err) 60 | return 61 | } 62 | 63 | client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) 64 | resp, err := client.CreateChatCompletion( 65 | context.Background(), 66 | *request, 67 | ) 68 | if err != nil { 69 | fmt.Printf("ChatCompletion error: %v\n", err) 70 | return 71 | } 72 | 73 | var completionMsgs []tlp.Message 74 | for _, choice := range resp.Choices { 75 | completionMsgs = append(completionMsgs, tlp.Message{ 76 | Index: choice.Index, 77 | Content: choice.Message.Content, 78 | Role: choice.Message.Role, 79 | }) 80 | } 81 | 82 | llmSpan.LogCompletion(ctx, tlp.Completion{ 83 | Model: resp.Model, 84 | Messages: completionMsgs, 85 | }, tlp.Usage{ 86 | TotalTokens: resp.Usage.TotalTokens, 87 | CompletionTokens: resp.Usage.CompletionTokens, 88 | PromptTokens: resp.Usage.PromptTokens, 89 | }) 90 | 91 | someOtherTask := wf.NewTask("some_other_task") 92 | defer someOtherTask.End() 93 | 94 | otherPrompt, _ := someOtherTask.LogPrompt(tlp.Prompt{ 95 | Vendor: "openai", 96 | Mode: "chat", 97 | Model: request.Model, 98 | Messages: []tlp.Message{ 99 | { 100 | Index: 0, 101 | Content: "some other prompt", 102 | Role: "user", 103 | }, 104 | }, 105 | }) 106 | 107 | otherPrompt.LogCompletion(ctx, tlp.Completion{ 108 | Model: resp.Model, 109 | Messages: completionMsgs, 110 | }, tlp.Usage{ 111 | TotalTokens: resp.Usage.TotalTokens, 112 | CompletionTokens: resp.Usage.CompletionTokens, 113 | PromptTokens: resp.Usage.PromptTokens, 114 | }) 115 | 116 | fmt.Println(resp.Choices[0].Message.Content) 117 | } 118 | -------------------------------------------------------------------------------- /semconv-ai/attributes.go: -------------------------------------------------------------------------------- 1 | package semconvai 2 | 3 | import "go.opentelemetry.io/otel/attribute" 4 | 5 | const ( 6 | // LLM 7 | LLMVendor = attribute.Key("llm.vendor") 8 | LLMRequestType = attribute.Key("llm.request.type") 9 | LLMRequestModel = attribute.Key("llm.request.model") 10 | LLMResponseModel = attribute.Key("llm.response.model") 11 | LLMRequestMaxTokens = attribute.Key("llm.request.max_tokens") 12 | LLMUsageTotalTokens = attribute.Key("llm.usage.total_tokens") 13 | LLMUsageCompletionTokens = attribute.Key("llm.usage.completion_tokens") 14 | LLMUsagePromptTokens = attribute.Key("llm.usage.prompt_tokens") 15 | LLMTemperature = attribute.Key("llm.temperature") 16 | LLMUser = attribute.Key("llm.user") 17 | LLMHeaders = attribute.Key("llm.headers") 18 | LLMTopP = attribute.Key("llm.top_p") 19 | LLMTopK = attribute.Key("llm.top_k") 20 | LLMFrequencyPenalty = attribute.Key("llm.frequency_penalty") 21 | LLMPresencePenalty = attribute.Key("llm.presence_penalty") 22 | LLMPrompts = attribute.Key("llm.prompts") 23 | LLMCompletions = attribute.Key("llm.completions") 24 | LLMChatStopSequence = attribute.Key("llm.chat.stop_sequences") 25 | LLMRequestFunctions = attribute.Key("llm.request.functions") 26 | 27 | // Vector DB 28 | VectorDBVendor = attribute.Key("vector_db.vendor") 29 | VectorDBQueryTopK = attribute.Key("vector_db.query.top_k") 30 | 31 | // LLM Workflows 32 | TraceloopSpanKind = attribute.Key("traceloop.span.kind") 33 | TraceloopWorkflowName = attribute.Key("traceloop.workflow.name") 34 | TraceloopEntityName = attribute.Key("traceloop.entity.name") 35 | TraceloopAssociationProperties = attribute.Key("traceloop.association.properties") 36 | ) 37 | -------------------------------------------------------------------------------- /semconv-ai/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/traceloop/go-openllmetry/semconv-ai 2 | 3 | go 1.21 4 | 5 | require go.opentelemetry.io/otel v1.22.0 6 | -------------------------------------------------------------------------------- /semconv-ai/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= 4 | github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 5 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 6 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 7 | github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= 8 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 9 | go.opentelemetry.io/otel v1.22.0 h1:xS7Ku+7yTFvDfDraDIJVpw7XPyuHlB9MCiqqX5mcJ6Y= 10 | go.opentelemetry.io/otel v1.22.0/go.mod h1:eoV4iAi3Ea8LkAEI9+GFT44O6T/D0GWAVFyZVCC6pMI= 11 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 12 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 13 | -------------------------------------------------------------------------------- /traceloop-sdk/config.go: -------------------------------------------------------------------------------- 1 | package traceloop 2 | 3 | import "time" 4 | 5 | type BackoffConfig struct { 6 | MaxRetries uint64 7 | } 8 | 9 | type Config struct { 10 | BaseURL string 11 | APIKey string 12 | TracerName string 13 | ServiceName string 14 | PollingInterval time.Duration 15 | BackoffConfig BackoffConfig 16 | } 17 | -------------------------------------------------------------------------------- /traceloop-sdk/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/traceloop/go-openllmetry/traceloop-sdk 2 | 3 | go 1.21 4 | 5 | require ( 6 | github.com/kluctl/go-jinja2 v0.0.0-20240108142937-8839259d2537 7 | github.com/sashabaranov/go-openai v1.18.1 8 | go.opentelemetry.io/otel v1.22.0 9 | go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.22.0 10 | go.opentelemetry.io/otel/trace v1.22.0 11 | ) 12 | 13 | require ( 14 | github.com/cenkalti/backoff/v4 v4.2.1 // indirect 15 | github.com/go-logr/logr v1.4.1 // indirect 16 | github.com/go-logr/stdr v1.2.2 // indirect 17 | github.com/golang/protobuf v1.5.3 // indirect 18 | github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect 19 | go.opentelemetry.io/otel/metric v1.22.0 // indirect 20 | go.opentelemetry.io/proto/otlp v1.0.0 // indirect 21 | golang.org/x/text v0.14.0 // indirect 22 | google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 // indirect 23 | google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97 // indirect 24 | google.golang.org/grpc v1.60.1 // indirect 25 | google.golang.org/protobuf v1.32.0 // indirect 26 | ) 27 | 28 | require ( 29 | github.com/cenkalti/backoff v2.2.1+incompatible 30 | github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect 31 | github.com/go-git/go-billy/v5 v5.5.0 // indirect 32 | github.com/go-git/go-git/v5 v5.11.0 // indirect 33 | github.com/gobwas/glob v0.2.3 // indirect 34 | github.com/hashicorp/errwrap v1.1.0 // indirect 35 | github.com/hashicorp/go-multierror v1.1.1 // indirect 36 | github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect 37 | github.com/jinzhu/copier v0.4.0 // indirect 38 | github.com/kluctl/go-embed-python v0.0.0-3.11.6-20231002-1 // indirect 39 | github.com/rogpeppe/go-internal v1.11.0 // indirect 40 | github.com/sirupsen/logrus v1.9.3 // indirect 41 | go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.22.0 42 | go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.22.0 43 | go.opentelemetry.io/otel/sdk v1.22.0 44 | golang.org/x/net v0.19.0 // indirect 45 | golang.org/x/sync v0.5.0 // indirect 46 | golang.org/x/sys v0.16.0 // indirect 47 | gopkg.in/warnings.v0 v0.1.2 // indirect 48 | ) 49 | -------------------------------------------------------------------------------- /traceloop-sdk/go.sum: -------------------------------------------------------------------------------- 1 | github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= 2 | github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= 3 | github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM= 4 | github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= 5 | github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg= 6 | github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= 7 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 8 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 9 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 10 | github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= 11 | github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= 12 | github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU= 13 | github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow= 14 | github.com/go-git/go-git/v5 v5.11.0 h1:XIZc1p+8YzypNr34itUfSvYJcv+eYdTnTvOZ2vD3cA4= 15 | github.com/go-git/go-git/v5 v5.11.0/go.mod h1:6GFcX2P3NM7FPBfpePbpLd21XxsgdAt+lKqXmCUiUCY= 16 | github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= 17 | github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= 18 | github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= 19 | github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= 20 | github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= 21 | github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= 22 | github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= 23 | github.com/golang/glog v1.1.2 h1:DVjP2PbBOzHyzA+dn3WhHIq4NdVu3Q+pvivFICf/7fo= 24 | github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= 25 | github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= 26 | github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= 27 | github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 28 | github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= 29 | github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 30 | github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 h1:YBftPWNWd4WwGqtY2yeZL2ef8rHAxPBD8KFhJpmcqms= 31 | github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0/go.mod h1:YN5jB8ie0yfIUg6VvR9Kz84aCaG7AsGZnLjhHbUqwPg= 32 | github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= 33 | github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= 34 | github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= 35 | github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= 36 | github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= 37 | github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= 38 | github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= 39 | github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8= 40 | github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg= 41 | github.com/kluctl/go-embed-python v0.0.0-3.11.6-20231002-1 h1:L+ZH/eN5gE7eh3BTye/Z8td8YjbhEs6hzybVByz2twQ= 42 | github.com/kluctl/go-embed-python v0.0.0-3.11.6-20231002-1/go.mod h1:2/V+QZL7VyhTXtKHorARyA7UYOizVV37M8kkXMEk+Kg= 43 | github.com/kluctl/go-jinja2 v0.0.0-20240108142937-8839259d2537 h1:oG9FYqprfbAI9kQtec4D0gPwJqLJlS+euknEVz25gp0= 44 | github.com/kluctl/go-jinja2 v0.0.0-20240108142937-8839259d2537/go.mod h1:7FmUmt2zgHJfJE82ZNY/AHNGsGdyHBaF3OA12r4Zj+8= 45 | github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= 46 | github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= 47 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 48 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 49 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 50 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 51 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 52 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 53 | github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= 54 | github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= 55 | github.com/sashabaranov/go-openai v1.18.1 h1:AnLoJrFaFtcUYWCtz+8V0zrlXxkiwqpWlAmCAZUnDNQ= 56 | github.com/sashabaranov/go-openai v1.18.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= 57 | github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= 58 | github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= 59 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 60 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 61 | github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= 62 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 63 | go.opentelemetry.io/otel v1.22.0 h1:xS7Ku+7yTFvDfDraDIJVpw7XPyuHlB9MCiqqX5mcJ6Y= 64 | go.opentelemetry.io/otel v1.22.0/go.mod h1:eoV4iAi3Ea8LkAEI9+GFT44O6T/D0GWAVFyZVCC6pMI= 65 | go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.22.0 h1:9M3+rhx7kZCIQQhQRYaZCdNu1V73tm4TvXs2ntl98C4= 66 | go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.22.0/go.mod h1:noq80iT8rrHP1SfybmPiRGc9dc5M8RPmGvtwo7Oo7tc= 67 | go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.22.0 h1:H2JFgRcGiyHg7H7bwcwaQJYrNFqCqrbTQ8K4p1OvDu8= 68 | go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.22.0/go.mod h1:WfCWp1bGoYK8MeULtI15MmQVczfR+bFkk0DF3h06QmQ= 69 | go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.22.0 h1:FyjCyI9jVEfqhUh2MoSkmolPjfh5fp2hnV0b0irxH4Q= 70 | go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.22.0/go.mod h1:hYwym2nDEeZfG/motx0p7L7J1N1vyzIThemQsb4g2qY= 71 | go.opentelemetry.io/otel/metric v1.22.0 h1:lypMQnGyJYeuYPhOM/bgjbFM6WE44W1/T45er4d8Hhg= 72 | go.opentelemetry.io/otel/metric v1.22.0/go.mod h1:evJGjVpZv0mQ5QBRJoBF64yMuOf4xCWdXjK8pzFvliY= 73 | go.opentelemetry.io/otel/sdk v1.22.0 h1:6coWHw9xw7EfClIC/+O31R8IY3/+EiRFHevmHafB2Gw= 74 | go.opentelemetry.io/otel/sdk v1.22.0/go.mod h1:iu7luyVGYovrRpe2fmj3CVKouQNdTOkxtLzPvPz1DOc= 75 | go.opentelemetry.io/otel/trace v1.22.0 h1:Hg6pPujv0XG9QaVbGOBVHunyuLcCC3jN7WEhPx83XD0= 76 | go.opentelemetry.io/otel/trace v1.22.0/go.mod h1:RbbHXVqKES9QhzZq/fE5UnOSILqRt40a21sPw2He1xo= 77 | go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I= 78 | go.opentelemetry.io/proto/otlp v1.0.0/go.mod h1:Sy6pihPLfYHkr3NkUbEhGHFhINUSI/v80hjKIs5JXpM= 79 | go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= 80 | golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= 81 | golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= 82 | golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= 83 | golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 84 | golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 85 | golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= 86 | golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 87 | golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= 88 | golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 89 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 90 | google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97 h1:SeZZZx0cP0fqUyA+oRzP9k7cSwJlvDFiROO72uwD6i0= 91 | google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 h1:W18sezcAYs+3tDZX4F80yctqa12jcP1PUS2gQu1zTPU= 92 | google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97/go.mod h1:iargEX0SFPm3xcfMI0d1domjg0ZF4Aa0p2awqyxhvF0= 93 | google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97 h1:6GQBEOdGkX6MMTLT9V+TjtIRZCw9VPD5Z+yHY9wMgS0= 94 | google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97/go.mod h1:v7nGkzlmW8P3n/bKmWBn2WpBjpOEx8Q6gMueudAmKfY= 95 | google.golang.org/grpc v1.60.1 h1:26+wFr+cNqSGFcOXcabYC0lUVJVRa2Sb2ortSK7VrEU= 96 | google.golang.org/grpc v1.60.1/go.mod h1:OlCHIeLYqSSsLi6i49B5QGdzaMZK9+M7LXN2FKz4eGM= 97 | google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= 98 | google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= 99 | google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= 100 | google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= 101 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 102 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= 103 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= 104 | gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= 105 | gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= 106 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 107 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 108 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 109 | -------------------------------------------------------------------------------- /traceloop-sdk/model/prompt_registry.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import "time" 4 | 5 | type ModelConfig struct { 6 | Mode string `json:"mode"` 7 | Model string `json:"model"` 8 | Temperature float32 `json:"temperature"` 9 | TopP float32 `json:"top_p"` 10 | Stop []string `json:"stop"` 11 | FrequencyPenalty float32 `json:"frequency_penalty"` 12 | PresencePenalty float32 `json:"presence_penalty"` 13 | } 14 | 15 | type Message struct { 16 | Index int `json:"index"` 17 | Role string `json:"role"` 18 | Template string `json:"template"` 19 | Variables []string `json:"variables"` 20 | } 21 | 22 | type PromptVersion struct { 23 | Id string `json:"id"` 24 | Hash string `json:"hash"` 25 | Version uint `json:"version"` 26 | Name string `json:"name"` 27 | CreatedAt time.Time `json:"created_at"` 28 | Provider string `json:"provider"` 29 | TemplatingEngine string `json:"templating_engine"` 30 | Messages []Message `json:"messages"` 31 | LlmConfig ModelConfig `json:"llm_config"` 32 | } 33 | 34 | type Target struct { 35 | Id string `json:"id"` 36 | PromptId string `json:"prompt_id"` 37 | Version string `json:"version"` 38 | UpdatedAt time.Time `json:"updated_at"` 39 | } 40 | 41 | type Prompt struct { 42 | Id string `json:"id"` 43 | Versions []PromptVersion `json:"versions"` 44 | Target Target `json:"target"` 45 | Key string `json:"key"` 46 | CreatedAt time.Time `json:"created_at"` 47 | UpdatedAt time.Time `json:"updated_at"` 48 | } 49 | 50 | type PromptRegistry map[string]*Prompt 51 | -------------------------------------------------------------------------------- /traceloop-sdk/prompt_registry.go: -------------------------------------------------------------------------------- 1 | package traceloop 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "time" 7 | 8 | "github.com/kluctl/go-jinja2" 9 | "github.com/sashabaranov/go-openai" 10 | "github.com/traceloop/go-openllmetry/traceloop-sdk/model" 11 | ) 12 | 13 | type PromptsResponse struct { 14 | Prompts []model.Prompt `json:"prompts"` 15 | Environment string `json:"environment"` 16 | } 17 | 18 | func (instance *Traceloop) populatePromptRegistry() { 19 | resp, err := instance.fetchPathWithRetry(PromptsPath, instance.config.BackoffConfig.MaxRetries) 20 | if err != nil { 21 | fmt.Println("Failed to fetch prompts", err) 22 | return 23 | } 24 | defer resp.Body.Close() 25 | decoder := json.NewDecoder(resp.Body) 26 | 27 | var response PromptsResponse 28 | err = decoder.Decode(&response) 29 | if err != nil { 30 | fmt.Println("Failed to decode response", err) 31 | return 32 | } 33 | 34 | instance.registryMutex.Lock() 35 | for _, prompt := range response.Prompts { 36 | instance.promptRegistry[prompt.Key] = &prompt 37 | } 38 | instance.registryMutex.Unlock() 39 | } 40 | 41 | func (instance *Traceloop) pollPrompts() { 42 | prompts := make(chan []model.Prompt) 43 | errs := make(chan error) 44 | 45 | instance.populatePromptRegistry() 46 | 47 | go func() { 48 | defer close(prompts) 49 | defer close(errs) 50 | 51 | ticker := time.NewTicker(instance.config.PollingInterval) 52 | 53 | for range ticker.C { 54 | instance.populatePromptRegistry() 55 | } 56 | }() 57 | } 58 | 59 | func (instance *Traceloop) getPromptVersion(key string) (*model.PromptVersion, error) { 60 | instance.registryMutex.RLock() 61 | defer instance.registryMutex.RUnlock() 62 | if instance.promptRegistry[key] == nil { 63 | return nil, fmt.Errorf("prompt with key %s not found", key) 64 | } 65 | 66 | if instance.promptRegistry[key].Target.Version == "" { 67 | return nil, fmt.Errorf("prompt with key %s has no version", key) 68 | } 69 | 70 | var promptVersion model.PromptVersion 71 | for _, version := range instance.promptRegistry[key].Versions { 72 | if version.Id == instance.promptRegistry[key].Target.Version { 73 | promptVersion = version 74 | } 75 | } 76 | 77 | if promptVersion.Id == "" { 78 | return nil, fmt.Errorf("prompt version was not found") 79 | } 80 | 81 | return &promptVersion, nil 82 | } 83 | 84 | func (instance *Traceloop) GetOpenAIChatCompletionRequest(key string, variables map[string]any) (*openai.ChatCompletionRequest, error) { 85 | promptVersion, err := instance.getPromptVersion(key) 86 | if err != nil { 87 | return nil, err 88 | } 89 | 90 | jinjaRenderer, err := jinja2.NewJinja2("renderer", 1, jinja2.WithGlobals(variables)) 91 | if err != nil { 92 | return nil, err 93 | } 94 | 95 | var messages []openai.ChatCompletionMessage 96 | 97 | for _, message := range promptVersion.Messages { 98 | renderedMessage, err := jinjaRenderer.RenderString(message.Template) 99 | if err != nil { 100 | return nil, err 101 | } 102 | 103 | messages = append(messages, openai.ChatCompletionMessage{ 104 | Role: message.Role, 105 | Content: renderedMessage, 106 | }) 107 | } 108 | 109 | return &openai.ChatCompletionRequest{ 110 | Model: promptVersion.LlmConfig.Model, 111 | Temperature: promptVersion.LlmConfig.Temperature, 112 | TopP: promptVersion.LlmConfig.TopP, 113 | Stop: promptVersion.LlmConfig.Stop, 114 | FrequencyPenalty: promptVersion.LlmConfig.FrequencyPenalty, 115 | PresencePenalty: promptVersion.LlmConfig.PresencePenalty, 116 | Messages: messages, 117 | }, nil 118 | } 119 | -------------------------------------------------------------------------------- /traceloop-sdk/sdk.go: -------------------------------------------------------------------------------- 1 | package traceloop 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "log" 7 | "net/http" 8 | "os" 9 | "sync" 10 | "time" 11 | 12 | "go.opentelemetry.io/otel/attribute" 13 | "go.opentelemetry.io/otel/sdk/trace" 14 | apitrace "go.opentelemetry.io/otel/trace" 15 | 16 | semconvai "github.com/traceloop/go-openllmetry/semconv-ai" 17 | "github.com/traceloop/go-openllmetry/traceloop-sdk/model" 18 | ) 19 | 20 | const PromptsPath = "/v1/traceloop/prompts" 21 | 22 | type Traceloop struct { 23 | config Config 24 | promptRegistry model.PromptRegistry 25 | registryMutex sync.RWMutex 26 | tracerProvider *trace.TracerProvider 27 | http.Client 28 | } 29 | 30 | type LLMSpan struct { 31 | span apitrace.Span 32 | } 33 | 34 | func NewClient(ctx context.Context, config Config) (*Traceloop, error) { 35 | instance := Traceloop{ 36 | config: config, 37 | promptRegistry: make(model.PromptRegistry), 38 | Client: http.Client{}, 39 | } 40 | 41 | err := instance.initialize(ctx) 42 | if err != nil { 43 | return nil, err 44 | } 45 | 46 | return &instance, nil 47 | } 48 | 49 | func (instance *Traceloop) initialize(ctx context.Context) error { 50 | if instance.config.BaseURL == "" { 51 | baseUrl := os.Getenv("TRACELOOP_BASE_URL") 52 | if baseUrl == "" { 53 | instance.config.BaseURL = "api.traceloop.com" 54 | } else { 55 | instance.config.BaseURL = baseUrl 56 | } 57 | } 58 | 59 | if instance.config.PollingInterval == 0 { 60 | pollingInterval := os.Getenv("TRACELOOP_SECONDS_POLLING_INTERVAL") 61 | if pollingInterval == "" { 62 | instance.config.PollingInterval = 5 * time.Second 63 | } else { 64 | instance.config.PollingInterval, _ = time.ParseDuration(pollingInterval) 65 | } 66 | } 67 | 68 | log.Printf("Traceloop %s SDK initialized. Connecting to %s\n", Version(), instance.config.BaseURL) 69 | 70 | instance.pollPrompts() 71 | err := instance.initTracer(ctx, instance.config.ServiceName) 72 | if err != nil { 73 | return err 74 | } 75 | 76 | return nil 77 | } 78 | 79 | func setMessagesAttribute(span apitrace.Span, prefix string, messages []Message) { 80 | for _, message := range messages { 81 | attrsPrefix := fmt.Sprintf("%s.%d", prefix, message.Index) 82 | span.SetAttributes( 83 | attribute.String(attrsPrefix+".content", message.Content), 84 | attribute.String(attrsPrefix+".role", message.Role), 85 | ) 86 | } 87 | } 88 | 89 | func (instance *Traceloop) tracerName() string { 90 | if instance.config.TracerName != "" { 91 | return instance.config.TracerName 92 | } else { 93 | return "traceloop.tracer" 94 | } 95 | } 96 | 97 | func (instance *Traceloop) getTracer() apitrace.Tracer { 98 | return (*instance.tracerProvider).Tracer(instance.tracerName()) 99 | } 100 | 101 | func (instance *Traceloop) LogPrompt(ctx context.Context, prompt Prompt, workflowAttrs WorkflowAttributes) (LLMSpan, error) { 102 | spanName := fmt.Sprintf("%s.%s", prompt.Vendor, prompt.Mode) 103 | _, span := instance.getTracer().Start(ctx, spanName) 104 | 105 | span.SetAttributes( 106 | semconvai.LLMVendor.String(prompt.Vendor), 107 | semconvai.LLMRequestModel.String(prompt.Model), 108 | semconvai.LLMRequestType.String(prompt.Mode), 109 | semconvai.TraceloopWorkflowName.String(workflowAttrs.Name), 110 | ) 111 | 112 | setMessagesAttribute(span, "llm.prompts", prompt.Messages) 113 | 114 | return LLMSpan{ 115 | span: span, 116 | }, nil 117 | } 118 | 119 | func (llmSpan *LLMSpan) LogCompletion(ctx context.Context, completion Completion, usage Usage) error { 120 | llmSpan.span.SetAttributes( 121 | semconvai.LLMResponseModel.String(completion.Model), 122 | semconvai.LLMUsageTotalTokens.Int(usage.TotalTokens), 123 | semconvai.LLMUsageCompletionTokens.Int(usage.CompletionTokens), 124 | semconvai.LLMUsagePromptTokens.Int(usage.PromptTokens), 125 | ) 126 | 127 | setMessagesAttribute(llmSpan.span, "llm.completions", completion.Messages) 128 | 129 | defer llmSpan.span.End() 130 | 131 | return nil 132 | } 133 | 134 | func (instance *Traceloop) Shutdown(ctx context.Context) { 135 | if instance.tracerProvider != nil { 136 | instance.tracerProvider.Shutdown(ctx) 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /traceloop-sdk/tracing.go: -------------------------------------------------------------------------------- 1 | package traceloop 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "os" 7 | "strings" 8 | 9 | otlp "go.opentelemetry.io/otel/exporters/otlp/otlptrace" 10 | otlpgrpc "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" 11 | otlphttp "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" 12 | "go.opentelemetry.io/otel/sdk/resource" 13 | "go.opentelemetry.io/otel/sdk/trace" 14 | semconv "go.opentelemetry.io/otel/semconv/v1.17.0" 15 | ) 16 | 17 | func newTraceloopExporter(ctx context.Context, config Config) (*otlp.Exporter, error) { 18 | return otlp.New( 19 | ctx, 20 | otlphttp.NewClient( 21 | otlphttp.WithEndpoint(config.BaseURL), 22 | otlphttp.WithHeaders( 23 | map[string]string{ 24 | "Authorization": fmt.Sprintf("Bearer %s", config.APIKey), 25 | }, 26 | ), 27 | ), 28 | ) 29 | } 30 | 31 | func parseHeaders(headers string) map[string]string { 32 | headersArr := strings.Split(headers, ",") 33 | headersMap := make(map[string]string) 34 | for _, header := range headersArr { 35 | h := strings.Split(header, "=") 36 | 37 | if len(h) == 2 { 38 | headersMap[h[0]] = h[1] 39 | } 40 | } 41 | 42 | return headersMap 43 | } 44 | 45 | func newGenericExporter(ctx context.Context) (*otlp.Exporter, error) { 46 | protocol := os.Getenv("OTEL_EXPORTER_OTLP_PROTOCOL") 47 | endpoint := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT") 48 | headers := parseHeaders(os.Getenv("OTEL_EXPORTER_OTLP_HEADERS")) 49 | 50 | // Default to http/protobuf 51 | if protocol == "" || protocol == "http/protobuf" { 52 | return otlp.New( 53 | ctx, 54 | otlphttp.NewClient( 55 | otlphttp.WithEndpoint(endpoint), 56 | otlphttp.WithHeaders(headers), 57 | ), 58 | ) 59 | } else if protocol == "grpc" { 60 | return otlp.New( 61 | ctx, 62 | otlpgrpc.NewClient( 63 | otlpgrpc.WithEndpoint(endpoint), 64 | otlpgrpc.WithHeaders(headers), 65 | ), 66 | ) 67 | } else { 68 | // Not supporting http/json for now 69 | return nil, fmt.Errorf("invalid OTLP exporter type") 70 | } 71 | } 72 | 73 | func newOtlpExporter(ctx context.Context, config Config) (*otlp.Exporter, error) { 74 | if os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT") == "" { 75 | return newTraceloopExporter(ctx, config) 76 | } else { 77 | return newGenericExporter(ctx) 78 | } 79 | } 80 | 81 | func resourceName(serviceName string) string { 82 | if serviceName != "" { 83 | return serviceName 84 | } 85 | 86 | envVar := os.Getenv("OTEL_SERVICE_NAME") 87 | if envVar != "" { 88 | return envVar 89 | } 90 | 91 | if len(os.Args) > 0 { 92 | return os.Args[0] 93 | } 94 | 95 | return "unknown_service" 96 | } 97 | 98 | func newTracerProvider(ctx context.Context, serviceName string, exp trace.SpanExporter) (*trace.TracerProvider, error) { 99 | r, err := resource.New( 100 | ctx, 101 | resource.WithAttributes( 102 | semconv.ServiceNameKey.String(resourceName(serviceName)), 103 | ), 104 | ) 105 | 106 | if err != nil { 107 | return nil, err 108 | } 109 | 110 | return trace.NewTracerProvider( 111 | trace.WithBatcher(exp), 112 | trace.WithResource(r), 113 | ), nil 114 | } 115 | 116 | func (instance *Traceloop) initTracer(ctx context.Context, serviceName string) error { 117 | exp, err := newOtlpExporter(ctx, instance.config) 118 | if err != nil { 119 | return fmt.Errorf("create otlp exporter: %w", err) 120 | } 121 | 122 | tp, err := newTracerProvider(ctx, serviceName, exp) 123 | if err != nil { 124 | return fmt.Errorf("create tracer provider: %w", err) 125 | } 126 | 127 | instance.tracerProvider = tp 128 | 129 | return nil 130 | } 131 | -------------------------------------------------------------------------------- /traceloop-sdk/tracing_types.go: -------------------------------------------------------------------------------- 1 | package traceloop 2 | 3 | type Message struct { 4 | Index int `json:"index"` 5 | Role string `json:"role"` 6 | Content string `json:"content"` 7 | } 8 | 9 | type Prompt struct { 10 | Vendor string `json:"vendor"` 11 | Model string `json:"model"` 12 | Mode string `json:"mode"` 13 | Temperature float32 `json:"temperature"` 14 | TopP float32 `json:"top_p"` 15 | Stop []string `json:"stop"` 16 | FrequencyPenalty float32 `json:"frequency_penalty"` 17 | PresencePenalty float32 `json:"presence_penalty"` 18 | Messages []Message `json:"messages"` 19 | } 20 | 21 | type Completion struct { 22 | Model string `json:"model"` 23 | Messages []Message `json:"messages"` 24 | } 25 | 26 | type WorkflowAttributes struct { 27 | Name string `json:"workflow_name"` 28 | AssociationProperties map[string]string `json:"association_properties"` 29 | } 30 | 31 | type Usage struct { 32 | TotalTokens int `json:"total_tokens"` 33 | CompletionTokens int `json:"completion_tokens"` 34 | PromptTokens int `json:"prompt_tokens"` 35 | } 36 | -------------------------------------------------------------------------------- /traceloop-sdk/utils.go: -------------------------------------------------------------------------------- 1 | package traceloop 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | 7 | "github.com/cenkalti/backoff" 8 | ) 9 | 10 | func (instance *Traceloop) fetchPath(path string) (*http.Response, error) { 11 | req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://%s/%s", instance.config.BaseURL, path), nil) 12 | if err != nil { 13 | fmt.Printf("Failed to create request: %v\n", err) 14 | return nil, err 15 | } 16 | 17 | req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", instance.config.APIKey)) 18 | req.Header.Set("X-Traceloop-SDK-Version", Version()) 19 | 20 | return instance.Client.Do(req) 21 | } 22 | 23 | func (instance *Traceloop) fetchPathWithRetry(path string, maxRetries uint64) (*http.Response, error) { 24 | var resp *http.Response 25 | 26 | err := backoff.Retry(func() error { 27 | var err error 28 | resp, err = instance.fetchPath(path) 29 | if err != nil { 30 | fmt.Printf("Failed to fetch path: %v\n", err) 31 | } 32 | return err 33 | }, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), maxRetries)) 34 | 35 | return resp, err 36 | } 37 | -------------------------------------------------------------------------------- /traceloop-sdk/version.go: -------------------------------------------------------------------------------- 1 | package traceloop 2 | 3 | func Version() string { 4 | return "0.0.2" 5 | } 6 | -------------------------------------------------------------------------------- /traceloop-sdk/workflow.go: -------------------------------------------------------------------------------- 1 | package traceloop 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | 7 | semconvai "github.com/traceloop/go-openllmetry/semconv-ai" 8 | "go.opentelemetry.io/otel/trace" 9 | ) 10 | 11 | type Workflow struct { 12 | sdk *Traceloop 13 | ctx context.Context 14 | Attributes WorkflowAttributes `json:"workflow_attributes"` 15 | } 16 | 17 | type Task struct { 18 | workflow *Workflow 19 | ctx context.Context 20 | Name string `json:"name"` 21 | } 22 | 23 | func (instance *Traceloop) NewWorkflow(ctx context.Context, attrs WorkflowAttributes) *Workflow { 24 | wCtx, span := instance.getTracer().Start(ctx, fmt.Sprintf("%s.workflow", attrs.Name), trace.WithNewRoot()) 25 | 26 | span.SetAttributes( 27 | semconvai.TraceloopWorkflowName.String(attrs.Name), 28 | semconvai.TraceloopSpanKind.String("workflow"), 29 | semconvai.TraceloopEntityName.String(attrs.Name), 30 | ) 31 | 32 | return &Workflow{ 33 | sdk: instance, 34 | ctx: wCtx, 35 | Attributes: attrs, 36 | } 37 | } 38 | 39 | func (workflow *Workflow) End() { 40 | trace.SpanFromContext(workflow.ctx).End() 41 | } 42 | 43 | func (workflow *Workflow) LogPrompt(prompt Prompt) (LLMSpan, error) { 44 | return workflow.sdk.LogPrompt(workflow.ctx, prompt, workflow.Attributes) 45 | } 46 | 47 | func (workflow *Workflow) NewTask(name string) *Task { 48 | tCtx, span := workflow.sdk.getTracer().Start(workflow.ctx, fmt.Sprintf("%s.task", name)) 49 | 50 | span.SetAttributes( 51 | semconvai.TraceloopWorkflowName.String(workflow.Attributes.Name), 52 | semconvai.TraceloopSpanKind.String("task"), 53 | semconvai.TraceloopEntityName.String(name), 54 | ) 55 | 56 | return &Task{ 57 | workflow: workflow, 58 | ctx: tCtx, 59 | Name: name, 60 | } 61 | } 62 | 63 | func (task *Task) End() { 64 | trace.SpanFromContext(task.ctx).End() 65 | } 66 | 67 | func (task *Task) LogPrompt(prompt Prompt) (LLMSpan, error) { 68 | return task.workflow.sdk.LogPrompt(task.ctx, prompt, task.workflow.Attributes) 69 | } 70 | --------------------------------------------------------------------------------